Freetutes.com

VB6 beginners tutorial - Learn VB6

Advanced VB6 tutorial - Learn Advanced VB6

Systems Analysis - System analysis and Design tutorial for Software Engineering


You are here: Visual Basic > Advanced VB6 tutorial > Chapter 3

Important Common Properties of CommandButtons, TextBoxes, and Labels

There are several properties that are shared by many of the standard controls.

Name
You use a control's Name property in code to refer to the control object when you want to manipulate its properties or methods. For example, if you name a TextBox txtFirst, you could write code to change its Enabled property and invoke its Move method as follows:

txtFirst.Enabled = True
txtFirst.Move 100,200,500,200

A control's Name also becomes part of all the event procedure names of that control. See the section in this chapter on "Assigning Code to a Control to Respond to an Event" for more discussion and its implications.

You should always rename a control a meaningful name as soon as you place it on its container. Most VB programmers use the "Hungarian notation" convention for naming controls and variables. This means that the name of each control begins with a lowercase prefix that is one to three (or sometimes four or five) letters long. The prefix is the same for all objects of the same type.

For instance, you should rename a TextBox control a name beginning with the letters txt as shown in the example given just above with txtFirst. Although you can reference the control's Name property directly in code (though it's almost never necessary), you cannot change the Name property at runtime.

Enabled
The Enabled property of a control is a True/False property that you can set to determine whether or not the control can receive focus or respond to user-generated events such as the Click event. Many controls' Caption or Text properties (including the CommandButton, TextBox and Label) will appear fainter or "grayed out" to the user when you set their Enabled properties to False, as illustrated in Figure 3.12.

A form with Enabled and Disabled controls.
FIGURE 3.12 A form with Enabled and Disabled controls.

Since the Label control never gets focus, its Enabled property has no effect on whether the user can set focus to the Label (the user never could set focus to a Label, anyway). However, an enabled Label can still receive events such as the Click and DblClick events when the mouse pointer is over it. Setting the Label's Enabled property to False disables these events for the Label as it does for other controls.

You can set a control's Enabled property at both design time and runtime.

Visible
This property is True by default. Setting it to False means that the control will not be visible to the user. If you set Visible to False at design time, you (the programmer) will still be able to see the control on the design surface but the user won't be able to see it at runtime. You can set the Visible property at both design time and runtime.

Font
This property is actually an object that contains many properties of its own. You can manipulate the Font object's properties through a design time dialog box that you can call up in one of two ways:

  • Double-click the word "Font" in the control's Properties window (see Figure 3.13).

    The Font property dialog box.
    FIGURE 3.13 The Font property dialog box.
  • Click the ellipsis button (…) to the right of the word "Font" in the Properties window.

You may also refer to the Font object's sub-properties in your code by using double-dotted syntax or the With construct. For instance, if you wanted to make the type in the Label named lblName appear bold (after first saving its original Bold setting), you would write the lines of code:

Dim blnOrigBold As Boolean
BlnOrigBold = lblName.Font.Bold
lblName.Font.Bold = True

However, if you wanted to refer to or manipulate several Font properties at the same time, it is more efficient to write lines such as

Dim blnOrigBold As Boolean, blnOrigUnderline As Boolean
Dim sOrigFontName As String
Dim iOrigSize As Integer
With lblName.Font
BlnOrigBold = .Bold
.Bold = True
blnOrigUnderLine = .Underline
.Underline = False
sOrigFontName = .Name
.Font = "Courier"
iOrigSize = .Size
.Size = 24
End With

NOTE: Different Use of a Timer's Enabled Property The Enabled property for the Timer control has a different meaning from the other controls. When you set a Timer's Enabled property to True, you cause the Timer event to fire at the interval of milliseconds specified by the Timer's Interval property. When you set its Enabled property to False, the Timer event will not fire.


  

<< Previous | Contents | Next >>

Home | About Us | Privacy Policy | Contact Us

Copyright © Freetutes.com | All Rights Reserved