Unit1 Visual Programming SPIT106
Unit1 Visual Programming SPIT106
After a short delay, the form will pop up super-imposed on the Visual Basic IDE
▪ Notice that this form has neither a minimize nor a maximize button and is located
relative to the screen as was indicated in the Form Layout window.
▪ Notice also that, unlike when you changed the caption, the changes to the Max and Min
button properties show up only at run time (that is, when you run the application).
To run a Visual Basic application
– To return to developing an application, you can click on the exit (X) button on the form,
or open the Run menu and click the End option, or use the End tool (the tool that looks
like a stop button on a cassette recorder).
Summary of the methods for changing
the properties
Scale Properties
Property Description
Sets the unit of measurement (e.g.,
ScaleMode
Twips, Pixels, Points, etc.).
Sets the coordinate for the left edge of
ScaleLeft
the scale.
Sets the coordinate for the top edge of
ScaleTop
the scale.
Sets or returns the width of the internal
ScaleWidth
coordinate system.
Sets or returns the height of the
ScaleHeight
internal coordinate system.
Define the current drawing position
CurrentX, CurrentY (used with graphics methods like Print,
Line).
Color Properties
• The BackColor and
ForeColor properties via
the color Platte
– hexdecimal code
– hexadecimal color codes
allows you to set up to
16,777,216
Color Properties
• The system tab on this dialog
box gives you a list of the colors
currently used by windows for
its various elements. If you click
on the palette tab, the colour
grid shown here pops up.
• The background color of the
form will automatically show
changes to the Background
property.
• You won’t see the effect of
changing the ForeColor
property until you do something
like displaying text on the form.
Working with the Color Palette
• Open the color palette. Open the color palette by going to the View menu and
choosing the color palette (ALT + V, L). Here’s something like what you’ll see:
• Notice, to the left of the palette, a dark box enclosed in a lighter box.
• The inner box diaplays the current foreground color, and the outer box display
the current background color.
• You can change the foreground color by clicking the inner box and then
clicking any of the colored boxes displayed. To change the Backgraound color,
click the outer box and then click any of the colored boxes displayed.
Working with the Color Palette
• The text box in the lower-left corner of the color
palette displays the foreground and backgraound
colors for any text in the form in the form or
control you’ve selected. To go back to the default
colors specified in the Windows control panel,
click the Default command button at the right.
• You can also create your own coco for the color
palette. Each of the blank boxes on the button of
the color palette represents a possible custom
color. To make one, follow these steps:
• Click one of these blank boxes, and
then click the Define Colors command
button (which is now enabled). This
opens the Define Color dialog box,
shown here.
• Change the amount of red, green, or
blue; color; hue; saturation; or
luminosity of the color to suit your
need by adjusting the controls in the
dialog box.
• Press the Add Color button to create
the custom color or the Close button to
cancel.
• To make the color palette go away, double-click on
its control box or use the ALT + F4 shortcut when
the palette has the focus.
Making a Form Responsive in
Visual Basic 6.0
Understanding Events and Form
Behavior
Introduction
• Making a form responsive is essential for creating interactive Visual Basic applications.
• Forms must respond to user actions (e.g., mouse clicks, loads, etc.).
• Making a Form Responsive
• By now, you should be comfortable with designing Visual Basic forms. But making a form in
the right size, shape, and having the right background color is hardly what Windows
programming is all about.
• The essence of a Microsoft Windows program (and, therefore, of Visual Basic) is to make
your forms respond to user actions.
Event Handling in VB6
• Visual Basic object can recognize many different events.
• For example, if a user clicks an area on the screen, you may want to display a message;
• if the user clicks a command button, you may want to perform a specific action, such as
displaying an image.
• The point is that although Visual Basic objects can recognize many different events, the
objects will basically sit there insert unless you’ve written code to tell them what to do when
the event happens.
• This means that for any events to which you want a Visual Basic object to respond, you must
write an event procedure telling Visual Basic what to do.
• Event procedures are nothing more than the lines of programming code that tell Visual Basic
how to respond to a given event.
Form_Load Event
End Sub
Printing a Visual Representation of a Form
•If you don’t notice the color change and try to run
defined
run as before.
Typos
• Visual Basic can even find some syntax errors after you finish typing a line. To make sure this
feature is on (or to turn it on, if it is off), choose Tools/Options and then go to the
Environment page. Move to the item marked Auto Syntax Check and make sure that it is
checked (on).
Saving Your Work
• You should get into the habit of saving your work frequently and, in fact, Visual Basic
will not let you exit the program or start a new project without asking whether you
want to save your work.
• Even more is true: Visual Basic lets you set its options so that it will automatically save
your work before a project runs.
• This minimizes the direct effects of system crashes, because you can’t save a project
while it is running or when you are in break mode.
• To activate this feature:
• Choose Tools/Options and go to the Environment page.
• In the “When a program starts” box, change the “Don’t Save Changes” to
either one of the two other options. (I always choose the “Save changes,
don’t prompt” option.)
• Saving from the File Menu
– Save form:
• Show current working form
• CRTL+S (or use ALT+F.S).
• The default extension for form files is. Frm.
• The first time you choose this option, Visual Basic pops up a dialog box
• Subsequent uses do not pop up a dialog box
– Save project:
• to save the entire project
• ALT+F,V
• Vbp is the extension
– Save Project As:
Creating Stand-Alone Windows Programs
• One of the most exciting of Visual Basic is the ability to change your projects into stand- alone
• These will be files that users can simply double-click on in Explorer in order to run them.
• To make a stand-alone VB application, simply go to the File menu and choose the Make Project
• .vbp file with an.exe extension). For the stand-alone program, the Windows desktop uses the
same icon that Visual Basic uses for the executable version of the project.
• When you distribute a Visual Basic program, it is also necessary to supply one or more dynamic
• DLLs can be shared, no matter how many different stand-alone Visual Basic applications users
have, they need only one copy of the 1.4 megabyte VB DLL in a directory accessible to Windows.
• The will also need only one copy of any custom control files used by your Visual Basic application.
Project Dialog box
Toolbox and Controls
• Toolbox contains UI controls (buttons,
textboxes, etc.)
• Use View → Toolbox or ALT+V, X
• Click and drag controls onto form
Creating and Naming Controls
• Double-click to create control
• Use 'Name' property to identify in code
• Prefix convention: cmd for button, txt for textbox
• There are two reasons why the feature of locating controls only
at grid points-called aligning to the grid or snapping to the grid-
makes positioning controls in Visual Basic a snap.
– First, the corners and sides of any control will always end up on a grid
mark.
– Second, you have a small amount of leeway when you move the
crosshair, Only when the vertical line of the crosshair hits a grid mark
does the object move left or right.
– Similarly, only when the horizontal line of the crosshair hits a grid mark
does the object move up or down.
Working with a Control Already on a Form
• To work with an existing control, you must first select it (remember the
buzzword for this is “giving it the focus’).
• You do this by moving the mouse pointer until it is inside the control and
then clicking. (Or, you can press the TAB key until the focus is at the
control.
• You can tell which control has the focus by looking for the sizing
handles.).
Resizing an Existing Control
• Suppose you’ve created a command button but aren’t happy with its
size. To change the size of an existing command button at design time,
you either:
• Use the Properties window to adjust the Width and Height properties,
or Work with the sizing handles.
• Simply click inside the control by moving the mouse pointer to the
control and clicking once. When the sizing handles are visible, you know
the control is selected.
• Try resizing a control with the sizing handles by following these steps:
– Move the mouse pointer to sizing handle and click and hold sown the left mouse button.
– Drag the mouse until the control is the size you want.
• Moving an Existing Control
– Now, to move an existing control
– Move the pointer anywhere inside the control, click the left
mouse button, and hold it down.
– Drag the mouse until the control is at the location you want it
to be, and then release the left button.
• For finer control over the movement of controls:
– Select the control.
– Use CTRL + an arrow key to move the control one grid mark at
the time
– Or, you can directly adjust the left and Top properties in the
Properties window.
Using the double-Click shortcut for creating
control
• As you know, if you double-click on any of the toolbox icons, the matching control appears in
the center of the screen.
• The more controls you double-click on, the higher they get stacked. You then can use the
techniques from the previous section for moving controls to reposition and resize the
controls on the stack.
• For example, suppose you want to create an application with five command buttons
symmetrically dispersed, as shown in figure 4-1.
• The easiest way to do this is to double-click on the Command Button icon five times.
• This stacks five command buttons in the center of the form.
• Then you can easily use the method given in the previous section to move the buttons to the
locations shown in figure 1-12
Working with multiple controls
• Here are the steps for the “dragging” method to select multiple control:
– Imagine a rectangle that surrounds only those controls you want to select. Move to one corner of this
imagined rectangle and click the left mouse button.
– Hold the left mouse button down and drag the dotted rectangle until it covers all (and only) the controls
you want to select. Then release the mouse button.
The grouped controls all show sizing handles now. Note that all but one of the sizing handles are grayed. This is how
you know a group has been successfully selected.
• Once you have selected a group controls, when you move any control in the group, Visual Basic moves the other
controls in a similar way.
• For example, if you move one control that is part of a selected group down two grid marks, all the other controls
move down two grid marks.
• The dragging method of selecting multiple controls only works when the controls to be move can be placed in a
rectangular “lasso” that excludes any other controls. If the controls are widely scattered on the form, you’ll need
this next method:
• Select each control by moving the mouse pointer to it and holding down the left mouse button while pressing
CTRL.
Working with multiple controls
Locking Controls
Deleting control
• ALT and one other key, the access key, to quickly activate a control or a menu for example.
These access keys are underlined in the caption of the control or name of the menu item.
• Visual Basic makes it easy to set up an access key for any object that has a Caption property.
• When you set the caption, all you have to do is place an ampersand ($) in front of the
• For example, look at Figure 2-1. Notice that the C in the caption is underlined. When the
application is running. You can activate this button either by pressing the ALT+C combination
• Although it is possible to have the same access key for more than one control on a form,
doing so is unusual.
• What happens in this situation is that the focus moves to the next control with the same
access key, but that control is not activated until you click the mouse or press the SPACEBAR.
Image Controls
• Image controls hold pictures. They can also be used to create toolboxes if you don’t have the
Professional edition
• The toolbox for an image control is the sun over a mountain
• Image controls can be used to
– Caption allowing access key for control
– Display icons or pictures created with a program such as Microsoft Paintbrush.
– They can also hold Windows metafiles or Jpegs or Gifs (the most common formats for the
Internet). Jpeg
– format is also the common format used by most digital cameras.
• You load a picture into an image control by resetting the value of the Picture property.
• If you choose the Picture property for the image control, this opens up a dialog box that lets you
choose what image file to load.
• You can also reset the Picture property directly by copying an image from a graphics program to the
Windows clipboard and then using the Copy command on the Edit menu to paste the image into the
image control.
• Visual Basic will attach the graphic to the project when you save it.
• Finally, you can also change this property via code
Properties of Image Controls
• BorderStyle property for an image control has two possible settings: you can either have no
the picture.
– If you change it to True, the picture resizes (as best it can) to fit the control.
• As a general rule, only Windows metafiles - which essentially store directions for drawing
pictures rather than actual bitmaps - can be enlarged without great loss of detail.
– To see this, start a new project and place two large image control on the form. Set the
Stretch property to True for one of them, and leave the default for the other.
– Load the TRFFCO7 “slippery road” icon. Notice that the detail in the enlarged image is
• Using the mouse is the most common way to move from control to control in a Windows application,
but your applications have to allow for using the TAB key as well.
• Tab order is the term used in a Windows application for the sequence of controls that pressing TAB
moves you through.
• In a Visual Basic application, the order in which you create the controls is the order used for the tab
order. The first control you create at design time is the one that receives the focus when the
application starts. If you press TAB once when the application is running, you move to the second
control you created at design time, and so on. If you press TAB when the focus is at the last control
you’ve created, the focus moves back to the first control. (Disabled controls are skipped).
• It’s Possible to change the setting for the tab order via the Properties window or by writing code.
• The property you need to set is the Tablndex property.
• If you set this value to 0 for a control, this control automatically becomes the first control in tab order,
and all the other controls move upward in tab order. What used to be the first control in tab order is
not the second, the second is now the third, and so on.
• If you change a control with a higher tab index, then only controls with larger tab indexes are affected.
If you create a control and set the Tablndex property at design time, the settings for the Tablndex
property are moved higher to make way for the new control.
• You can also change the Tablndex property via code
The Grid