Introduction to Visual Basic 6.0
Colours and graphics (continued)
• Multiple forms
• Control array
• Created from RGB value (Red/Green/Blue)
• Each value specifies the intensity of the individual colour
- O indicates absence and 255 saturation
• VB supports 16 million colors (256x256x256)
• RGB function to create a Long value of a color
pinkColor = RGB(255, 175, 175)
• Color depth - a computer graphics term describing the number of bits
used to represent the color of a single pixel in a bitmapped image.
• Also known as bits per pixel (bpp)
• The number of distinct color that can be represented by a pixel
depends on the number of bits per pixel (bpp) - two to the power of a
• E.g. one bit - Monochrome image; eight bits – palette images; 16 bits –
high color images; 24 bits – true color images
Sets the background colour of the form or control used for drawing.
If you change the BackColor property after using graphics methods
to draw, the graphics are displayed in the new background colour.
Sets the colour used by the graphics methods to create text or
graphics in a form or control.
Sets the colour of the border of a shape control.
Sets the colour of shapes created either by graphic methods or
note – need to set the FillStyle to solid not transparent
• In visual basic a colour is represented by a long integer
• There are two ways in which colours can be set
• RGB function
• QBColor function
• The RGB function uses the concept of specifying the intensity of the
individual colours Red Green and Blue to achieve the final desired
• Each colour (red green and blue) is specified by using a value
between 0 and 255 (0 represents least intensity, 255 represents most)
• These values are passed to the function and the result can be assigned
to a color property.
• E.g. frmFormColour.BackColor = RGB(125, 123, 45)
• Primitive way of specifying colour
• Colour is represented by a number between 0 and 15
• frmFormColour.BackColor = QBColor(12) ‘sets background to red
Single Document Interface (SDI)
• So far we have just looked at SDI applications
• Applications where the forms are independent of each other
• SDI applications can become complex when there are many forms
Multiple Document Interface (MDI)
• In MDI applications there is one main form (called a parent). This form acts
as a container to multiple forms that can reside inside it (child forms).
• Child forms cannot be moved outside of the parent, nor can they be bigger.
• When maximised a child form will be the same size as its parent and when
minimised it will appear like an icon inside the parent form.
• MDI applications are advantageous as they group all the forms together (like
individual documents inside MS word).
• All child forms can share the same menu system as the parent.
• You can use as many as you’d like
• Only one form is loaded and actively displayed at startup
• Use the project property to specify the startup form
• At startup, the system automatically loads one designated
• Loading more than one form requires the Load statement
and the Show method
• Loads an existing form into memory. Once the form is
loaded, its properties and controls can be altered by the
application, whether or not the form is actually visible
• Use the Load statement when you want to load a form
without displaying it
Show and Hide Statements
• Used to display or conceal a form on the screen during run
• Working with more than one form
• The simplest way is using the .hide and .show methods.
• For example
Starting with a Main() Procedure
• An alternative to using a start up form with attached code
• Sub Main can only appear in a standard module, not in a
Benefits of Sub Main()
• Error checking before the program is launched
• Check for proper screen resolution
• Check for screen size (enables auto-sizing forms)
• Promotes modularity and flexibility
SDI and MDI Interfaces
• SDI interfaces feature totally independent windows
• MDI interfaces feature child windows within one parent
• Child menus are displayed by the parent when child is not
maximized or minimized
• Examples: Notepad vs Word
Creating a MDI application
• From the file menu, choose New Project
• From the project menu, choose New MDIForm to create the
container form. The project should contain an MDI form
(MDIForm1) and a standard form (Form1) .
• Now all normal forms that are added can be set as child forms (if
desired). This includes the form that already exists as part of your
• This is set using the MDIChild property to true/false in the properties
• We must now tell our application where we want it to start (which
form to load first).
• To do this we must set the project properties. From the project menu
select project properties, this will open a dialog. On the General tab,
modify the start-up object (set it to the mdi form). This tells VB
what form to load first.
Creating an MDI Application
10 - 4
Code in Modules
• Up to now, all the code we’ve written has been directly contained on
the forms we’ve been creating.
• In visual basic we can place our code in a module.
• These modules are added to the project in the same way in which
forms can be added.
• Code placed in a form can only be used inside that form, whereas
code placed in a module can be called/used anywhere in your
application (if declared as public).
• It is also good practice to declare global variables in a module.
• Code on a module must be placed inside procedures.
• In a module variables are usually declared at the top with procedures
• Containers for function or subroutine
code not associated with specific
• Utility code or common
• Invisible at run time
10 - 5
• The arrays you have seen so far are variable arrays. Eg arrays that
store, integers, variables, records etc.
• We can also create arrays of controls. This is a slightly different
concept than arrays of variables.
• Many of our programs use groups of controls (like a list of text boxes
to display the contents of a record)
• Control arrays allow us to group collections of controls together
• Control arrays can be created by placing our first or primary control
on our form, lets use a button as an example, command1.
• We then select and copy this button
• When we try to paste this button control the computer will ask us do
we want to create a control array.
• If we select no visual basic will create another button called command2 (the
first one was called command1).
• If we choose yes, visual basic will create a button with the same name, but it
will introduce a new feature, what we end up with is an array of two buttons.
• Eg command1(0)
• If we choose to continue pasting buttons we just add to this array.
. We can then change properties of the individual buttons like this
command1(3).caption = “press to clear”
Using control arrays
• This is a very powerful feature.
• Lets say we have an array of text boxes on a form
• We can easily carry out multiple operations on these text boxes
• Example 1
Lets say we want to clear them all
for i = 0 to 3
txtBox(i).txt = “”
This is much neater and more efficient than writing
txtBox(0).text = “”
txtBox(1).text = “”
txtBox(2).text = “”
txtBox(3).text = “”
Using control arrays
• Example 2
Another instance were control arrays are useful is when we want to display the
content of a data array.
Lets say we have an array of 4 integers ( arrayData() ) that we want to display in 4
If these text boxes are actually an array of text boxes we can say
for i = 0 to 3
txtBox(i).txt = arrayData(i)
Again, this is much neater and more efficient than writing
txtBox(0).text = arrayData(0)
txtBox(1).text = arrayData(1)
txtBox(2).text = arrayData(2)
txtBox(3).text = arrayData(3)
Features of Control Arrays
• We’ve seen already how the properties of each element in the array
can be changed e.g. txtBox(0).text = “”
• We can see that although the controls are in an array, they each have
their own individual properties.
• One limitation is that, when controls are in an array they all share the
same events. For example, if we have an array of 20 command
buttons, the command_click event will be the same for all the buttons
in the array (ie although there are 20 buttons on the screen there is
only one piece of code attached to them all!)
• We can get round this, as the events associated with a control array
are always passed the index of the element which they were invoked
Private Sub cmdButton_Click (Index as integer)
Summary of control arrays
• We create a control array by copying and pasting any control on our
• We can only create control arrays of the same type of control. i.e. an
array of command buttons, and array of text boxes and so on……
• The index of a control array works just as it does in a regular array,
and again begins with zero for the first element.
• The controls in a control array have individual properties.
• Controls in a control array all share the same event code.