UNIT-II Handling controls


3.2.1 The Text Box  
The text box is the standard control for accepting input from the user as well as to display the output. It can handle string (text) and numeric data but not images or pictures. String in a text box can be converted to a numeric data by using the function Val(text). The following example illustrates a simple program that processes the input from the user.
Example 3.1
In this program, two text boxes are inserted into the form together with a few labels. The two text boxes are used to accept inputs from the user and one of the labels will be used to display the sum of two numbers that are entered into the two text boxes. Besides, a command button is also programmed to calculate the sum of the two numbers using the plus operator. The program use creates a variable sum to accept the summation of values from text box 1 and text box 2.The procedure to calculate and to display the output on the label is shown below. The output is shown in Figure 3.2
Private Sub Command1_Click()
‘To add the values in text box 1 and text box 2
Sum = Val(Text1.Text) + Val(Text2.Text)
‘To display the answer on label 1
Label1.Caption = Sum
End Sub
Figure 3.2

3.2.2 The Label  
The label is a very useful control for Visual Basic, as it is not only used to provide instructions and guides to the users, it can also be used to display outputs. One of its most important properties is Caption. Using the syntax label.Caption, it can display text and numeric data . You can change its caption in the properties window and also at runtime.  Please refer to Example 3.1 and Figure 3.1 for the usage of label.
 3.2.3 The Command Button
The command button is one of the most important controls as it is used to execute commands. It displays an illusion that the button is pressed when the user click on it. The most common event associated with the command button is the Click event, and the syntax for the procedure is
Private Sub Command1_Click ()
Statements
End Sub
3.2.4 The Picture Box
The Picture Box is one of the controls that is used to handle graphics. You can load a picture at design phase by clicking on the picture item in the properties window and select the picture from the selected folder. You can also load the picture at runtime using the LoadPicture method. For example, the statement will load the picture grape.gif into the picture box.
Picture1.Picture=LoadPicture ("C:\VB program\Images\grape.gif")
You will learn more about the picture box in future lessons. The image in the picture box is not resizable.

 3.2.5 The Image Box
The Image Box is another control that handles images and pictures. It functions almost identically to the picture box. However, there is one major difference, the image in an Image Box is stretchable, which means it can be resized. This feature is not available in the Picture Box. Similar to the Picture Box, it can also use the LoadPicture method to load the picture. For example, the statement loads the picture grape.gif into the image box.
Image1.Picture=LoadPicture ("C:\VB program\Images\grape.gif")
 3.2.6 The List Box
The function of the List Box is to present a list of items where the user can click and select the items from the list. In order to add items to the list, we can use the AddItem method. For example, if you wish to add a number of items to list box 1, you can key in the following statements
Example 3.2

Private Sub Form_Load ( )

List1.AddItem “Lesson1”
List1.AddItem “Lesson2”
List1.AddItem “Lesson3”
List1.AddItem “Lesson4”

End Sub
The items in the list box can be identified by the ListIndex property, the value of the ListIndex for the first item is 0, the second item has a ListIndex 1, and the second item has a ListIndex 2 and so on
3.2.7 The Combo Box
The function of the Combo Box is also to present a list of items where the user can click and select the items from the list. However, the user needs to click on the small arrowhead on the right of the combo box to see the items which are presented in a drop-down list. In order to add items to the list, you can also use the AddItem method. For example, if you wish to add a number of items to Combo box 1, you can key in the following statements
Example 3.3
Private Sub Form_Load ( )

Combo1.AddItem “Item1”
Combo1.AddItem “Item2”
Combo1.AddItem “Item3”
Combo1.AddItem “Item4”

End Sub

3.2.8 The Check Box
The Check Box control lets the user  selects or unselects an option. When the Check Box is checked, its value is set to 1 and when it is unchecked, the value is set to 0.  You can include the statements Check1.Value=1 to mark the Check Box and Check1.Value=0 to unmark the Check Box, as well as  use them to initiate certain actions. For example, the program will change the background color of the form to red when the check box is unchecked and it will change to blue when the check box is checked.  You will learn about the conditional statement If….Then….Elesif in later lesson. VbRed and vbBlue are color constants and BackColor is the background color property of the form.
Example 3.4
Private Sub Command1_Click()
If Check1.Value = 1 And Check2.Value = 0 Then
MsgBox "Apple is selected"
ElseIf Check2.Value = 1 And Check1.Value = 0 Then
MsgBox "Orange is selected"
Else
MsgBox "All are selected"
End If
End Sub

3.2.9 The Option Box
 The Option Box control also lets the user selects one of the choices. However, two or more Option Boxes must work together because as one of the Option Boxes is selected, the other Option Boxes will be unselected. In fact, only one Option Box can be selected at one time. When an option box is selected, its value is set to “True” and when it is unselected; its value is set to “False”. In the following example, the shape control is placed in the form together with six Option Boxes. When the user clicks on different option boxes, different shapes will appear. The values of the shape control are 0, 1, and 2,3,4,5 which will make it appear as a rectangle, a square, an oval shape, a rounded rectangle and a rounded square respectively.
Example 3.5
Private Sub Option1_Click ( )
Shape1.Shape = 0
End Sub

Private Sub Option2_Click()
Shape1.Shape = 1
End Sub

Private Sub Option3_Click()
Shape1.Shape = 2
End Sub

Private Sub Option4_Click()
Shape1.Shape = 3
End Sub

Private Sub Option5_Click()
Shape1.Shape = 4
End Sub

Private Sub Option6_Click()
Shape1.Shape = 5
End Sub

 3.2.10 The Drive List Box
The Drive ListBox is for displaying a list of drives available in your computer. When you place this control into the form and run the program, you will be able to select different drives from your computer as shown in Figure 3.3

Figure 3.3 The Drive List Box

 3.2.11 The Directory List Box
The Directory List Box is for displaying the list of directories or folders in a selected drive. When you place this control into the form and run the program, you will be able to select different directories from a selected drive in your computer as shown in Figure 3.4
Figure 3.4 The Directory List Box

 The File List Box
          The File List Box is for displaying the list of files in a selected directory or folder. When you place this control into the form and run the program, you will be able to shown the list of files in a selected directory as shown in figure
                               

Shape :
  The Shape control provides an easy way to draw rectangles, circles, and other shapes on a form at design time.                  
                                                 
Line :
             The Line control provides an easy way to draw lines on a form at design time

                                                   
     Shape and Line controls are useful for drawing graphical elements on the surface of a form.        
Data :
     Data control is used as a mechanism for binding controls to a database .  
                                     
                                                   
OLE (Object Linking and Embedding) :
OLE (Object Linking and Embedding) is a means to interchange data between applications. In object embedding, an object is embedded in the client application. Along with the object, client application also stores the information regarding source application (or server) that created the object. The data stored in client application is separate and no link is maintained between the data supplied by source application and data stored in client application.
            The advantage with Object Embedding is, client application maintains its own copy of the data.

                                                 



Control Arrays :
Control Array (mostly used in Visual Basic) is the collection of controls that programmer put it in form (VB Form). Control array is always a single dimensional array. Control array is that you can add or delete array elements at run-time. With some controls, it is very useful to define control arrays - it depends on the application.
Control arrays are a convenient way to handle groups of controls that perform a similar function. All of the events available to the single control are still available to the array of controls, the only difference -being an argument indicating the index of the selected array, element is passed to the event. Hence, instead of writing individual procedures for each control (i.e. not using control arrays), you only have to write one procedure for each array.
The VisualBasic objects that can be interacted with by a user are referred to as Control Objects and are normally placed onto the form by the programmer. It is often useful to have an array of such control objects, and they can be created in the following ways.
1. Create the first object and name it and set its properties to the required values and set the Index to 0. Copy and paste this object and each time the new object becomes the next element in the array automatically setting the Index to 1, 2, 3 etc. Thereafter the array element can be addressed as Name(0), Name(1) .... Name(I) etc.
2. Alternatively if you create the first object and name it then create a second object and give it the same name VisualBasic will then ask if you wish to create a "Control Array". Each time the new object is given the same name it will be created as the next element of the array. Thereafter the array element can be addressed as Name(0), Name(1) .... Name(I) etc.
3. If you need a large array it is more useful to create the array computationally. To do this you must create the first control object and set its Index to the lowest value, usually 0. Then within an appropriate subroutine, usually the Form_Load( ), write a For loop to generate the rest of the elements of the array. Code will be needed to place each object at an appropriate location on the Form otherwise they will appear on top of each other. In addition it is necessary to set each object to be visible. Thereafter the array element can be addressed as Name(0), Name(1) .... Name(I) etc.
4. To enable the program to determine which button has been clicked an event driven subroutine is automatically setup with the Index as a parameter. Thus code within that subroutine can access the Index value as indicated in the example below.

  Private Sub Button_Click(Index As
Integer)
Text1.Text = Index
End Sub
       
________________________________________

Private Sub Form_Load()
Dim i As Integer
Button(0).Caption = 0
For i = 1 To 5
Load Button(i)
Button(i).Top = Button(i - 1).Top + 1.2 *Button(0).Height
Button(i).Caption = i
Button(i).Visible = True
Next i


  The Form created by the programmer is shown above and when the program is running it is shown on the left. The five additional button are created at run time and located below the previous one using the Top property of the object. The text box displays the Index value (3) indicating that Button (3) has been clicked.
Creation of pop-up menu
It is created via the menu editor in the same manner as a drop-down menu except that the main menu item is not visible. An event procedure must be entered into the code editor. The general form of the event procedure is
Private sub form_mouseDown(Button as Integer, Shift as Inter, X as Single,                              Y as Single )
                  If button=vbrightbutton then
                            Popmenu  ………
                  End if
            End sub
Ex:
                                   
Private Sub Command1_Click()
          End
End Sub
Private Sub Form_MouseDown(Button As Integer, Shift As Integer, x As Single, Y       As Single)
       If Button = vbRightButton Then
                 PopupMenu mnucolor
      End If
End Sub
Private Sub mnuclear_Click()
          Shape1.FillColor = vbclear
End Sub
Private Sub mnugreen_Click()
         Shape1.FillColor = vbGreen
End Sub
Private Sub mnublue_Click()
           Shape1.FillColor = vbBlue
End Sub
Private Sub mnured_Click()
            Shape1.FillColor = vbRed
End sub
Mouse Events

Visual Basic responds to various mouse events, which are recognized by most of the controls.
The main events are
MouseDown,
MouseUp
MouseMove.

MouseDown :
Mouse Down occurs when the user presses any mouse button
 MouseUp :
Mouse Up occurs when the user releases any mouse button.
MouseMove :
Mouse Move occurs when the user moves mouse pointer.

These events use the arguments
Button,
Shift,
X, Y and they contain information about the mouse's condition when the button is clicked.

Button:
  The first argument is an integer called Button. The value of the argument indicates whether the left, right or middle mouse button was clicked.
Shift:
   The second argument in an integer called shift. The value of this argumnet indicates whether the mouse button was clicked simultaneously with the Shift key, Ctrl key or Alt key.
X and Y (X,Y) :
       The third and fourth arguments X and Y are the coordinates of the mouse location at the time the mouse button was clicked. As the Form_MouseDown( ) is executed automatically whenever the mouse button is clicked inside the Form's area the X, Y co-ordinates are referenced to the form.
Positioning a control:
                       MouseDown is the commonly used event and it is combined with the move method to move an Image control to different locations in a Form.

The following example illustrates the movement of objects responding to mouse events
Ex:
Open a new standard EXE project and the Form.
Add two OptionButton controls, two Images and a CommandButton as shown below.

                       

Enter the following code in the Form_MouseDown() procedure
Private Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
If Option1 = True Then
Image1.Move X, Y
Else
Image2.Move X, Y
End If
End Sub
Now execute the program by pressing F5.
The application is designed in such a way that when an OptionButton is selected, the corresponding Image control is placed anywhere in the Form whenever it is clicked which is shown below.

                   

Graphical Mouse Application :
Mouse events can be combined with graphics methods and any number of customized drawing or painting applications can be created.
The following example illustrates a drawing using both the MouseMove and MouseDown events.
Ex:
Open a new standard EXE project and the Form.
Enter the following code in the Form_MouseDown() procedure
Private Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
Form2.CurrentX = X
Form2.CurrentY = Y
End Sub
Enter the following code in the Form_MouseMove() procedure
Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
If Button = 1 Then
    Line (Form2.CurrentX, Form2.CurrentY)  -  (X, Y)
End If
End Sub
Now execute the program by pressing F5.
When the mouse button is clicked and moved in the Form a line is drawn corresponding to the mouse movement which is shown below.

                     




MouseMove Application :
MouceMove event occurs when the user moves the mouse pointer. These events uses three arguments i.e.  
Button : The first argument is an integer called Button. The value         indicates the left, right or middle mouse button  when clicked.
Shift : The value of this argument indicates whether the mouse button was clicked simultaneously.
X,Y : These are the coordinates of the mouse location at the time the mouse button was clicked0.
The following application illustrates how often the Form_MouseMove() procedure is executed :
Ex:
Open a new standard EXE project and the Form and place a CommandButton on the Form. Change the caption of the CommandButton as CLEAR.

                 
Enter the following code in the Form_MouseMove() procedure
Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
              Circle (X, Y), 80
End Sub
Enter the following code in the Command1_Click () procedure
Private Sub Command1_Click()
Form2.Cls
End Sub
Now execute the program by pressing F5.
When the mouse button is moved inside the Form circles are drawn along the path of the mouse movement which is shown below and when the user clicks the clear button it clears off the circles from the screen.
                                 
Dragging and Dropping :
Dragging is the process of clicking the mouse button in a control and moving the mouse while holding down the mouse button. The action of releasing the mouse button after the dragging is called Dropping.
The drag and drop properties, events and method are supported in VB are:
DragMode  property enables automatic or manual dragging of a control.
DragIcon property specifies the icon that is displayed when the control is dragged.
DragDrop event is recognized when a control is dropped onto the object.
DragOver event is recognized when a control is dragged over the object.
Drag method starts or stops manual dragging.
All controls except Menus, Timers, Lines, Shapes support the above mentioned properties and methods.
Forms recognize the DragDrop and DragOver events but they do not support the DragMethod and DragMode, DragIcon properties.
The following application illustrates how the DragDrop() and DragOver() procedure is executed:
Ex:
Open a new standard EXE project and the Form.
Place an Image control on the Form.
Set its DragMode property to 1-Automatic, add any Picture u want to display, and set the Stretch Property to True.
A textbox and a CommandButton is added to the Form as shown below.

         
Enter the following code in the Form_DragDrop() procedure
Private Sub Form_DragDrop(Source As Control, X As Single, Y As Single)
   Text1.Text = "   "
   Source.Move  X, Y
End Sub
Enter the following code in the Form_DragOver() procedure
Private Sub Form_DragOver(Source As Control, X As Single, Y As Single, State As Integer)
                  Dim info As String
info = "now dragging state is"
info = info + Source.Tag
Text1.Text = info
End Sub
Enter the following code in the Command1_DragOver() procedure
Private Sub Command1_DragOver(Source As Control, X As Single, Y As Single, State As                   Integer)
 Dim info As String
info = "now dragging state over Exit button is "
info = info + Source.Tag
info = info + Str(State)
Text1.Text = info
End Sub
Enter the following code in the Command1_Click() procedure
Private Sub Command1_Click()
           End
End Sub
Now execute the program by pressing F5.
When the Image is dragged over the Form it displays a message in the TextBox. Similarly, a message is displayed when it is moved over the Exit CommandButton which is shown below and when the user clicks the Exit  CommandButton the program is ended and exits  from the output screen.
                         
Dialog Boxes
Dialog Boxes are used to display information and to prompt the user about the data needed to continue an application.
Types of Dialog Box:
Dialog Boxes are either Modal or Modeless.
Modal :
A Modal  Dialog Box must be closed or hidden before you can continue working with the rest of the application. It does not allow the user to continue with other application unless it is closed or unloaded. MessageBox is a modal form that you can use.
Modeless:
Modeless forms let you shift the focus between the form and another form without having to close the initial form. The user can continue to work elsewhere in any application while the form is displayed.
Classification of Dialog Boxes:
There are three ways of adding dialog boxes to an application, which are :
Predefined Dialog Box :
Created using MsgBox() and InputBox() function.
Custom-Dialog Box :
Created by adding controls to the form or by customizing an existing dialog box.
Standard Dialog Box :
Created using Common Dialog control.

1. Predefined Dialog Box :
These dialog boxes are always Modal dialog Boxes.It uses the MsgBox() and InputBox() functions.
Message Box:
It is a dialog box that displays text and used to create error messages. It returns a positive integer whose value depends on the action taken by the user.
1. Simple Message Box :
Used to display only messages.
                  Ex:
                        The statement
                   MsgBox "Hello there!"
                        causes the following box to be displayed:
This is the simplest use of MsgBox.It uses only the required prompt argument. Since the buttons argument was omitted, the default (OK button with no icons) was used; and since the title argument was omitted, the default title (the project name) was displayed in the title bar.
Syntax :

MsgBox  “ text that u want to be displayed “

The following application illustrates how the message is displayed using a simple MessageBox function i.e. msgbox() :
Ex:
Open a new standard EXE project and the Form.
Place aCommandButton control on the Form and change the caption as CLICK ME as shown below.

                   

Enter the following code in the Command1_Click() procedure
Private Sub Command1_Click()
    MsgBox "Welcome to Visual Basic "
End Sub
Now execute the program by pressing F5.
Whenever the commandbutton is clicked a message box is displayed with the message “welcome to the Visual Basic”as shown below.
                                                 

2. Syntax:

              MsgBox(prompt[, buttons] [, title] [, helpfile, context])

The MsgBox function syntax has these parts:

Part Description
prompt Required. String expression displayed as the message in the dialog box. The maximum length of prompt is approximately 1024 characters, depending on the width of the characters used.
buttons Optional.
               Numeric expression that is the sum of values specifying the number and type of buttons to display, the icon style to use, the identity of the default button, and the modality of the message box.
                If omitted, the default value for buttons is 0 (which causes only an OK button to be displayed with no icon). The buttons argument is explained in more detail below.
title Optional.
               String expression displayed in the title bar of the dialog box. If you omit title, the application name is placed in the title bar.
helpfile
and
context Both optional.
              These arguments are only applicable when a Help file has been set up to work with the application.

Determines which buttons to display :
Constant Value Description
vbOKOnly 0 Display OK button only.
vbOKCancel 1 Display OK and Cancel buttons.
vbAbortRetryIgnore 2 Display Abort, Retry, and Ignore buttons.
vbYesNoCancel 3 Display Yes, No, and Cancel buttons.
vbYesNo 4 Display Yes and No buttons.
vbRetryCancel 5 Display Retry and Cancel buttons.

Determines which icon to display:
Constant Value Description Icon
vbCritical 16 Display Critical Message icon.
vbQuestion 32 Display Warning Query (question mark) icon.
vbExclamation 48 Display Warning Message icon.
vbInformation 64 Display Information Message icon.
The following application illustrates how the message is displayed using a different options using MessageBox function i.e. MsgBox(prompt[, buttons] [, title] [, helpfile, context]):
Ex:
Open a new standard EXE project and the Form.
Place a CommandButton control on the Form and change the caption as CLICK ME as shown below.
                   
Enter the following code in the Command1_Click() procedure
Private Sub Command1_Click()
     MsgBox "Please try again", vbCritical, "Welcome to Viasual Basic "
End Sub
Now execute the program by pressing F5.
Whenever the commandbutton is clicked a message box is displayed as       “please try again” with a critical message icon and the title as “welcome to visual basic” which is shown below.
                   

Input box :
Displays a prompt in a dialog box, waits for the user to input text or click a button, and returns a String containing the contents of the text box.
Following is an expanded InputBox

Syntax :
memory_variable = InputBox (prompt[,title][,default])
memory_variable is a variant data type but typically it is declared as string, which accept the message input by the users. The arguments are explained as follows:
Prompt - String expression displayed as the message in the dialog box. If prompt consists of more than one line, you can separate the lines using the vbCrLf constant
Title - String expression displayed in the title bar of the dialog box. If you omit the title, the application name is displayed in the title bar
default-text - The default text that appears in the input field where users can use it as his intended input or he may change to the message he wish to key in.
x-position and y-position - the position or the coordinate of the input box.
Following example demonstrates the use of InputBox function

* Open a new project and save the Form as InputBox.frm and save the Project as InputBox.vbp
* Design the application as shown below.
Object Property Setting
Form
  Caption
Name InputBox test
frmInputBox
Label
  Caption
Name You entered
lbl1
Label
  Caption
Name
BorderStyle ( empty)
lbl2
1-Fixed Single
CommandButton
  Caption
Name OK
cmdOK

Following code is entered in cmdOK_Click ( ) event
Private Sub cmdok_Click()
Dim ans As String
ans = InputBox("Enter something to be displayed in the label", "Testing", 0)
If ans = "" Then
lbl2.Caption = "No message"
Else
lbl2.Caption = ans
End If
End Sub
Save and run the application. As soon as you click the OK button you will get the following InputBox

Here I have entered "Hello World" in text field. As soon as you click OK the output is shown as shown below

2. Custom Dialog Boxes :
A custom dialog box is a form you create containing controls — including command buttons, option buttons, and text boxes — that lets the user supply information to the application. You customize the appearance of the form by setting property values. You also write code to display the dialog box at run time.
To create a custom dialog box, you can start with a new form or customize an existing dialog box. Over time, you can build up a collection of dialog boxes that can be used in many applications.
To customize an existing dialog box
1. From the Project menu, choose Add Form to add an existing form to your project.
2. From the File menu, choose Save filename As and enter a new file name. (This prevents you from making changes to the existing version of the form).
3. Customize the appearance of the form as needed.
4. Customize event procedures in the Code window.
To create a new dialog box
1. From the Project menu, choose Add Form.
–or–
Click the Form button on the toolbar to create a new form.
2. Customize the appearance of the form as needed.
3. Customize event procedures in the Code window.
You have considerable freedom to define the appearance of a custom dialog box. It can be fixed or movable, modal or modeless. It can contain different types of controls; however, dialog boxes do not usually include menu bars, window scroll bars, Minimize and Maximize buttons, status bars, or sizable borders. The remainder of this topic discusses ways to create typical dialog box styles.
Adding a Title
A dialog box should always have a title that identifies it. To create a title, set the form's Caption property to the text string that will appear in the title bar. Usually, this is done at design time using the Properties window, but you can also do this from code.
frmAbout.Caption = "About"
Displaying a Custom Dialog Box
You display a dialog box in the same way you display any other form in an application. The startup form loads automatically when the application is run. When you want a second form or dialog box to appear in the application, you write code to load and display it. Similarly, when you want the form or dialog box to disappear, you write code to unload or hide it.
Display Options
The code you write determines how a dialog box is loaded into memory and displayed. The following table describes various form displaying tasks and the keywords that are used to perform them.
Task Keyword
Load a form into memory, but do not display it. Use the Load statement, or reference a property or control on the form.
Load and display a modeless form. Use the Show method.
Load and display a modal form. Use the Show method with style = vbModal.
Display a loaded form. Set its Visible property to True, or use the Show method.
Hide a form from view. Set its Visible property to False, or use the Hide method.
Hide a form from view and unload from memory. Use the Unload statement.
Ex:
1. From the Project menu, choose Add Form.
2. Click the Form button on the toolbar to create a new form2 and form3 as shown below.

3. Enter the following code in the code editor window as shown below.

4. Now execute the program by pressing F5.
5. The output will be displayed as follows.
           
6. Unless and until we close the Form3 then only we can select Form2 as Form3 is Modal  DialogBox.
3.Common dialog boxes :
In Visual Basic you can use Dialog boxes for common events such as selecting a colour or a font. This saves you from having to create your own Dialog boxes for these events.
There are three steps to using Common Dialog boxes in Visual Basic. These are:
1. As the Common Dialog tool is not automatically part of the toolbox in the IDE, you will need to add it to the toolbox.


2. Place the Common Dialog Control on the form.
3. Decide which dialog box you want to use, then add code to the relevant Click event to display this box and use the options selected by the user.
<using the common dialog control>
To use the Common Dialog Control, you need to place it on your form in the same way as you would place other controls on your form - by double-clicking on the control's tool in the toolbox.
You do not need to worry about the size of the control or its position on the form, as it will not be visible when your project is executing. You only need to have one common dialog control on your form, regardless of how many different types of dialog boxes you want to use.
Your form will look like this when the Common Dialog Control has been added to it:

<coding for dialog box methods and properties>
To specify which dialog box you want to display, you need to use the Show method. The Show method has the following general form:
Object.ShowMethod
Show Method can be any one of the following:
Method Dialog box
ShowOpen Open
ShowSave Save As
ShowColor Color
ShowFont Font
ShowPrint Print

Private Sub mnucolor_Click()
'display the Color dialog box
CommonDialog1.ShowColor
End Sub

Private Sub mnuopen_Click()
'display the Open dialog box
CommonDialog1.ShowOpen
End Sub
When we run this form, click on the color menu which was created by menu editor ,then  the Color dialog box will be displayed as shown below.                              
Post a Comment

UNIT-II Handling controls


3.2.1 The Text Box  
The text box is the standard control for accepting input from the user as well as to display the output. It can handle string (text) and numeric data but not images or pictures. String in a text box can be converted to a numeric data by using the function Val(text). The following example illustrates a simple program that processes the input from the user.
Example 3.1
In this program, two text boxes are inserted into the form together with a few labels. The two text boxes are used to accept inputs from the user and one of the labels will be used to display the sum of two numbers that are entered into the two text boxes. Besides, a command button is also programmed to calculate the sum of the two numbers using the plus operator. The program use creates a variable sum to accept the summation of values from text box 1 and text box 2.The procedure to calculate and to display the output on the label is shown below. The output is shown in Figure 3.2
Private Sub Command1_Click()
‘To add the values in text box 1 and text box 2
Sum = Val(Text1.Text) + Val(Text2.Text)
‘To display the answer on label 1
Label1.Caption = Sum
End Sub
Figure 3.2

3.2.2 The Label  
The label is a very useful control for Visual Basic, as it is not only used to provide instructions and guides to the users, it can also be used to display outputs. One of its most important properties is Caption. Using the syntax label.Caption, it can display text and numeric data . You can change its caption in the properties window and also at runtime.  Please refer to Example 3.1 and Figure 3.1 for the usage of label.
 3.2.3 The Command Button
The command button is one of the most important controls as it is used to execute commands. It displays an illusion that the button is pressed when the user click on it. The most common event associated with the command button is the Click event, and the syntax for the procedure is
Private Sub Command1_Click ()
Statements
End Sub
3.2.4 The Picture Box
The Picture Box is one of the controls that is used to handle graphics. You can load a picture at design phase by clicking on the picture item in the properties window and select the picture from the selected folder. You can also load the picture at runtime using the LoadPicture method. For example, the statement will load the picture grape.gif into the picture box.
Picture1.Picture=LoadPicture ("C:\VB program\Images\grape.gif")
You will learn more about the picture box in future lessons. The image in the picture box is not resizable.

 3.2.5 The Image Box
The Image Box is another control that handles images and pictures. It functions almost identically to the picture box. However, there is one major difference, the image in an Image Box is stretchable, which means it can be resized. This feature is not available in the Picture Box. Similar to the Picture Box, it can also use the LoadPicture method to load the picture. For example, the statement loads the picture grape.gif into the image box.
Image1.Picture=LoadPicture ("C:\VB program\Images\grape.gif")
 3.2.6 The List Box
The function of the List Box is to present a list of items where the user can click and select the items from the list. In order to add items to the list, we can use the AddItem method. For example, if you wish to add a number of items to list box 1, you can key in the following statements
Example 3.2

Private Sub Form_Load ( )

List1.AddItem “Lesson1”
List1.AddItem “Lesson2”
List1.AddItem “Lesson3”
List1.AddItem “Lesson4”

End Sub
The items in the list box can be identified by the ListIndex property, the value of the ListIndex for the first item is 0, the second item has a ListIndex 1, and the second item has a ListIndex 2 and so on
3.2.7 The Combo Box
The function of the Combo Box is also to present a list of items where the user can click and select the items from the list. However, the user needs to click on the small arrowhead on the right of the combo box to see the items which are presented in a drop-down list. In order to add items to the list, you can also use the AddItem method. For example, if you wish to add a number of items to Combo box 1, you can key in the following statements
Example 3.3
Private Sub Form_Load ( )

Combo1.AddItem “Item1”
Combo1.AddItem “Item2”
Combo1.AddItem “Item3”
Combo1.AddItem “Item4”

End Sub

3.2.8 The Check Box
The Check Box control lets the user  selects or unselects an option. When the Check Box is checked, its value is set to 1 and when it is unchecked, the value is set to 0.  You can include the statements Check1.Value=1 to mark the Check Box and Check1.Value=0 to unmark the Check Box, as well as  use them to initiate certain actions. For example, the program will change the background color of the form to red when the check box is unchecked and it will change to blue when the check box is checked.  You will learn about the conditional statement If….Then….Elesif in later lesson. VbRed and vbBlue are color constants and BackColor is the background color property of the form.
Example 3.4
Private Sub Command1_Click()
If Check1.Value = 1 And Check2.Value = 0 Then
MsgBox "Apple is selected"
ElseIf Check2.Value = 1 And Check1.Value = 0 Then
MsgBox "Orange is selected"
Else
MsgBox "All are selected"
End If
End Sub

3.2.9 The Option Box
 The Option Box control also lets the user selects one of the choices. However, two or more Option Boxes must work together because as one of the Option Boxes is selected, the other Option Boxes will be unselected. In fact, only one Option Box can be selected at one time. When an option box is selected, its value is set to “True” and when it is unselected; its value is set to “False”. In the following example, the shape control is placed in the form together with six Option Boxes. When the user clicks on different option boxes, different shapes will appear. The values of the shape control are 0, 1, and 2,3,4,5 which will make it appear as a rectangle, a square, an oval shape, a rounded rectangle and a rounded square respectively.
Example 3.5
Private Sub Option1_Click ( )
Shape1.Shape = 0
End Sub

Private Sub Option2_Click()
Shape1.Shape = 1
End Sub

Private Sub Option3_Click()
Shape1.Shape = 2
End Sub

Private Sub Option4_Click()
Shape1.Shape = 3
End Sub

Private Sub Option5_Click()
Shape1.Shape = 4
End Sub

Private Sub Option6_Click()
Shape1.Shape = 5
End Sub

 3.2.10 The Drive List Box
The Drive ListBox is for displaying a list of drives available in your computer. When you place this control into the form and run the program, you will be able to select different drives from your computer as shown in Figure 3.3

Figure 3.3 The Drive List Box

 3.2.11 The Directory List Box
The Directory List Box is for displaying the list of directories or folders in a selected drive. When you place this control into the form and run the program, you will be able to select different directories from a selected drive in your computer as shown in Figure 3.4
Figure 3.4 The Directory List Box

 The File List Box
          The File List Box is for displaying the list of files in a selected directory or folder. When you place this control into the form and run the program, you will be able to shown the list of files in a selected directory as shown in figure
                               

Shape :
  The Shape control provides an easy way to draw rectangles, circles, and other shapes on a form at design time.                  
                                                 
Line :
             The Line control provides an easy way to draw lines on a form at design time

                                                   
     Shape and Line controls are useful for drawing graphical elements on the surface of a form.        
Data :
     Data control is used as a mechanism for binding controls to a database .  
                                     
                                                   
OLE (Object Linking and Embedding) :
OLE (Object Linking and Embedding) is a means to interchange data between applications. In object embedding, an object is embedded in the client application. Along with the object, client application also stores the information regarding source application (or server) that created the object. The data stored in client application is separate and no link is maintained between the data supplied by source application and data stored in client application.
            The advantage with Object Embedding is, client application maintains its own copy of the data.

                                                 



Control Arrays :
Control Array (mostly used in Visual Basic) is the collection of controls that programmer put it in form (VB Form). Control array is always a single dimensional array. Control array is that you can add or delete array elements at run-time. With some controls, it is very useful to define control arrays - it depends on the application.
Control arrays are a convenient way to handle groups of controls that perform a similar function. All of the events available to the single control are still available to the array of controls, the only difference -being an argument indicating the index of the selected array, element is passed to the event. Hence, instead of writing individual procedures for each control (i.e. not using control arrays), you only have to write one procedure for each array.
The VisualBasic objects that can be interacted with by a user are referred to as Control Objects and are normally placed onto the form by the programmer. It is often useful to have an array of such control objects, and they can be created in the following ways.
1. Create the first object and name it and set its properties to the required values and set the Index to 0. Copy and paste this object and each time the new object becomes the next element in the array automatically setting the Index to 1, 2, 3 etc. Thereafter the array element can be addressed as Name(0), Name(1) .... Name(I) etc.
2. Alternatively if you create the first object and name it then create a second object and give it the same name VisualBasic will then ask if you wish to create a "Control Array". Each time the new object is given the same name it will be created as the next element of the array. Thereafter the array element can be addressed as Name(0), Name(1) .... Name(I) etc.
3. If you need a large array it is more useful to create the array computationally. To do this you must create the first control object and set its Index to the lowest value, usually 0. Then within an appropriate subroutine, usually the Form_Load( ), write a For loop to generate the rest of the elements of the array. Code will be needed to place each object at an appropriate location on the Form otherwise they will appear on top of each other. In addition it is necessary to set each object to be visible. Thereafter the array element can be addressed as Name(0), Name(1) .... Name(I) etc.
4. To enable the program to determine which button has been clicked an event driven subroutine is automatically setup with the Index as a parameter. Thus code within that subroutine can access the Index value as indicated in the example below.

  Private Sub Button_Click(Index As
Integer)
Text1.Text = Index
End Sub
       
________________________________________

Private Sub Form_Load()
Dim i As Integer
Button(0).Caption = 0
For i = 1 To 5
Load Button(i)
Button(i).Top = Button(i - 1).Top + 1.2 *Button(0).Height
Button(i).Caption = i
Button(i).Visible = True
Next i


  The Form created by the programmer is shown above and when the program is running it is shown on the left. The five additional button are created at run time and located below the previous one using the Top property of the object. The text box displays the Index value (3) indicating that Button (3) has been clicked.
Creation of pop-up menu
It is created via the menu editor in the same manner as a drop-down menu except that the main menu item is not visible. An event procedure must be entered into the code editor. The general form of the event procedure is
Private sub form_mouseDown(Button as Integer, Shift as Inter, X as Single,                              Y as Single )
                  If button=vbrightbutton then
                            Popmenu  ………
                  End if
            End sub
Ex:
                                   
Private Sub Command1_Click()
          End
End Sub
Private Sub Form_MouseDown(Button As Integer, Shift As Integer, x As Single, Y       As Single)
       If Button = vbRightButton Then
                 PopupMenu mnucolor
      End If
End Sub
Private Sub mnuclear_Click()
          Shape1.FillColor = vbclear
End Sub
Private Sub mnugreen_Click()
         Shape1.FillColor = vbGreen
End Sub
Private Sub mnublue_Click()
           Shape1.FillColor = vbBlue
End Sub
Private Sub mnured_Click()
            Shape1.FillColor = vbRed
End sub
Mouse Events

Visual Basic responds to various mouse events, which are recognized by most of the controls.
The main events are
MouseDown,
MouseUp
MouseMove.

MouseDown :
Mouse Down occurs when the user presses any mouse button
 MouseUp :
Mouse Up occurs when the user releases any mouse button.
MouseMove :
Mouse Move occurs when the user moves mouse pointer.

These events use the arguments
Button,
Shift,
X, Y and they contain information about the mouse's condition when the button is clicked.

Button:
  The first argument is an integer called Button. The value of the argument indicates whether the left, right or middle mouse button was clicked.
Shift:
   The second argument in an integer called shift. The value of this argumnet indicates whether the mouse button was clicked simultaneously with the Shift key, Ctrl key or Alt key.
X and Y (X,Y) :
       The third and fourth arguments X and Y are the coordinates of the mouse location at the time the mouse button was clicked. As the Form_MouseDown( ) is executed automatically whenever the mouse button is clicked inside the Form's area the X, Y co-ordinates are referenced to the form.
Positioning a control:
                       MouseDown is the commonly used event and it is combined with the move method to move an Image control to different locations in a Form.

The following example illustrates the movement of objects responding to mouse events
Ex:
Open a new standard EXE project and the Form.
Add two OptionButton controls, two Images and a CommandButton as shown below.

                       

Enter the following code in the Form_MouseDown() procedure
Private Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
If Option1 = True Then
Image1.Move X, Y
Else
Image2.Move X, Y
End If
End Sub
Now execute the program by pressing F5.
The application is designed in such a way that when an OptionButton is selected, the corresponding Image control is placed anywhere in the Form whenever it is clicked which is shown below.

                   

Graphical Mouse Application :
Mouse events can be combined with graphics methods and any number of customized drawing or painting applications can be created.
The following example illustrates a drawing using both the MouseMove and MouseDown events.
Ex:
Open a new standard EXE project and the Form.
Enter the following code in the Form_MouseDown() procedure
Private Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
Form2.CurrentX = X
Form2.CurrentY = Y
End Sub
Enter the following code in the Form_MouseMove() procedure
Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
If Button = 1 Then
    Line (Form2.CurrentX, Form2.CurrentY)  -  (X, Y)
End If
End Sub
Now execute the program by pressing F5.
When the mouse button is clicked and moved in the Form a line is drawn corresponding to the mouse movement which is shown below.

                     




MouseMove Application :
MouceMove event occurs when the user moves the mouse pointer. These events uses three arguments i.e.  
Button : The first argument is an integer called Button. The value         indicates the left, right or middle mouse button  when clicked.
Shift : The value of this argument indicates whether the mouse button was clicked simultaneously.
X,Y : These are the coordinates of the mouse location at the time the mouse button was clicked0.
The following application illustrates how often the Form_MouseMove() procedure is executed :
Ex:
Open a new standard EXE project and the Form and place a CommandButton on the Form. Change the caption of the CommandButton as CLEAR.

                 
Enter the following code in the Form_MouseMove() procedure
Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
              Circle (X, Y), 80
End Sub
Enter the following code in the Command1_Click () procedure
Private Sub Command1_Click()
Form2.Cls
End Sub
Now execute the program by pressing F5.
When the mouse button is moved inside the Form circles are drawn along the path of the mouse movement which is shown below and when the user clicks the clear button it clears off the circles from the screen.
                                 
Dragging and Dropping :
Dragging is the process of clicking the mouse button in a control and moving the mouse while holding down the mouse button. The action of releasing the mouse button after the dragging is called Dropping.
The drag and drop properties, events and method are supported in VB are:
DragMode  property enables automatic or manual dragging of a control.
DragIcon property specifies the icon that is displayed when the control is dragged.
DragDrop event is recognized when a control is dropped onto the object.
DragOver event is recognized when a control is dragged over the object.
Drag method starts or stops manual dragging.
All controls except Menus, Timers, Lines, Shapes support the above mentioned properties and methods.
Forms recognize the DragDrop and DragOver events but they do not support the DragMethod and DragMode, DragIcon properties.
The following application illustrates how the DragDrop() and DragOver() procedure is executed:
Ex:
Open a new standard EXE project and the Form.
Place an Image control on the Form.
Set its DragMode property to 1-Automatic, add any Picture u want to display, and set the Stretch Property to True.
A textbox and a CommandButton is added to the Form as shown below.

         
Enter the following code in the Form_DragDrop() procedure
Private Sub Form_DragDrop(Source As Control, X As Single, Y As Single)
   Text1.Text = "   "
   Source.Move  X, Y
End Sub
Enter the following code in the Form_DragOver() procedure
Private Sub Form_DragOver(Source As Control, X As Single, Y As Single, State As Integer)
                  Dim info As String
info = "now dragging state is"
info = info + Source.Tag
Text1.Text = info
End Sub
Enter the following code in the Command1_DragOver() procedure
Private Sub Command1_DragOver(Source As Control, X As Single, Y As Single, State As                   Integer)
 Dim info As String
info = "now dragging state over Exit button is "
info = info + Source.Tag
info = info + Str(State)
Text1.Text = info
End Sub
Enter the following code in the Command1_Click() procedure
Private Sub Command1_Click()
           End
End Sub
Now execute the program by pressing F5.
When the Image is dragged over the Form it displays a message in the TextBox. Similarly, a message is displayed when it is moved over the Exit CommandButton which is shown below and when the user clicks the Exit  CommandButton the program is ended and exits  from the output screen.
                         
Dialog Boxes
Dialog Boxes are used to display information and to prompt the user about the data needed to continue an application.
Types of Dialog Box:
Dialog Boxes are either Modal or Modeless.
Modal :
A Modal  Dialog Box must be closed or hidden before you can continue working with the rest of the application. It does not allow the user to continue with other application unless it is closed or unloaded. MessageBox is a modal form that you can use.
Modeless:
Modeless forms let you shift the focus between the form and another form without having to close the initial form. The user can continue to work elsewhere in any application while the form is displayed.
Classification of Dialog Boxes:
There are three ways of adding dialog boxes to an application, which are :
Predefined Dialog Box :
Created using MsgBox() and InputBox() function.
Custom-Dialog Box :
Created by adding controls to the form or by customizing an existing dialog box.
Standard Dialog Box :
Created using Common Dialog control.

1. Predefined Dialog Box :
These dialog boxes are always Modal dialog Boxes.It uses the MsgBox() and InputBox() functions.
Message Box:
It is a dialog box that displays text and used to create error messages. It returns a positive integer whose value depends on the action taken by the user.
1. Simple Message Box :
Used to display only messages.
                  Ex:
                        The statement
                   MsgBox "Hello there!"
                        causes the following box to be displayed:
This is the simplest use of MsgBox.It uses only the required prompt argument. Since the buttons argument was omitted, the default (OK button with no icons) was used; and since the title argument was omitted, the default title (the project name) was displayed in the title bar.
Syntax :

MsgBox  “ text that u want to be displayed “

The following application illustrates how the message is displayed using a simple MessageBox function i.e. msgbox() :
Ex:
Open a new standard EXE project and the Form.
Place aCommandButton control on the Form and change the caption as CLICK ME as shown below.

                   

Enter the following code in the Command1_Click() procedure
Private Sub Command1_Click()
    MsgBox "Welcome to Visual Basic "
End Sub
Now execute the program by pressing F5.
Whenever the commandbutton is clicked a message box is displayed with the message “welcome to the Visual Basic”as shown below.
                                                 

2. Syntax:

              MsgBox(prompt[, buttons] [, title] [, helpfile, context])

The MsgBox function syntax has these parts:

Part Description
prompt Required. String expression displayed as the message in the dialog box. The maximum length of prompt is approximately 1024 characters, depending on the width of the characters used.
buttons Optional.
               Numeric expression that is the sum of values specifying the number and type of buttons to display, the icon style to use, the identity of the default button, and the modality of the message box.
                If omitted, the default value for buttons is 0 (which causes only an OK button to be displayed with no icon). The buttons argument is explained in more detail below.
title Optional.
               String expression displayed in the title bar of the dialog box. If you omit title, the application name is placed in the title bar.
helpfile
and
context Both optional.
              These arguments are only applicable when a Help file has been set up to work with the application.

Determines which buttons to display :
Constant Value Description
vbOKOnly 0 Display OK button only.
vbOKCancel 1 Display OK and Cancel buttons.
vbAbortRetryIgnore 2 Display Abort, Retry, and Ignore buttons.
vbYesNoCancel 3 Display Yes, No, and Cancel buttons.
vbYesNo 4 Display Yes and No buttons.
vbRetryCancel 5 Display Retry and Cancel buttons.

Determines which icon to display:
Constant Value Description Icon
vbCritical 16 Display Critical Message icon.
vbQuestion 32 Display Warning Query (question mark) icon.
vbExclamation 48 Display Warning Message icon.
vbInformation 64 Display Information Message icon.
The following application illustrates how the message is displayed using a different options using MessageBox function i.e. MsgBox(prompt[, buttons] [, title] [, helpfile, context]):
Ex:
Open a new standard EXE project and the Form.
Place a CommandButton control on the Form and change the caption as CLICK ME as shown below.
                   
Enter the following code in the Command1_Click() procedure
Private Sub Command1_Click()
     MsgBox "Please try again", vbCritical, "Welcome to Viasual Basic "
End Sub
Now execute the program by pressing F5.
Whenever the commandbutton is clicked a message box is displayed as       “please try again” with a critical message icon and the title as “welcome to visual basic” which is shown below.
                   

Input box :
Displays a prompt in a dialog box, waits for the user to input text or click a button, and returns a String containing the contents of the text box.
Following is an expanded InputBox

Syntax :
memory_variable = InputBox (prompt[,title][,default])
memory_variable is a variant data type but typically it is declared as string, which accept the message input by the users. The arguments are explained as follows:
Prompt - String expression displayed as the message in the dialog box. If prompt consists of more than one line, you can separate the lines using the vbCrLf constant
Title - String expression displayed in the title bar of the dialog box. If you omit the title, the application name is displayed in the title bar
default-text - The default text that appears in the input field where users can use it as his intended input or he may change to the message he wish to key in.
x-position and y-position - the position or the coordinate of the input box.
Following example demonstrates the use of InputBox function

* Open a new project and save the Form as InputBox.frm and save the Project as InputBox.vbp
* Design the application as shown below.
Object Property Setting
Form
  Caption
Name InputBox test
frmInputBox
Label
  Caption
Name You entered
lbl1
Label
  Caption
Name
BorderStyle ( empty)
lbl2
1-Fixed Single
CommandButton
  Caption
Name OK
cmdOK

Following code is entered in cmdOK_Click ( ) event
Private Sub cmdok_Click()
Dim ans As String
ans = InputBox("Enter something to be displayed in the label", "Testing", 0)
If ans = "" Then
lbl2.Caption = "No message"
Else
lbl2.Caption = ans
End If
End Sub
Save and run the application. As soon as you click the OK button you will get the following InputBox

Here I have entered "Hello World" in text field. As soon as you click OK the output is shown as shown below

2. Custom Dialog Boxes :
A custom dialog box is a form you create containing controls — including command buttons, option buttons, and text boxes — that lets the user supply information to the application. You customize the appearance of the form by setting property values. You also write code to display the dialog box at run time.
To create a custom dialog box, you can start with a new form or customize an existing dialog box. Over time, you can build up a collection of dialog boxes that can be used in many applications.
To customize an existing dialog box
1. From the Project menu, choose Add Form to add an existing form to your project.
2. From the File menu, choose Save filename As and enter a new file name. (This prevents you from making changes to the existing version of the form).
3. Customize the appearance of the form as needed.
4. Customize event procedures in the Code window.
To create a new dialog box
1. From the Project menu, choose Add Form.
–or–
Click the Form button on the toolbar to create a new form.
2. Customize the appearance of the form as needed.
3. Customize event procedures in the Code window.
You have considerable freedom to define the appearance of a custom dialog box. It can be fixed or movable, modal or modeless. It can contain different types of controls; however, dialog boxes do not usually include menu bars, window scroll bars, Minimize and Maximize buttons, status bars, or sizable borders. The remainder of this topic discusses ways to create typical dialog box styles.
Adding a Title
A dialog box should always have a title that identifies it. To create a title, set the form's Caption property to the text string that will appear in the title bar. Usually, this is done at design time using the Properties window, but you can also do this from code.
frmAbout.Caption = "About"
Displaying a Custom Dialog Box
You display a dialog box in the same way you display any other form in an application. The startup form loads automatically when the application is run. When you want a second form or dialog box to appear in the application, you write code to load and display it. Similarly, when you want the form or dialog box to disappear, you write code to unload or hide it.
Display Options
The code you write determines how a dialog box is loaded into memory and displayed. The following table describes various form displaying tasks and the keywords that are used to perform them.
Task Keyword
Load a form into memory, but do not display it. Use the Load statement, or reference a property or control on the form.
Load and display a modeless form. Use the Show method.
Load and display a modal form. Use the Show method with style = vbModal.
Display a loaded form. Set its Visible property to True, or use the Show method.
Hide a form from view. Set its Visible property to False, or use the Hide method.
Hide a form from view and unload from memory. Use the Unload statement.
Ex:
1. From the Project menu, choose Add Form.
2. Click the Form button on the toolbar to create a new form2 and form3 as shown below.

3. Enter the following code in the code editor window as shown below.

4. Now execute the program by pressing F5.
5. The output will be displayed as follows.
           
6. Unless and until we close the Form3 then only we can select Form2 as Form3 is Modal  DialogBox.
3.Common dialog boxes :
In Visual Basic you can use Dialog boxes for common events such as selecting a colour or a font. This saves you from having to create your own Dialog boxes for these events.
There are three steps to using Common Dialog boxes in Visual Basic. These are:
1. As the Common Dialog tool is not automatically part of the toolbox in the IDE, you will need to add it to the toolbox.


2. Place the Common Dialog Control on the form.
3. Decide which dialog box you want to use, then add code to the relevant Click event to display this box and use the options selected by the user.
<using the common dialog control>
To use the Common Dialog Control, you need to place it on your form in the same way as you would place other controls on your form - by double-clicking on the control's tool in the toolbox.
You do not need to worry about the size of the control or its position on the form, as it will not be visible when your project is executing. You only need to have one common dialog control on your form, regardless of how many different types of dialog boxes you want to use.
Your form will look like this when the Common Dialog Control has been added to it:

<coding for dialog box methods and properties>
To specify which dialog box you want to display, you need to use the Show method. The Show method has the following general form:
Object.ShowMethod
Show Method can be any one of the following:
Method Dialog box
ShowOpen Open
ShowSave Save As
ShowColor Color
ShowFont Font
ShowPrint Print

Private Sub mnucolor_Click()
'display the Color dialog box
CommonDialog1.ShowColor
End Sub

Private Sub mnuopen_Click()
'display the Open dialog box
CommonDialog1.ShowOpen
End Sub
When we run this form, click on the color menu which was created by menu editor ,then  the Color dialog box will be displayed as shown below.                              
Post a Comment