Thursday, 6 September 2012

vb lesson -3


Checkbox is used to deal with toggle options, which have only two possible states – true/ false, or yes/no.

Checkbox can be in either of the two states – checked or unchecked.  When checkbox is checked, a tick mark appears in the rectangle. When it is unchecked, the rectangle will be empty.

Options buttons are used to allow user to select one from a set of mutually exclusive options. You can have more than one set of option button but in that case each set of option button should be placed inside a separate Frame control. 
Out of the available option buttons only one option button can be selected at a time. Selecting an option button will automatically unselect all other option buttons in the group.

Frame control is used to group option buttons physically and logically. All the option buttons placed in one frame are considered to be a group and only one option button within the group frame can be selected. Visually, frame is a rectangle that encloses a set of controls.

A Frame is also used to provide better visibility. A Frame can enclose a set of controls related to a particular topic.

Now, let us develop a small program to understand how to use checkboxes. Later we will develop one more application to deal with option buttons.
Here are the steps to create a sample application:

1.      Start a new project using File->New Project and select Standard Exe as the type of project.
2.      Place a textbox, three checkboxes and a command button on the form.
Change properties of form and controls 

1.      Now try to arrange control as shown in figure 3.1. Please see “Aligning and Sizing control” section for further details.

Aligning and Sizing Control
You have to align and resize control to make them look good. But this process could be very tedious if you have more control on a form. The common tasks in this process are:

¨       Setting two or more controls to the same size.
¨       Aligning two or more controls to the required direction.
¨       Giving equal amount of gap between control.
The above mentioned tasks can be carried out by you manually, but if you use facilities provided by Visual Basic IDE, then it will be more accurate and less time consuming.

But before we take any action on a set of controls, you have to select multiple control.

To select multiple controls:
1.      Select first control by clicking on it
2.      While selecting second control onwards, select control by holding down CTRL key.

Or

Identify the rectangle where you have controls that are to be selected.
3.      Go to upper left corner of the rectangle and click at the location and hold down the mouse button.
4.      Drag mouse pointer until you reach bottom right corner of the rectangle. As you drag mouse pointer you must see a dashed rectangle being drawn to show you the area that is being enclosed.
5.      Release mouse button once you reach button right corner of the rectangle.
At this stage all the controls that are enclosed by the rectangle will be selected.
Here, second method is possible only when controls are placed adjacently.

To align multiple controls:
1.      Select the control that you want to align using either of the methods given above.
2.      Select Format -> Align and select one of the option listed there. See figure 3.2
3.      Visual Basic aligns all selected controls according to the selection, taking the automatically last selected control as the base.
 
To change the size of a set of controls to the same size:
1.      Select all controls that you want to set to same size.
2.      Select Format ->Make Same Size and select one of the options given in sub menu. The options are Width, Height and Both.
Visual Basic changes the width or height or both, depending upon selection, to the size of last selected control.
To change space between controls:
1.      Select controls between which the space is to be adjusted.
2.      Select Format menu and Horizontal Spacing or Vertical Spacing depending upon the need.
After having understood the options available to arrange controls in the required manner. Apply them if you need.

We have to write code for each checkbox. Whenever user clicks on the checkbox, the state of checkbox is toggled. That means, if checkbox is checked it will be unchecked and if checkbox is unchecked then it will be checked.  So we have to write code for Click event of the checkbox. And depending upon the status of the checkbox we have to either turn on or off the required font attribute.

Value property of checkbox
Value property of checkbox indicates whether checkbox is currently checked or unchecked. The valid values are; 0 is Unchecked (default), 1 is Checked and 2 is Grayed (dimmed).

Writing code
The following is the required code to change font attributes of textbox as and when ever user clicks on the check box.

Private Sub chkBold_Click()
  ' turn on bold attribute if checkbox is checked
  If chkBold.Value = 1 Then
       txtsample.Font.Bold = True
  Else
       txtsample.Font.Bold = False
  End If
 
End Sub

Private Sub chkItalic_Click()
  ' turn on Italic  attribute if checkbox is checked
  If chkItalic.Value = 1 Then                              
       txtsample.Font.Italic = True
  Else
       txtsample.Font.Italic = False
  End If
End Sub

Private Sub chkUnderline_Click()
  ' turn on Underline attribute if checkbox is checked
  If chkUnderline.Value = 1 Then
       txtsample.Font.Underline = True
  Else
       txtsample.Font.Underline = True
  End If
End Sub

Private Sub cmdquit_Click()
   unload current form and as the result program is terminated   Unload Me
End Sub




No comments:

Post a Comment