Monday, February 7, 2011

Visual Basic 6.0 - Session 2

1. Open a new form and change its name to ColourChanger.  Place the following objects on this form.
  •  A heading label2 (Caption = Colour Changer)
  •  3 horizontal scroll bars (Set the max value property of all three to 255)
  •  3 other labels (2red, 3Green, 4Blue)
  •  a command button to quit the form (Caption = Return)
  •  another small label5 under the button with its visible property set to false.



2. Double click each scroll bar and add the following code to its _onChange() event.  Use cut and paste to make the task easier.

Label1.BackColor=RGB(HScroll1.Value,HScroll2.Value,HScroll3.Value)
Label5.BackColor=RGB(HScroll1.Value,HScroll2.Value,HScroll3.Value)
Label1.ForeColor=RGB(255-HScroll1.Value,255-HScroll2.Value,255-
HScroll3.Value)
Label5.ForeColor=RGB(255-HScroll1.Value,255-HScroll2.Value,255-
HScroll3.Value)
Label5.Visible=True
Label5.Caption=“WOW!”
Label2.BackColor=RGB(HScroll1.Value,0,0)
Label3.BackColor=RGB(0,HScroll2.Value,0)
Label4.BackColor=RGB)0,0,HScroll3.Value)

3. Double click the return button and add the following code to its _onClick() event

UnloadMe
 
4. Use the Project Explorer window to return to your main form and double click example 2 in your menu to add the appropriate code.

5. Use <F5> function key to test your project.  Save and backup.

Naming conventions  
Up till now, we have often accepted default names, Text1, Label1, etc.  In a big project, this is not good practice as it makes the code harder to read or maintain.  Naming conventions use a prefix of three lowerCase letters to identify the type of control, followed by a meaningful name. eg.  lblTitle




 Data types in VB
A variable is a named location that holds data.  A variable can only hold one datatype.  A program can have as many variables as you need but before you can use a variable it must be declared.

You use a DIM statement to declare variables (where DIM stands for dimension).  Here is the format of the DIM statement:

Dim VarName As Datatype

e.g.  Dim curCost As Currency, Dim strSurname As String







A function is a segment of code that accepts zero, one or more arguments and returns a single result.  Visual Basic includes many built-in functions (intrinsic functions).  Some perform basic mathematical tasks.  Others manipulate string data such as converting text to upperCase or lowerCase letters.  

An argument is a value you pass to a function so the function has data to work with.

Function names have parentheses at the end to hold the function arguments.  Even if a function has no arguments, the parenthesis are required.

Two intrinsic functions include message boxes and input boxes.

No comments:

Post a Comment