Thursday, August 27, 2009

VB6 CommandButton and OptionButton Controls - Visual Basic 6


When compared to TextBox controls, these controls are really simple. Not only do they expose relatively few properties, they also support a limited number of events, and you don't usually write much code to manage them.

CommandButton Controls in VB6

Using CommandButton controls is trivial. In most cases, you just draw the control on the form's surface, set its Caption property to a suitable string (adding an & character to associate a hot key with the control if you so choose), and you're finished, at least with user-interface issues. To make the button functional, you write code in its Click event procedure, as in this fragment:
Private Sub Command1_Click()
' Save data, then unload the current form.
Call SaveDataToDisk
Unload Me
End Sub
You can use two other properties at design time to modify the behavior of a CommandButton control. You can set the Default property to True if it's the default push button for the form (the button that receives a click when the user presses the Enter key—usually the OK or Save button). Similarly, you can set the Cancel property to True if you want to associate the button with the Escape key.
The only relevant CommandButton's run-time property is Value, which sets or returns the state of the control (True if pressed, False otherwise). Value is also the default property for this type of control. In most cases, you don't need to query this property because if you're inside a button's Click event you can be sure that the button is being activated. The Value property is useful only for programmatically clicking a button:
This fires the button's Click event.
Command1.Value = True
The CommandButton control supports the usual set of keyboard and mouse events (KeyDown, KeyPress, KeyUp, MouseDown, MouseMove, MouseUp, but not the DblClick event) and also the GotFocus and LostFocus events, but you'll rarely have to write code in the corresponding event procedures.

Properties of a CommandButton control

  • To display text on a CommandButton control, set its Caption property.
  • An event can be activated by clicking on the CommandButton.
  • To set the background colour of the CommandButton, select a colour in the BackColor property.
  • To set the text colour set the Forecolor property.
  • Font for the CommandButton control can be selected using the Font property.
  • To enable or disable the buttons set the Enabled property to True or False
  • To make visible or invisible the buttons at run time, set the Visible property to True or False.
  • Tooltips can be added to a button by setting a text to the Tooltip property of the CommandButton.
  • A button click event is handled whenever a command button is clicked. To add a click event handler, double click the button at design time, which adds a subroutine like the one given below.

    Private Sub Command1_Click( )
    ..................
    End Sub

OptionButton Controls in VB6

OptionButton controls are also known as radio buttons because of their shape. You always use OptionButton controls in a group of two or more because their purpose is to offer a number of mutually exclusive choices. Anytime you click on a button in the group, it switches to a selected state and all the other controls in the group become unselected.
Preliminary operations for an OptionButton control are similar to those already described for CheckBox controls. You set an OptionButton control's Caption property to a meaningful string, and if you want you can change its Alignment property to make the control right aligned. If the control is the one in its group that's in the selected state, you also set its Valueproperty to True. (The OptionButton's Value property is a Boolean value because only two states are possible.) Value is the default property for this control.
At run time, you typically query the control's Value property to learn which button in its group has been selected. Let's say you have three OptionButton controls, named optWeekly, optMonthly, and optYearly. You can test which one has been selected by the user as follows:
If optWeekly.Value Then
' User prefers weekly frequency.
ElseIf optMonthly.Value Then
' User prefers monthly frequency.
ElseIf optYearly.Value Then
' User prefers yearly frequency.
End If
Strictly speaking, you can avoid the test for the last OptionButton control in its group because all choices are supposed to be mutually exclusive. But the approach I just showed you increases the code's readability.
A group of OptionButton controls is often hosted in a Frame control. This is necessary when there are other groups of OptionButton controls on the form. As far as Visual Basic is concerned, all the OptionButton controls on a form's surface belong to the same group of mutually exclusive selections, even if the controls are placed at the opposite corners of the window. The only way to tell Visual Basic which controls belong to which group is by gathering them inside a Frame control. Actually, you can group your controls within any control that can work as a container—PictureBox, for example—but Frame controls are often the most reasonable choice.
Example
Open a new Standard EXE project and the save the Form as Option.frm and save the project as Option.vbp.
Design the Form as per the following specifications table.
Object
Property
Settings
Label Caption
Name
Enter a Number
Label1
TextBox Text
Name
(empty)
Text1
CommandButton Caption
Name
&Close
Command1
OptionButton Caption
Name
&Octal
optOct
OptionButton Caption
Name
&Hexadecimal
optHex
OptionButton Caption
Name
&Decimal
optDec
The application responds to the following events

  • The change event of the TextBox reads the value and stores it in a form-level numeric variable.
  • The click event of optOct button returns curretval in octal.
  • The click event of the optHex button curerntval in hexadecimal
  • The click event of the optDec button returns the decimal equivalent of the value held currentval.
The following code is entered in the general declarations section of the Form.
Dim currentval as variant
The variable is initialized to 0 by default. The change event procedure checks to ascertain the number system (Octal, Hexadecimal) that is in effect and then reads in the number.
Private Sub Text1_Change()
If optOct.Value = True Then
currentval = Val ("&O" & LTrim (Text1.Text) & "&")
Elseif optDec.value = True Then
currentval = Val (LTrim (Text1.Text) & "&")
Else
currentval = Val ("&H" & LTrim (Text1.Text) & "&")
End if
End Sub
The Val function is used to translate string to a number and can recognize Octal and Hexadecimal strings. The LTrim function trims the leading blanks in the text. The following code is entered in the click events of the OptionButton controls.
Private Sub optOct_Click()
Text1.Text = Oct(currentval)
End Sub
Private Sub optHex_Click()
Text1.Text = Hex(currentval)
End Sub
Private Sub optDec_Click()
Text1.Text = Format(currentval)
End Sub
The follwoing code is entered in the click event of teh Close button.
Private Sub cmdClose_Click()
Unlod Me
End Sub
The Application is run by pressing F5 or clicking on the Run icon in the tool bar. By pressing the Exit button the program is terminated.

0 comments:

Your Ad Here