Controls

In this example code (containers.zip) picture example We create a picturebox control and add some other controls around and in it to alter the picture it displays. The picture box control is a container object. That means that it has properties and methods of its own but also can contain other controls within it. You can effect the controls in a container as a group. Moving the container moves the controls. You can alter any of the properties that the contained controls have in common.

We have an example of another container in this form. A frame has very few properties. It's mostly used to give a group of related controls place of their own. You can reduce the complexity of your forms by collecting controls into groups based on their function. In this form from the grade book, the controls concerning the class information are grouped together. Frame example

Another cause for using frames is to collect option buttons together. Option buttons act as a group. That is, if one of them is selected, the others are deselected. All the option buttons on the page are related. You can have separate groups of options by putting them in frames. Since frames can be made without a border or caption, they can be invisible.

To put things in a frame, you have two choices. You can create the frame first and then create the controls inside it or you can cut and paste existing controls into the frame. You can't simple drag and drop the controls. The controls must fit into the frame, so adjust the sizes to fit.

The BASIC Language

The programming elements to be discussed in here are loops, selects and file handling.

Loops

VB supports several kinds of loop structures. The first we will see is the standard for loop. Here are some examples with the equivalent C code for comparison.

Dim i as Integer
For i = 3 To 18 Step 3
   Debug.print "i is " & Cstr(i)
Next i
	
				
for(int i=3; i<=19; i += 3) 
{
   cout << "i = " << i << endl;
} // end for
	

Dim i as Integer
For i = 20 To -2 Step -1
   Debug.print "i is " & Cstr(i)
Next i
	
				
for(int i=20; i>=-2; i--) 
{
   cout << "i = " << i << endl;
} // end for
	

Dim i as Integer
For i = 0 To 10 
   Debug.print "i is " & Cstr(i)
Next i
	
				
for(int i=0; i<=10; i++) 
{
   cout << "i = " << i << endl;
} // end for
	

There are couple of kinds of do loops as well.

Dim i as Integer
i = 0;
Do While i < 3
   Debug.print "i is " & Cstr(i)
   i = i + 1
Loop 
	
				
int i = 0;
while(i<3)
{
   cout << "i = " << i << endl;
   i++;
} // end while
	
Runs 0 or more times

Dim i as Integer
i = 0;
Do 
   Debug.print "i is " & Cstr(i)
   i = i + 1
Loop While i < 3
	
				
int i = 0;
do 
{
   cout << "i = " << i << endl;
   i++;
} while(i<3)
	
Runs 1 or more times

Dim i as Integer
i = 0;
Do Until i > 3
   Debug.print "i is " & Cstr(i)
   i = i + 1
Loop 
	
				
int i = 0;
while(i<=3 )
{
   cout << "i = " << i << endl;
   i++;
} 
	
Runs 0 or more times

The for each loop doesn't have a direct equivalent in C. It is very useful when dealing with collections of things like arrays.


		Dim num as Variant
		Dim myarray as Variant
		myarray = Array(4,5,6,7)
		For Each num In myarray
			Debug.print "num is " & Cstr(num)
		Next num

		const int SIZE=4
		int num, myarray[SIZE] = {4,5,6,7}
		for (int i = 0; i < SIZE; i++) {
			num = myarray[i];
			cout << "num is " << num << endl;
		} // for

Select

This is like the switch in C but more flexible. The general form looks like:


		Select Case value
		case val1
			code for val1
		case val2
			code for val2
		case val3
			code for val3
		case else
			code for everything else
		end select
A bit like

		switch(c ) {
			case v1:
			      code for v1
			      break;
			case v2:
			      code for v2
			      break;
			case v3:
			      code for v3
			      break;
			default:
			      code for everything else
			      break;
		} // end switch
But, the values can only be integers or characters and the case only checks equality. In the select case statement, there are other possible comparisons. Like these from the select code example (select.zip):

    Dim i As Integer
    i = CStr(Text1.Text)
    Select Case i
        Case Is < 2
           Label1.Caption = "i is less than 2"
        Case 2
           Label1.Caption = "i is 2"
        Case 3, 4, 5
           Label1.Caption = "i is 3 or 4 or 5"
        Case 6 To 10
            Label1.Caption = "i is between 6 and 10, inclusive"
        Case Else
            Label1.Caption = "i wasn't caught be any of the above"
    End Select

Programming Notes