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
























Visual Basic Integrated Development Environment

The Visual Basic Integrated Development Environment
The IDE, shown in Figure 1.19, has become more complex than in previous versions of Visual Basic, and being able to use it, or at least knowing what the various parts are called, is a skill we'll need in the coming chapters. Part of the reasons it's become more complex is that the same IDE is now shared by all Visual Studio languages, such as VB and C# (something Microsoft has promised for many years, but only implemented now). We've already seen the IDE at work, of course, but now it's time to take a more systematic look.
                     Figure 1.19:
There are so many independent windows in the IDE that it's easy to misplace or rearrange them inadvertently. The IDE windows are docking windows, which means you can use the mouse to move windows around as you like; when the windows are near an edge, they'll "dock"-adhere-to that edge, so you can reconfigure the IDE windows as you like. If you move IDE windows inadvertently, don't panic; just use the mouse to move them back.

You also can restore the default window layout by selecting the Tools|Options item, then selecting the General item in the Environment folder, and clicking the Reset Window Layout button. That's really good to know, because sooner or later, Visual Basic will dock some window you didn't want to dock, such as the Edit|Replace window, to the IDE, rearranging all your other windows, and it can take a long time to try to fix that manually.

Also note that the windows in the IDE come with an X button at upper left, which means you can close them. I don't know about you, but I sometimes click these when I don't mean to, and a window I wanted disappears. It's easy to panic: The toolbox is gone! I'll have to reinstall everything! In fact, all you have to do is to find that window in the View menu again (such as View|Toolbox) to make it reappear. (Note that some windows are hidden in the View|Other Windows menu item, which opens a submenu of additional windows-there are simply too many windows to fit them all into one menu without needing to use a submenu.)

There's so much packed into the IDE that Microsoft has started to make windows share space, and you can keep them separate using tabs such as those you can see above the form at the center of Figure 1.19. If you click the Form1.vb[Design] tab, you see the form itself as it'll appear when the program runs; if you click the Form1.vb tab, you'll see the form's code, and if you click the Start Page tab, you'll seethe Start page, which lets you select from among recent solutions to open. Also note at lower right that the Properties window and the Dynamic Help window-a new VB .NET feature-are sharing the same space, and you can select between them using tabs.

The IDE is a very crowded place, and in an effort to unclutter the cluttered IDE a little, VB .NET adds a new button in dockable IDE windows-a little thumbtack button at upper right as you see in various windows in Figure 1.19, next to the X close button. This is the "auto-hide" feature, which lets you reduce a window to a tab connected to the edge it's docked on. For example, in Figure 1.19, the Server Explorer (which lets you explore data sources on servers) window is hidden and has become a tab at upper left in the IDE. If I let the mouse move over that tab, the full Sever Explorer window will glide open, covering most of the toolbox. You can auto-hide most windows like this; for example, if I were to click the thumbtack button in the toolbox, it would close and become a tab under the Server Explorer tab in the IDE. To restore a window to stay-open status, just click the thumbtack again.

And, of course, you can customize the IDE as well. For example, to customize IDE options such as the fonts and colors used to display code, you select the Tools|Options menu item and use the various items in the Environment folder. To customize menus and toolbars, such as specifying the toolbars to display (How many are there to choose from? Twenty-seven.), or what buttons go on what toolbars, use the Tools|Customize menu item.
That's it for general discussion-it's time to get to the IDE itself, starting with the Start page.

The Start Page
We've already seen the Start page, which is what you see when you first start Visual Basic, and which appears outlined in Figure 1.20. You can use the Start page to select from recent projects; by default, the Get Started item is selected in the Start page at upper left. You can also create a new project here by clicking the New Project button.
Figure 1.20: The Visual Basic IDE Start page.

The Start page has other useful aspects as well: for example, because you use the same IDE for all Visual Studio languages, it'll also search through all those languages when you search the help files. To make it search only pertinent help files, you can select the My Profile item in the Start page, and select either Visual Basic or Visual Basic and Related (which is my preference) in the Help Filter drop-down list box.

The Start page is actually being displayed in a browser. Its URL is vs:/default.htm, as you can see in a drop-down list box above the Start page. Entering a new URL in that drop-down list box and pressing Enter navigates to that new URL, replacing the Start page. And if you have an URL in your code (a quoted string that begins with "http://"), VB .NET will turn that text into a hyperlink, underline it, and allow you to click that URL to bring up the corresponding Web page in place of the Start page.

The Menu System
After you've started Visual Basic and have seen the Start page, you often turn to the menu system to proceed, as when you want to create a new project and use the File|New|Project menu item to bring up the New Project dialog box (you can do the same thing by clicking the New Project button in the Start page).

The IDE menu system is very involved, with many items to choose from-and you don't even see it all at once. The menu system changes as you make selections in the rest of the IDE-for example, the Project menu will display 16 items if you first select a project in the Solution Explorer, but only 4 items if you have selected a solution, not a project. In fact, there are even more dramatic changes; for example, try clicking a form under design and you'll see a Data menu in the menu bar, used to generate datasets. If you then select not the form but the form's code, however (for example, double-click the form to open the code window), the Data menu disappears.

There are hundreds of menu items here, and many useful ones that will quickly become favorites, such as File|New|Project that you use to create a new project, or the most recently used (MRU) list of files and projects that you can access from the Recent Files or Recent Projects items near the bottom of the File menu.

You can set the number of items that appear in MRU lists by selecting the Tools|Options menu item, clicking the Environment folder and selecting the General item, and entering a value in the "most recently used lists" text box.

The menu system also allows you to switch from debug to release modes if you use the Build|Configuration Manager item, lets you configure the IDE with the Tools|Options and Tools|Customize items, and so on. I'll introduce more and more menu items throughout the book as appropriate.

Toolbars
The toolbars feature is another handy aspect of the IDE. These appear near the top of the IDE, as shown in Figure 1.21. There are plenty of toolbars to choose from, and sometimes VB .NET will choose for you, as when it displays the Debug toolbar when you've launched a program with the Start item in the Debug menu.
Figure 1.21: Visual Basic IDE toolbars.

Because the IDE displays tool tips (those small yellow windows with explanatory text that appear when you let the mouse rest over controls such as buttons in a toolbar), it's easy to get to know what the buttons in the toolbars do. As mentioned, you can also customize the toolbars in the IDE, selecting which toolbars to display or customizing which buttons appear in which toolbars with the Tools|Customize menu item, or you can right-click a toolbar itself to get a menu of the possible toolbars to display (the bottom item in this popup menu is Customize, which lets you customize which buttons go where), or you can open the Toolbars submenu in the View menu to do the same thing (as is often the case in VB, there's more than one way to do it).

Toolbars provide a quick way to select menu items, and although I personally usually stick to using the menu system, there's no doubt that toolbar buttons can be quicker; for example, to save the file you're currently working on, you only need to click the diskette button in the standard toolbar (as you see in Figure 1.21), or the stacked diskettes button to save all the files in the solution.

The New Project Dialog Box
When you want to create a new project, you turn to the New Project dialog box. We've already used this quite a bit, and you can see it in Figure 1.22.
Figure 1.22: The New Project dialog box.

In addition to letting you select from all the possible types of projects you can create in Visual Basic, you can also set the name of the project, and its location; for Windows projects, the location is a folder on disk, but for Web projects, you specify a server running IIS.

Note also that you can add projects to the current solution using the New Project dialog box; just click the Add to Solution radio button instead of the Close Solution one (the default). If your project is entirely new, VB .NET will create an enclosing solution for the new project if there isn't already one.

Finally, note the Setup and Deployment Projects folder, which you use to create projects for deploying your program as we'll do near the end of the book.


Graphical Designers
When you're working on a project that has user interface elements-such as forms, VB .NET can display what those elements will look like at run time, and, of course, that's what makes Visual Basic visual. For example, when you're looking at a Windows form, you're actually looking at a Windows form designer, as you see in Figure 1.23, and you can manipulate the form, as well as add controls to it and so on.
Figure 1.23: A Visual Basic application graphical designer.

There are several different types of graphical designers, including:
Windows form designers
Web form designers
Component designers
XML designers
You may have noticed-or may already know from VB6-that Windows forms display a grid of dots, which you can see in Figure 1.23. To set the grid spacing, and specify whether or not controls should "snap" to the grid (that is, position their corners on grid points), you can use the Tools|Options menu item to open the Options dialog box, and select the Windows Form Designer folder, displaying the possible options for you to set.
In Visual Basic 6.0, coordinates for forms and controls were expressed in twips; in Visual Basic .NET, coordinates are expressed in pixels (and only pixels).

Code Designers
Unlike graphical designers, code designers let you edit the code for a component, and you can see a code designer in Figure 1.24. You can use the tabs at the top center of the IDE to switch between graphical designers (such as the tabs Form1.vb[Design], which displays a graphical designer, and the Form1.vb tab, which displays the corresponding code designer). You can also switch between graphical and code designers using the Designer and Code items in the View menu, or you can use the top two buttons at left in the Solution Explorer.
Figure 1.24: A code designer.

Note the two drop-down list boxes at the top of the code designer; the one on the left lets you select what object's code you're working with, and the one on the right lets you select the part of the code that you want to work on, letting you select between the declarations area, functions, Sub procedures, and methods (all of which we'll see starting in Chapter 2). The declarations area, which you select by selecting the (Declarations) item in the right-hand list box, is where you can put declarations of module-level objects, as we'll discover in Chapter 3 (see "Understanding Scope" in that chapter).
When you double-click a control in a graphical designer, its code designer will open and Visual Basic creates an event handler (see "Handling Events" in Chapter 4) for its default event (such as theClick event for buttons), which is a procedure that is called when the event occurs, as we'll see in Chapter 4. To add code to a different event handler, select the object you want to work with in the left-hand drop-down list box in the code designer, and select the event you want to add code to in the right-hand drop-down list box; Visual Basic will create an event handler for that event.

Also note the + and - boxes in the code designer's text area, at left. Those are new in VB .NET, and were introduced because VB .NET now writes a great deal of code for your forms and components automatically. You can use the + and - buttons to show or hide that code. For example, here's what that code looks like for a typical Windows form:
#Region " Windows Form Designer generated code " Public Sub New() MyBase.New() 'This call is required by the Windows Form Designer. InitializeComponent() 'Add any initialization after the InitializeComponent() call End Sub 'Form overrides dispose to clean up the component list. Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean) If disposing Then If Not (components Is Nothing) Then components.Dispose() End If End If MyBase.Dispose(disposing) End Sub Friend WithEvents TextBox1 As System.Windows.Forms.TextBox Friend WithEvents Button1 As System.Windows.Forms.Button 'Required by the Windows Form Designer Private components As System.ComponentModel.Container 'NOTE: The following procedure is required by the Windows Form Designer 'It can be modified using the Windows Form Designer. 'Do not modify it using the code editor. <System.Diagnostics.DebuggerStepThrough()> Private Sub _ InitializeComponent() Me.TextBox1 = New System.Windows.Forms.TextBox() Me.Button1 = New System.Windows.Forms.Button() Me.SuspendLayout() ' 'TextBox1 ' Me.TextBox1.Location = New System.Drawing.Point(32, 128) Me.TextBox1.Name = "TextBox1" Me.TextBox1.Size = New System.Drawing.Size(224, 20) Me.TextBox1.TabIndex = 0 Me.TextBox1.Text = "" ' 'Button1 ' Me.Button1.Location = New System.Drawing.Point(112, 56) Me.Button1.Name = "Button1" Me.Button1.TabIndex = 1 Me.Button1.Text = "Click Me" ' 'Form1 ' Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13) Me.ClientSize = New System.Drawing.Size(292, 213) Me.Controls.AddRange(New System.Windows.Forms.Control() _ {Me.Button1, Me.TextBox1}) Me.Name = "Form1" Me.Text = "Form1" Me.ResumeLayout(False) End Sub #End Region 

We'll dissect what this code means when we start working with Windows applications in depth in Chapter 4; for now, note the #Region and #End Region directives at top and bottom of this code-those are how the code designer knows that this region of code can be collapsed or expanded with a + or - button. Visual Basic also automatically adds those + or - buttons for other programming constructions like procedures, enumerations, and so on, allowing you to hide the parts of your code you don't want to see. The IDE is cluttered enough, and this helps a little in uncluttering it.
You can use the #Region and #End Region directives in your own code as well, allowing you to expand and contract whole sections of code at once.
As with the rest of the IDE, there are features upon features packed into code designers-for example, right-clicking a symbol lets you go to its definition, or its declaration, and so on.

IntelliSense
One useful feature of VB .NET code designers is Microsoft's IntelliSense. IntelliSense is what's responsible for those boxes that open as you write your code, listing all the possible options and even completing your typing for you. IntelliSense is one of the first things you encounter when you use VB .NET, and you can see an example in Figure 1.25, where I'm looking at all the members of a text box object.
Figure 1.25: Using IntelliSense.

If you enter some code that VB .NET considers a syntax error, it will underline the error with a wavy red line. You can rest the mouse over the underlined text to see a tool tip explaining what VB .NET thinks is wrong. That's not part of the IntelliSense package, although it also is useful.

IntelliSense is made up of a number of options, including:
List Members-Lists the members of an object.
Parameter Info-Lists the arguments of procedure calls.
Quick Info-Displays information in tool tips as the mouse rests on elements in your code.
Complete Word-Completes typed words.
Automatic Brace Matching-Adds parentheses or braces as needed.

There's also a Visual Basic-specific IntelliSense, which offers syntax tips that display the syntax of the statement you're typing. That's great if you know what statement you want to use but don't recall its exact syntax, because its syntax is automatically displayed.

IntelliSense is particularly useful when you can't remember what arguments a built-in Visual Basic procedure accepts (if these terms are not familiar to you, note that they're coming up in the next chapter), because it'll display those arguments as you type in the call to the procedure. Such procedures also can be overloaded, which means they have several forms that take different arguments-in such cases, IntelliSense will display an up and down arrow in its tool tip with the text "1 of n" where n is the number of overloaded forms, and you can use the arrows to select the overloaded form of the procedure you want prompts for.


IntelliSense is something you quickly get used to, and come to rely on. However, you can turn various parts of IntelliSense off if you want; just select the Tools|Options menu item, then select the Text Editor folder, then the Basic subfolder, and finally the General item in the Basic subfolder. You'll see a number of IntelliSense options you can turn on and off with check boxes.

The Object Explorer
IntelliSense is useful because it tells you what syntax is correct automatically, or lists all the members of an object that are available. Another useful tool that's too often overlooked by Visual Basic programmers is the Object Explorer. This tool lets you look at all the members of an object at once, which is invaluable to pry into the heart of objects you've added to your code. The Object Explorer helps open up any mysterious objects that Visual Basic has added to your code so you can see what's going on inside.

To open the Object Explorer, select View|Other Windows|Object Explorer (see Figure 1.26.)
Figure 1.26: The Object Explorer.

The Object Explorer shows all the objects in your program and gives you access to what's going on in all of them. For example, in Figure 1.26, I'm looking at a Windows form, Form1, and all its internal members-and the parameters they require-are made visible. To close the Object Explorer, just click the X button at its upper right.

The Toolbox
The toolbox is something that all veteran Visual Basic developers are familiar with, and you can see it in Figure 1.27.
Figure 1.27: The Visual Basic toolbox.

Microsoft has crammed more into the toolbox with each successive version of Visual Basic, and now the toolbox uses tabs to divide its contents into categories; you can see these tabs, marked Data, Components, Windows Forms, and General, in Figure 1.27. The tabs available, as you might surmise, depend on the type of project you're working on-and even what type of designer you're working with. The Data, Components, Windows Forms, and General tabs appear when you're working with a Windows form in a Windows form designer, but when you switch to a code designer in the same project, all you'll see are General and Clipboard Ring (which displays recent items stored in the clipboard, and allows you to select from among them) in the toolbox. When you're working on a Web form, you'll see Data, Web Forms, Components, Components, HTML, Clipboard Ring, and General, and so on.

The Data tab displays tools for creating datasets and making data connections, the Windows Forms tab displays tools for adding controls to Windows forms, the Web Forms tab displays tools for adding server controls to Web forms, and so on. The General tab is empty by default, and is a place to store general components, controls, and fragments of code in. (You can even add more tabs to the toolbox by right-clicking the toolbox and selecting the Add Tab item.) In fact, there are so many controls that even when you click a tab in the toolbox, you'll still most likely get a list that you have to scroll to see everything that's available.

When you're adding controls to forms using the toolbox, note that you can use the items in the Format menu and the Layout toolbar to align, make the same size, and set the spacing for controls. You can also select multiple controls and move and resize them all at once.

The Solution Explorer
We've already discussed the Solution Explorer quite a bit; this window gives you an overview of the solution you're working with, including all the projects in it, and the items in those projects. (You can seethe Solution Explorer in Figure 1.28.) This tool displays a hierarchy-with the solution at the top of the hierarchy, the projects one step down in the hierarchy, and the items in each project as the next step down.
Figure 1.28: The Solution Explorer.

You can set the properties of various items in a project by selecting them in the Solution Explorer and then setting their properties in the properties window. And you can set properties of solutions and projects by right-clicking them and selecting the Properties item in the menu that appears, or you can select an item and click the properties button, which is the right-most button at the top of the Solutions Explorer.

If you're working on an object that has both a user interface and code, you can switch between graphical and code designers by using the buttons that appear at top left in the Solution Explorer when that object has been selected. You can right-click a solution and add a new project to it by selecting the Add|New Project menu item in the popup menu that appears. And you can specify which of multiple projects runs first-that is, is the startup project or projects-by right-clicking the project and selecting the Set As Startup Object item, or by right-clicking the solution and selecting the Set Startup Projects item.

Much of what goes on in the VB .NET IDE depends on which solution or project is the current one, and you set that by selecting it in the Solution Explorer. For example, you can specify what icon you want an application to use in Windows if you don't like the plain default one; to do that, you select its project in the Solution Explorer, select Properties in the Project menu, then open the Common Properties|Build folder, browse to the .ico (icon) file you want, and click OK.

The Solution Explorer tracks the items in your projects; to add new items, you can use the menu items in the Project menu, such as Add Windows Form and Add User Control. To add new empty modules and classes to a project (we'll see what these terms mean in detail in the next chapter), you can use the Project|Add New Items menu item.

The Solution Explorer sees things in terms of files, as you can see in Figure 1.28. There, the References folder holds the currently referenced items (such as namespaces) in a project, AssemblyInfo.vb is the file that holds information about the assembly you're creating, and Form1.vb is the file that holds the code for the form under design. However, there's another way of looking at object-oriented programs-in terms of classes-and the Class View Window does that.

The data in AssemblyInfo.vb gives all kinds of information about the assembly, such as its version number. To set the version number of the assembly you're creating, open AssemblyInfo.vb and edit the line <Assembly: AssemblyVersion("1.0.*")> according to the directions you'll find directly above this line. Windows will be able to display this version number to the user in Windows tools such as the Windows Explorer.

The Class View Window
If you click the Class View tab under the Solution Explorer, you'll see the Class View window, as shown in Figure 1.29. This view presents solutions and projects in terms of the classes they contain, and the members of these classes.
Figure 1.29: The Class View window.

Using the Class View window gives you an easy way of jumping to a member of class that you want to access quickly-just find it in the Class View window, and double-click it to bring it up in a code designer.

The Properties Window
The Properties window is another old favorite in Visual Basic, although now it shares its space with the Dynamic Help window. The Properties window appears in Figure 1.30.
Figure 1.30: The Properties window.

You set properties of various objects in Visual Basic to customize them; for example, we've set the Text property of a button in the WinHello project to "Click Me" to make that text appear in the button. To set an object's properties when you're designing your program in Visual Basic-called design time (as opposed to run time)-you select that object (by clicking a control or form, or a project, or a solution), and then set the new property values you want in the Properties window.

The Properties window is divided into two columns of text, with the properties on the left, and their settings on the right. The object you're setting properties for appears in the drop-down list box at the top of the Properties window, and you can select from all the available objects using that list box. When you select a property, Visual Basic will give you an explanation of the property in the panel at the bottom of the Properties window, as you see in Figure 1.30. And you can display the properties alphabetically by clicking the second button from the left at the top of the Properties window, or in categories by clicking the left-most button.

To change a property's setting, you only have to click the right-hand column next to the name of the property, and enter the new setting. Often properties can have only a few specific values, in which case Visual Basic will display a drop-down list box next to the property's name when you click the right-hand column, and you can select values from that list. Sometimes, Visual Basic requires more information, as when you create data connections, and instead of a list box, a button with an ellipsis ("…") appears; when you click that button, Visual Basic will usually walk you through the steps it needs to get that information. Note also that, as usual with properties and methods in Visual Basic, not all properties of a form or control will be available at design time in the Properties window when you're designing your code-some will be available only at run time.

In fact, there aren't many changes in the Properties window from VB6 (something VB6 programmers might be pleased to hear), so if you've used it before, you're all set.

The Dynamic Help Window
The window that shares the Properties window's space, however, is quite new-the Dynamic Help window. Visual Basic .NET includes the usual Help menu with Contents, Index, and Search items, of course, but it also now supports dynamic help, which looks things up for you automatically. You can see the Dynamic Help window by clicking the Dynamic Help tab under the Properties window, and you can seethe Dynamic Help window in Figure 1.31.
Figure 1.31: The Dynamic Help window.

VB .NET looks up all kinds of help topics on the element you've selected automatically; for example, in Figure 1.31, I've selected a button on a Windows form, and dynamic help has responded by displaying all kinds of helpful links to information on buttons. This is more helpful than simply searching the whole help system for the word "button", because dynamic help will typically select introductory and overview help topics, not all the hundreds of topics with the word "button" in their text. If you click a help link in the Dynamic Help window, the corresponding help topic is opened in the central space of the IDE where the designers appear (and you can switch between designers and help topics using tabs).
Tip 

If you're like me, you'll find it too cramped in the IDE to display help topics effectively. You can have VB .NET display help in an external IDE-independent window instead, if you wish-select Tools|Options, then select the Help item in the Environment folder, click the External Help radio button and click OK.


Component Trays
In VB6, when you added a component to a form, and that component wasn't visible at run time-such as a timer control-the timer would still appear on the form at design time. That's changed in VB .NET; now, when you add components that are invisible at run time, they'll appear in a component tray, which will appear automatically in the designer, as you see in Figure 1.32.
Figure 1.32: Adding a timer to an application in a component tray.

The Server Explorer
You use the Server Explorer, which appears in Figure 1.33, to explore what's going on in a server, and it's a great tool to help make distant severs feel less distant, because you can see everything you need in an easy graphical environment.
Figure 1.33: The Server Explorer.

You can do more than just look using the Server Explorer too-you can drag and drop whole items onto Windows forms or Web forms from the Server Explorer. For example, if you dragged a database table onto a form, VB .NET would create the connection and command objects you need to access that table from code.

The Output Window
If you look at the bottom of the IDE, you'll see two tabs for the Output and Breakpoints windows. We'll look at the Breakpoints window when we discuss debugging, because it lets you manage the breakpoints at which program execution halts when you're debugging your code. The Output window, which you see in Figure 1.34, on the other hand, gives you the results of building and running programs, as you can also see in Figure 1.34.
Figure 1.34: The Output window.

You can also send messages to the Output window yourself if you use the System.Diagnostics.Debug.Write method like this: System.Diagnostics. Debug.Write("Hello from the Output window!").

The Task List
The Task List is another useful window that not many Visual Basic programmers know about. To see it, select the View|Show Tasks|All; this window appears in Figure 1.35. As its name implies, the Task List displays tasks that VB .NET assumes you still have to take care of, and when you click a task, the corresponding location in a code designer appears.
Figure 1.35: The Task List.

There are a number of such tasks; for example, if VB .NET has detected a syntax error, underlined with a wavy line as shown in Figure 1.35, that error will appear in the task list. If you've used a wizard, such as the Upgrade Wizard where VB .NET still wants you to take care of certain issues, it'll put a TODO comment into the code, as we saw earlier:
If blnDrawFlag Then 'UPGRADE_ISSUE: Graphics statements can't be migrated. 'Click for more: ms-help://MS.MSDNVS/vbcon/html/vbup2034.htm Line(X,Y) End If 
TODO comments like this will appear in the Task List.

In fact, you can create your own custom comments that the Task List will track. To do so, select the Tools|Options menu item then select the Task List item in the Environment folder, and enter the name of your custom comments in the Comment Token area. For example, if I entered STEVE there, then any comments beginning with 'STEVE will be tracked in the Task List.

The Command Window
Plenty of other windows are available. For example, selecting View|Other Windows|Command Window opens the Command window, as you see in Figure 1.36.
Figure 1.36: The Command window.

This window is a little like the Immediate window in VB6, because you can enter commands like File.AddNewProject here and VB .NET will display the Add New Project dialog box. However, this window is not exactly like the Immediate window, because you can't enter Visual Basic code and have it executed.

And there are other windows that we'll see as needed, such as when we're discussing debugging programs where we'll introduce the Call Stack window, the Breakpoints window, Watch and Value display windows, Autos and Locals windows, and so on.

Visual Basic Overview


The concept of computer programming
Before we begin Visual Basic 6 programming, let us understand some basic concepts of programming. According to Webopedia, a computer program is an organized list of instructions that, when executed, causes the computer to behave in a predetermined manner. Without programs, computers are useless. Therefore, programming means designing or creating a set of instructions to ask the computer to carry out certain jobs which normally are very much faster than human beings can do.

A lot of people think that computer CPU is a very intelligent thing, which in actual fact it is a dumb and inanimate object that can do nothing without human assistance. The microchips of  a CPU can only understand two distinct electrical states, namely, the on and off states, or 0 and 1 codes in the binary system. So, the CPU only understands a combinations of 0 and 1 codes, a language which we called machine language. Machine language is extremely difficult to learn and it is not for us laymen to master it easily.  Fortunately , we have many smart programmers who wrote interpreters and compilers that can translate human language-like programs such as BASIC into machine language so that the computer can carry out the instructions entered by the users. Machine language  is known as the primitive language while Interpreters and compilers like Visual Basic are called high-level language. Some of the high level computer languages beside Visual Basic are  Fortran, Cobol, Java, C, C++, Turbo Pascal, and etc .  

What is Visual Basic?
VISUAL BASIC is a high level programming language which  evolved from the earlier DOS version called BASIC. BASIC means Beginners' All-purpose Symbolic Instruction Code. It is a very  easy programming language to learn. The code look a lot  like English Language. Different software companies produced different versions of BASIC, such as Microsoft QBASIC, QUICKBASIC, GWBASIC ,IBM BASICA and so on. However, people prefer to use Microsoft Visual Basic today, as it is a well developed programming language and supporting resources are available everywhere. Now, there are many versions of VB exist in the market, the most popular one and still widely used by many VB programmers is none other than Visual Basic 6. We also have VB.net, VB2005, VB2008 and the latest VB2010. Both Vb2008 and VB2010 are fully object oriented programming (OOP) language.

VISUAL BASIC is a VISUAL and  events driven Programming Language. These are the main divergence from the old BASIC. In BASIC, programming is done in a text-only environment and the program is executed sequentially. In VB, programming is done in a graphical environment. In the old BASIC, you have to write program code for each graphical object you wish to display it on screen, including its position and its color. However, In VB , you just need to drag and drop any graphical object anywhere on the form, and you can change its color any time using the properties windows.

On the other hand, because  the user may click on a certain object randomly, so each object has to be programmed independently to be able to response to those actions (events). Therefore, a VB Program is made up of many subprograms, each has its own program code, and each can be executed independently and at the same time each can be linked together in one way or another.

What programs can you create with Visual Basic 6?
With VB 6, you can create any program depending on your objective. For example, if you are a college or university lecturer,  you can create  educational programs to teach business, economics, engineering, computer science, accountancy , financial management, information system and more to make teaching more effective and interesting. If you are in business, you can also create business programs such as inventory management system , point-of-sale system, payroll system, financial program as well as accounting program to help manage your business and increase productivity. For those of you who like games and working as games programmer, you can create those programs as well. Indeed, there is no limit to what program you can create ! There are many such programs in this tutorial, so you must spend more time on the tutorial in order to learn how to create those programs.

 
Twitter Bird Gadget