Search In Site

24 June, 2013

Managing Forms In Visual Basic

Managing Forms In Visual Basic

If you need an immediate solution to:
Setting Title Bar Text
Adding/Removing Min/Max Buttons And Setting A Windows Border
Adding Toolbars To Forms
Adding Status Bars To Forms
Referring To The Current Form
Redrawing Form Contents
Setting Control Tab Order
Moving And Sizing Controls From Code
Showing And Hiding Controls In A Form
Measurements In Forms
Working With Multiple Forms

Loading, Showing, And Hiding Forms
Setting The Startup Form
Creating Forms In Code
Using The Multiple Document Interface
Arranging MDI Child Windows
Opening New MDI Child Windows
Arrays Of Forms
Coordinating Data Between MDI Child Forms (Document Views)
Creating Dialog Boxes
All About Message Boxes And Input Boxes
Passing Forms To Procedures
Minimizing/Maximizing And Enabling/Disabling Forms From Code


The Parts Of A Form
Forms are the names for windows in Visual Basic (originally, you called windows under design forms, and the actual result when running a window, but common usage has named both forms now), and you add controls to forms in the Integrated Development Environment (IDE). At the top of the form is the title bar, which displays the forms title; here thats just Form1. At right in the title bar is the control box, including the minimizing/maximizing buttons and the close button. These are controls the user takes for granted in most windows, although well see they are inappropriate in others (such as dialog boxes).


A form under design.
Under the title bar comes the menu bar, if there is one.the form has one menu: the File menu. Under the menu bar, forms can have toolbars, as you see in the IDE itself. The main area of a formthe area where everything takes placeis called the client area. In general, Visual Basic code works with controls in the client area and leaves the rest of the form to Visual Basic (in fact, the client area is itself a window).

Finally, the whole form is surrounded by a border, and there are several types of borders that you can use.

An MDI form.

You can see that an MDI form looks much like a standard form, with one major
difference, of coursethe client area of an MDI form acts like a kind of corral for
other forms. That is, an MDI form can display MDI child forms in it, which is how
the multiple document interface works.

There three types of forms available to us in Visual Basic: standard forms, MDI forms, and MDI child forms.

Setting Title Bar Text

This stymies a lot of Visual Basic programmers, because the text in the title bar
seems like something that Windows itself manages, not the program. In fact, its up
to the program, and setting the text in the title bar couldnt be easier. At design time,
you just change the forms Caption property,You can also set the Caption property at runtime in code like this (note that we use the Me keyword here to refer to the current formsee Referring to the Current Form  later in this  chapter):
Private Sub Command1_Click()
Me.Caption = "Hello from Visual Basic!"
End Sub


Setting A Forms Border
You set a forms border style with its BorderStyle property; here are the possible values for that property:
" 0None
" 1Fixed Single
" 2Sizable
" 3Fixed Dialog
" 4Fixed Tool window
" 5Sizable Tool window


Adding Toolbars To Forms
For some reason, adding toolbars to forms isnt covered in a lot of Visual Basic books. However, users have come to expect toolbars in more complex  programs, and well see how to add them here. Toolbars provide buttons that correspond to menu items and give the user an easy way to select the commands those items correspond to. 

Adding A Toolbar With The Application Wizard
The easiest way to design a toolbar and add it to a program is with the Application Wizard. When you create a new application using the Application Wizard, it lets you design the toolbar


Designing a toolbar with the Application Wizard.
This is a great way to put a toolbar in a program, because the support is already there for all these buttons by default. When you create the program, heres how it handles the buttons in the toolbar, with a Select Case statement that looks at the buttons Key value:
Private Sub tbToolBar_ButtonClick(ByVal Button As ComctlLib.Button)
On Error Resume Next
Select Case Button.Key
Case "New"
LoadNewDoc
Case "Open"
mnuFileOpen_Click
Case "Save"
mnuFileSave_Click
Case "Print"
mnuFilePrint_Click
Case "Copy"
mnuEditCopy_Click
Case "Cut"
mnuEditCut_Click
Case "Paste"
mnuEditPaste_Click
Case "Bold"
ActiveForm.rtfText.SelBold = Not ActiveForm.rtfText.SelBold
Button.Value = IIf(ActiveForm.rtfText.SelBold, tbrPressed,_
Visual Basic 6 Black Book:Managing Forms In Visual Basic
http://24.19.55.56:8080/temp/ch04\123-126.html (1 of 3) [3/14/2001 1:29:20 AM]
tbrUnpressed)
Case "Italic"
ActiveForm.rtfText.SelItalic = Not ActiveForm.rtfText._
SelItalic
Button.Value = IIf(ActiveForm.rtfText.SelItalic, tbrPressed,_
tbrUnpressed)
Case "Underline"
ActiveForm.rtfText.SelUnderline = Not _
ActiveForm.rtfText.SelUnderline
Button.Value = IIf(ActiveForm.rtfText.SelUnderline,_
tbrPressed,tbrUnpressed)
Case "Align Left"
ActiveForm.rtfText.SelAlignment = rtfLeft
Case "Align Right"
ActiveForm.rtfText.SelAlignment = rtfRight
Case "Center"
ActiveForm.rtfText.SelAlignment = rtfCenter
End Select
End Sub


Adding A Toolbar To A Program Yourself
You can also add toolbars to already-existing programs; just follow these steps:
1. Use the Project[vbar]Components item to open the Components box, and select the Controls tab.
2. Click the Microsoft Windows Common Controls box, and click on OK to close the Components box.
3. Double-click the New Toolbar tool in the toolbox to add a new toolbar to your form now.
4. Right-click the toolbar now, and select the Properties item in the pop-up menu that appears, opening the
buttons property page.
5. Click the Buttons tab in the property page now, and click Insert Button to insert a new button into the
toolbar.
6. Give the new button the caption you want, and set its Key property to a string of text you want to refer to
the button with in code.
7. Add other buttons in the same way and close the property page.
8. Double-click a button in the toolbar now to open the code window, displaying Toolbar1_ButtonClick():

Private Sub Toolbar1_ButtonClick(ByVal Button As ComctlLib.Button)
...
End Sub
9. Add the code you want to Toolbar1_ButtonClick(). You do this with a Select Case statement, selecting
on the buttons Key property:
Private Sub Toolbar1_ButtonClick(ByVal Button As ComctlLib.Button)
Select Case Button.Key
Case "First"
MsgBox "You clicked the first button."
Case "Second"
MsgBox "You clicked the second button."
Case "Third"
MsgBox "You clicked the third button."
End Select
End Sub
And thats itnow weve added a toolbar to a program; when the user clicks a key in the toolbar, our program
will handle it.



Adding Status Bars To Forms
Youve finished your program, and its ready to go to marketbut suddenly the
project director calls and asks why theres so many message boxes popping up all the time. You explain that you have to give the user feedback on the file
downloading processafter all, downloading the 200MB initialization file from the Internet takes some time, and you like to update the user on the process every time a kilobyte of data has been read. 
What about using the status bar? the project director asks. Hmm, you thinkwhat about using the status bar? The easiest way to put a status bar in a form is to design your program with the Application Wizard. However, you can also add status bars to a program yourself with these steps:
1. Use the Project[vbar]Components item to open the Components box, and select the Controls tab.
2. Click the Microsoft Windows Common Controls box, and click on OK to close
the Components box.
3. Double-click the New Status Bar tool in the toolbox to add a new status bar to your form now.
4. Right-click the status bar, and select the Properties item in the pop-up menu that appears, opening the buttons property page,.
5. Status bars are organized into panels, and each panel can display separate text. To add the panels you want to the status bar, use the Insert Panel button. Close the property page.
6. Now you can set the text in the panels from code. You do that with the status bar's Panels collection. The first panel in the status bar is Panels(1), the second Panels(2), and so on. For example, to set the text in the first panel to Status: 
you would use this code:
Private Sub Command1_Click ()
StatusBar1.Panels(1).Text = "Status: OK"
End Sub


Referring To The Current Form
Youve written a terrific subroutine to change a forms color to red
Sub ColorWindow(FormToColor As Form)
FormToColor.BackColor = RGB(255, 0, 0)
End Sub
and you want to color all the forms in your project when the user clicks a button. Thats easy to do using the Me keyword, which refers to the current object. Here, for example, is how wed pass the current form to the  colorWindow() subroutine:
Private Sub Command1_Click()
ColorWindow Me
End Sub


Redrawing Form Contents
Youve written some code to draw an x across a form like this:
Private Sub Command1_Click()
Line (0, 0)-(ScaleWidth, ScaleHeight)
Line (0, ScaleHeight)-(ScaleWidth, 0)
End Sub
You try it out and it looks perfectbut then the boss walks past and you minimize your program for a second to go back to that word-processing program so you'll look busy. When you maximize the x program again, the x is gonewhat happened? One of the biggest headaches for Windows programmers is refreshing the window when required, because that involves redrawing the entire forms contents. To make matters worse, this is a common occurrence, because in Windows, the user is always covering and uncovering windows, minimizing and maximizing them, and changing their size, all of which means that your program has to keep redrawing itself. In C or C++ programs, you have to write all the redrawing code yourself; fortunately, there is an easy fix in Visual Basic (and thats one of the things that made Visual Basic so popular in the first place)you just use the AutoReDraw property. Youve probably already used the AutoReDraw property, but we include it here for reference.


Setting Control Tab Order
Another call from the Testing Department. Theyve been going over your program with a fine-tooth comb and asking about the keyboard interface.
What does that mean? you ask. They explain that theoretically, according to Microsoft, users should be able to run all Windows programs with the keyboard alone. But that was archaic years ago, you say.
Add it to your program, they say. In Visual Basic, you can make controls accessible to the keyboard by setting their tab order. The user can move around from control to control, highlighting the currently selected control, using the Tab key. But its up to you to set the order in which the focus moves from control to control, and even whether or not a control can be reached with the Tab key.
To set the tab order of the controls in your program, follow these steps:
1. Select a control whose tab order you want to set with the mouse.

2. Next, make sure the controls TabStop property is set to True. If this property is False, the user cannot reach the control using the Tab key.
3. Now set the controls position in the tab order by setting its TabIndex property.
The first control in the tab order has a TabIndex of 0, the next a TabIndex of 1,
and so on.
4. When you run the program, the first control is highlighted; when the user presses the Tab key, the focus moves to the second control in the tab order, when he presses Tab again, the focus moves to the third control, and so on.
That's all it takesnow youre giving your program a keyboard interface.


Moving And Sizing Controls From Code
Sometimes its necessary to move or resize the controls in a form as a program is running, but for some reason, many Visual Basic programmers think you can only do that at design time. In fact, you can do it at runtime easily. All controls have these properties available at design time or runtime to set their location and dimensions:
" TopThe y coordinate of the top left of the control.
" LeftThe x coordinate of the top left of the control.
" WidthThe width of the control.
" HeightThe height of the control.
You can change all these settings interactively to move or resize a control in a form. Note that all measurements are in twips (1/1440 of an inch) by default, and that the origin (0, 0) in a form is at upper left. You can also use a controls Move() method to move a control to a new location: object.Move left, [top, [width, [height]]] .Here's an examplein this case, when the user clicks a button, Command1, we double the buttons width and height, and move it 500 twips to the left:
Private Sub Command1_Click()
Const intIncrement = 500
Command1.Width = 2 * Command1.Width
Command1.Height = 2 * Command1.Height
Command1.Move (Command1.Left + intIncrement)
End Sub


Showing And Hiding Controls In A Form
The Testing Department is on the phone againdoes your program really need 120 buttons in the main form? After all, thats exactly what menus were designed for: to hide controls not needed, getting them out of the users way. (In fact, thats usually a good way to determine if a control item should be in a menu or on the main form: you use menus to make options available to the user at all times, while keeping them out of the way.) 
However, lets say you really dont want to put your control items into menusyou can still use buttons if you hide the ones that dont apply at a particular time, showing them when appropriate. Hiding and showing controls in a form as needed can produce dramatic effects at times. Showing and hiding controls is easy: just use the controls Visible property. Setting this property to True displays the control; setting it to False hides it. Heres an example where we make a button disappear (probably much to the users surprise) when the user clicks it:
Private Sub Command1_Click()
Command1.Visible = False
End Sub

Measurements In Forms
The default measurement units for forms are twips, but the project design board says they want the data-entry forms youre designing to look like real 3×5 cards on the screen. Can you convert from twips to inches in Visual Basic? Yes, you can, and well take a look at that and other measurement issues here.
You can get the dimensions of a forms client area with these properties:
" ScaleWidthThe width of the client area.
" ScaleHeightThe height of the client area.
" ScaleLeftThe horizontal coordinate of upper left of client area.
" ScaleTopThe vertical coordinate of upper left of client area.
And you can get the overall dimensions of the form using these properties:
" WidthThe width of the form.
" HeightThe height of the form.
" LeftThe horizontal coordinate of upper left of the form
" TopThe vertical coordinate of upper left of the form
You can also use the ScaleMode property to set a forms coordinate system units
you dont have to use twips. Here are the possible values for ScaleMode :
" 0User-defined
" 1Twips (1/1440ths of an inch)
" 2Points (1/72nds of an inch)
" 3Pixels
" 4Characters (120 twips horizontally, 240 twips vertically)
" 5Inches
" 6Millimeters
" 7Centimeters

User-Defined Coordinates
To make life easier for yourself, you can set up a user-defined coordinate system: just set the ScaleWidth and ScaleHeight properties yourself. For example, if you want to plot data on a 1000x1000 grid, just set ScaleWidth and ScaleHeight to 1000. To draw a scatter plot of your data, then, you could use PSet() to set individual pixels directly. If one of the points to graph was (233, 599), you could draw that dot this way: PSet(233, 599).

Working With Multiple Forms
Youve designed your program and its a beauty: an introductory form to welcome the user, a data-entry form to get data from the user, a summary form to display the data analysis results, a logon form to connect to the Internetits all there. Suddenly it occurs to youarent Visual Basic projects organized into modules and forms? How does the code in one form reach the code in  another that is, how can the code in the analysis module read what the user has entered in the data-entry form? Its time to take a look at working with multiple forms. A single form that lets the user display another form. When the user clicks the Show Form2 button, the program should display Form2
on the screenand place the text Welcome to Visual Basic in the text box in Form2


A multiform program.
Create a new Visual Basic project now. This project has one default form, Form1. To add another form, Form2, just select the Add Form item in the Project menu;
click on OK in the Add Form dialog box that appears to accept the default new
form. In addition, add a new text box, Text1, to the new form, Form2. In addition, add a command button to Form1 and give it the caption Show Form2
and open the code for that button now:
Private Sub Command1_Click ()
End Sub
When the user clicks the Show Form2 button, we will show Form2, which we do
with Form2s Show() method:
Private Sub Command1_Click()
Form2.Show
End Sub
Next, to place the text Welcome to Visual Basic in the text box, Text1, in Form2, we need to use that text boxs fully qualified name: Form2.Text1, indicating that the text box we want is in Form2. We can use that text boxs Text property this way to set the text in the box:
Private Sub Command1_Click()
Form2.Show
Form2.Text1.Text = "Hello from Visual Basic"
End Sub

That completes the code for the Show Form2 button. Form2 has a button labeled Hide Form, and we can implement that by hiding Form2 in that buttons event handler procedure:
Private Sub Command1_Click()
Hide
End Sub


Loading, Showing, And Hiding Forms
There are times when you might want to work with a form before displaying it on the screen to initialize it (with graphics and so on), in which case you can load the form into memory using the Load statement. To actually show the form on the screen, then, you use the Show() method. Heres an example in which we load a new form, Form2, and then show it:
Private Sub Command1_Click()
Load Form2
Form2.Show
End Sub

After displaying a form, you can hide it with the Hide() method and unload it
(although thats not necessary) with the Unload statement. You usually unload
forms if you have a lot of them and are concerned about memory usage. Heres an example in which we hide Form2 and then unload it:
Private Sub Command2_Click()
Form2.Hide
Unload Form2
End Sub


Creating Forms In Code
Youve added a handy calculator form to your financial planning programbut you find that many users have several calculations open at once and want to open multiple calculators. How do you create and display new forms like that in Visual Basic? New forms are simply new objects in Visual Basic. To declare a new form based on a form you already have, say Form1, you just use Dim :
Private Sub NewForm_Click()
Dim NewForm As Form1
End Sub
Next, you create the new form with the New keyword:
Private Sub NewForm_Click()
Dim NewForm As Form1
Set NewForm = New Form1
End Sub
Finally, you show the new form:
Private Sub NewForm_Click()
Dim NewForm As Form1
Set NewForm = New Form1
NewForm.Show
End Sub
Calling this subroutine will add as many new forms as you want to a program.
Note that we do not keep track of the new forms name (NewForm is a local
variable in NewForm_Click(), and you cant use it after returning from that
procedure); you might want to save the new forms in an array so you can close them under program control. Using the code, we create new forms
























0 comments:

Post a Comment

Dear Visitors All The Tricks And Hacks Posted Here Are Only For Knowledge Purpose.Don't Use These for Illegal Operations.

 
Twitter Bird Gadget