Visual Basic Concepts

Using the Value of a Control

All controls have a property that you can use to store or retrieve values just by referring to the control, without using the property name. This is called the value of the control and is usually the most important or most commonly used property of the control. The following table lists the property that is considered to be the value for each control.

Control Value
Check box Value
Combo box Text
Command button Value
Common dialog Action
Data Caption
DataCombo Text
DataGrid Text
DataList Text
Directory list box Path
Drive list box Drive
File list box FileName
FlexGrid Text
Frame Caption
Horizontal scroll bar Value
Image Picture
Label Caption
Line Visible
List box Text
Option button Value
Picture box Picture
Shape Shape
Text box Text
Timer Enabled
Vertical scroll bar Value

Whenever you want to refer to a property on a control that happens to be the value of that control, you can do so without specifying the property name in your code. For example, this line sets the value of the Text property of a text box control:

Text1 = "This text is assigned to the Text property _
of Text1"

In this example, the Caption property of Label1 is set to the FileName property of File1 whenever the user clicks a file in the file list box:

Private Sub File1_Click ()
   Label1 = File1
End Sub

Note   Because using the value of a control makes your code somewhat less readable, the examples in this guide do not use it but instead refer explicitly to the properties on all controls. You may want to try writing your code both ways, and decide to use the value of controls in your code if you have no trouble reading it.