The Secret Guide to Computers

 home » read » visual basic 2  home  read  buy the book now!  contact us 


 
 

Objects

You’ve learned how to create and manipulate an object called “Form1”. You can create other kinds of objects also! Here’s how.…

Command button

Try this experiment. Exit from VB (by clicking its X button). Then go back into VB by doing this:

Click “Start” then “Programs” then “Microsoft Visual Studio 6.0” (if you see that choice) then “Microsoft Visual Basic 6.0”.

Press ENTER (which has the same effect as double-clicking “Standard EXE”).

Click “AutoRedraw”. Press the T key (to make AutoRedraw be True).

You see the Form1 window (which is blank). At the screen’s left edge, you should see theToolbox.

(If you accidentally erased it, make it reappear by clicking “View” then “Toolbox”.)

Each object in that Toolbox is called a tool. Double-click the Command Button tool. That makes a command button appear in the middle of Form1. The button is a rectangle and says “Command1” on it.

(If you wish, you can drag that button to a different place in Form1. You can also change the button’s size by dragging its 9 square handles. But for your first experiment, you just leave the command button where the computer put it.)

The command button says “Command1” on it. Just for fun, let’s make it say “Please click me” instead. To do that, click Caption (in the property list) and type “Please click me”, so the property list’s Caption line becomes:

Caption         Please click me

That makes the command button’s caption become “Please click me”.

Notice that the property list concerns the command button and its caption (instead of Form1’s caption), because the command button is highlighted.

Let’s write a program so if a human clicks the command button (which says “Please click me”), Form1 will say “Thanks for the click”. To do that, double-click the command button. The double-clicking tells the computer you want to write a subroutine about that object (the command button).


The computer starts writing the subroutine for you. The computer writes:

Private Sub Command1_Click()

 

End Sub

Insert this line in the middle of the subroutine —

Print "Thanks for the click"

so the subroutine looks like this:

Private Sub Command1_Click()

Print "Thanks for the click"

End Sub

That subroutine tells the computer that when the Command1 button is clicked, the computer should print “Thanks for the click” onto Form1. (The computer knows it should print that onto Form1 rather than onto the command button, because the Print command applies just to forms, not to buttons.)

Then run the program (by pressing F5). You’ll see Form1 with a button on it that says “Please click me”. If you click the button, the subroutine makes Form1 say “Thanks for the click”. If you click the button again, the subroutine makes Form1 say “Thanks for the click” again, so Form1 says “Thanks for the click” twice, like this:

Thanks for the click

Thanks for the click

Click the button as often as you wish; each time, Form1 says “Thanks for the click” again. If you click the button many times, Form1 will eventually get filled up and won’t be able to show any more thanks. If that happens, make Form1 bigger temporarily, by clicking its maximize button, so you can see more thanks.

When you get tired of clicking, end the program by clicking Form1’s X button.

Two buttons Let’s write a program that has two command buttons! Let’s make the first button be called “Red” and the second button be called “Blue”. If the human clicks the “Red” button, let’s make Form1 turn red; if the human clicks the “Blue” button, let’s make Form1 turn blue.

To do all that, start a new program as follows:

Close any old program.

If you’re already in VB, click “File” then “New Project” (then handle any questions about saving your old program). If you’re not in VB yet, click “Start” then “Programs” then “Microsoft Visual Studio 6.0” (if you see that choice) then “Microsoft Visual Basic 6.0” then press ENTER.

Press ENTER (which has the same effect as double-clicking “Standard EXE.”).

Click “AutoRedraw”. Press the T key (to make AutoRedraw be True).

You see the Form1 window (which is blank).

In the Toolbox, double-click the Command Button tool. A command button appears in Form1 and is called Command1. In the property list, click Caption then type “Click here for red”, so the property list’s Caption line becomes:

Caption         Click here for red

That makes the command button’s caption become “Click here for red”.

In the Toolbox, double-click the Command Button tool again. That makes another command button appear in Form1 and be called Command2. Unfortunately, the Command2 button covers up the Command1 button, so you can’t see the Command1 button. Drag the Command2 button out of the way (toward the right), so you can see both buttons side-by-side.

The Command2 button should be highlighted. (If it’s not highlighted, click it to make it highlighted.) In its property list, change its Caption from “Command2” to “Click here for blue”.

Now you screen shows Form1 with two buttons on it. The first button says “Click here for red”. The second button says “Click here for blue”.


Double-click the “Click here for red” button, and write this subroutine for it:

Private Sub Command1_Click()

BackColor = vbRed

End Sub

That subroutine says: clicking that button will make Form1’s background color be red.

Move that subroutine out of the way (by clicking its X button), so you can see Form1.

Double-click the “Click here for blue” button, and write this subroutine for it:

Private Sub Command2_Click()

BackColor = vbBlue

End Sub

While you’re writing that subroutine, you’ll see the other subroutine above it.

Then run the program by pressing F5. Here’s what happens.…

You see Form1 with two buttons on it. The first button says “Click here for red”; if you click it, Form1 turns red. The other button says “Click here for blue”; if you click it, Form1 turns blue.

Try clicking one button, then the other. Click as often as you like. When you get tired of clicking, end the program (by clicking Form1’s X button).

Where to put command buttons A good habit is to put command buttons side-by-side, in Form1’s bottom right corner. That way, the buttons won’t interfere with the Print command or any other objects on Form1.

Exit button To stop running a typical program, you have to click its X button. Some humans don’t know to do that. To help them, create a command button called “Exit”, so that clicking it will make the computer exit from the program.

To do that, create an ordinary command button; but make the button’s caption say “Exit” (or anything else you prefer, such as “Quit” or “End” or “Abort” or “Click here to end the program”), and make the button’s subroutine say End, like this:

Private Sub Command3_Click()

End

End Sub

Put that Exit button in Form1’s bottom right corner.


Option button

You learned how to use the Command Button tool. The Option Button tool is similar. Here are the differences.…

A command button is a gray rectangle, with a caption inside the rectangle. An option button is a tiny white circle, with a caption to the right of the circle.

Although you can put command buttons and option buttons wherever you wish, it’s customary to arrange command buttons horizontally (so the second command button is to the right of the first) but arrange option buttons vertically (so the second option button is below the first). The option buttons (and their captions) form a vertical list of choices. Command buttons are best if you have just 1, 2, or 3 choices; option buttons are best if you have 4, 5, or 6 choices.

When the human starts running your program, the first option button (which is Option1) has a black dot inside the white circle, and the computer automatically does Option1’s subroutine (even if the human hasn’t clicked the Option1 button yet).

Afterwards, if the human clicks a different option button, the black dot moves to that option button and the computer does that button’s subroutine. At any given moment, just one of the option buttons contains the black dot.

If you’re annoyed that Option1 starts with a black dot inside it (and automatically gets its subroutine run), make Option1’s caption say “Nothing” or “Plain” or “Regular” and have no subroutine.

OK button When the human clicks an option button, the computer can react to the click immediately, but that might startle and upset the human. If you want to be gentler, delay the computer’s reaction until the human also clicks an OK button, which confirms the human’s desires.

To do that, make the option buttons have no subroutines, so nothing will happen when those buttons are clicked. In Form1’s bottom right corner, create a command button whose caption says “OK” and whose subroutine looks like this:

Private Sub Command1_Click()

If Option1.Value Then write here what to do if Option1 button clicked

If Option2.Value Then write here what to do if Option2 button clicked

If Option3.Value Then write here what to do if Option3 button clicked

End Sub

That subroutine says: when the OK button is clicked, notice which option button was clicked and react appropriately.

Here’s how to start typing that subroutine. Type the word “If”, then a space, then the word “Option”, then 1, then a period. After you’ve typed the period, the computer will show you a list of what can come after the period. From that list, choose “Value” by using one of these methods.…

Method 1: scroll down to the word “Value” (by using the list’s scroll arrow), then double-click “Value”.

Method 2: highlight the word “Value” (by pressing the V key), then press the TAB key (which confirms that you want the highlighted choice).

Then type the rest of the subroutine.

Put the OK button in Form1’s bottom right corner.

Check box

A check box is a small white square, with a caption to the right of the square. At first, the white square has nothing inside it: the square is empty. While the program is running, clicking the square makes a check mark (a) appear in the square. If you click it again, the check mark goes away.

To create a check box, double-click the Check Box tool. A check box appears in the middle of Form1. Drag the check box wherever you wish. The first check box’s caption is temporarily “Check1”; to change that caption, click Caption (in the property list) and type whatever caption you wish.

If you want the computer to react immediately to whether the check box is checked, give the check box this subroutine:

Private Sub Check1_Click()

If Check1.Value = vbChecked Then

    write here what to do if Check1 box just became checked

Else

    write here what to do if Check1 box just became unchecked

End If

End Sub

For example, this subroutine makes the computer print “I am dressed” if the check box is just became checked, but print “I am naked” if the check box just became empty:

Private Sub Check1_Click()

If Check1.Value = vbChecked Then

    Print "I am dressed"

Else

    Print "I am naked"

End If

End Sub

When that program runs, the check box starts by being empty. Clicking the check box makes you see a and makes the computer print “I am dressed”. The next time you click the check box, the a disappears from the box, so the box becomes empty and the computer prints “I am naked”. Clicking the check box again makes the a reappear and makes the computer print “I am dressed”.

Form1 can contain many check boxes. The human can check several at the same time, so that several of the boxes contain check marks simultaneously. That’s how check boxes differ from option buttons: the human can check several check boxes but must choose just one option button.

Like option buttons, check boxes are usually arranged vertically (so the second check box is below the first).

OK button If Form1 contains several check boxes, you should typically delay the computer’s reaction until the human has decided which boxes to check, has checked all the ones desired, and has clicked an OK button to confirm that the correct boxes are checked.

To do that, make the boxes have no subroutines. Instead, create an OK button in Form1’s bottom right corner (by creating a command button there and making its caption be “OK”), then make the OK button’s subroutine look like this:

Private Sub Command1_Click()

If Check1.Value = vbChecked Then

    write here what to do if Check1 box is checked

Else

    write here what to do if Check1 box is unchecked

End If

If Check2.Value = vbChecked Then

    write here what to do if Check2 box is checked

Else

    write here what to do if Check2 box is unchecked

End If

End Sub

That subroutine says: when the OK button is clicked, notice which check boxes are checked and react appropriately.

For example, let’s make the computer print “I love you” in whatever style the human chooses: “bold” or “italic” or “bold italic” or “regular” (neither bold nor italic). To do that, create a check box for “bold”, a check box for “italic”, and an OK button that looks at which boxes are checked and prints “I love you” in the desired style. Here’s how to do all that:


Create a check box (called Check1) with caption “Bold”.

Create a check box (called Check2) with caption “Italic”.

Create a command button (called Command1) with caption “OK” and this subroutine:

Private Sub Command1_Click()

If Check1.Value = vbChecked Then

    FontBold = True

Else

    FontBold = False

End If

If Check2.Value = vbChecked Then

    FontItalic = True

Else

    FontItalic = False

End If

Print "I love you"

End Sub

Label

A label is a sneaky command button. Here’s the best way to create one:

Click the Label tool (just once). On Form1, drag from where you want the label’s top left corner to where you want the label’s bottom right corner.

Here’s how a label differs from a command button.…

A command button is a rectangle that’s small, and you can clearly see its four sides. A label is a rectangle that you typically make bigger, and its four sides are invisible while the program is running.

A command button’s caption is short, centered in the button’s middle. A label’s caption is typically longer: if you make the label big, you can make the caption be quite long, containing many words. In a typical label, the caption is a whole paragraph.

If your paragraph doesn’t fit in your label, enlarge the label by dragging the label’s handles. Another way to make your paragraph fit in your label is to do this before typing the paragraph:

In the property list, click AutoSize, then AutoSize’s down-arrow, then True. That lets the label automatically widen to fit the paragraph.

If the paragraph is too long to fit on a single line, also do this before typing the paragraph: click WordWrap, then WordWrap’s down-arrow, then True. That lets the label automatically grow taller to fit the paragraph. (To make that procedure work, do it before typing the paragraph and make sure you’ve set AutoSize to True.)

Printing to a label The Print command makes the computer start printing at Form1’s top left corner. If you want to print elsewhere on Form1, create Label1 where you want to print, then tell the computer to “print to Label1”. To “print to Label1”, tell the computer to “print to Label1’s caption”.

For example, suppose you want to print the answer to 4 + 2.

This Form1 subroutine prints the answer (which is 6) at Form1’s top left corner:

Private Sub Form_Load()

Print 4 + 2

End Sub

This Form1 subroutine prints the answer at Label1’s caption instead:

Private Sub Form_Load()

Label1.Caption = 4 + 2

End Sub

When writing that subroutine, make sure you say Label1.Caption, not just Caption. (If you accidentally say just Caption, that Form1 subroutine will print at Form1’s caption instead of Label1’s caption.)

That subroutine, if typed correctly, makes Label1’s caption say “6” instead of “Label1”, so you see 6 at Label1’s top left corner.


List box

A list box is a big white box that contains a list of choices, such as these color choices —

Red

Green

Blue

or these style choices —

Bold

Italic

or these country choices —

United States

Canada

Mexico

The list can be short (2 or 3 choices) or long (hundreds of choices). If the list is too long to fit in the box, the computer will automatically add scroll arrows so humans can scroll through the list.

To create a list box, double-click the List Box tool. A list box (big white box) appears in the middle of Form1. Drag the list box wherever you wish.

The first list box is called List1. Inside that list box, you temporarily see the word “List1”, but you should put your own list of choices there instead. Here’s how:

In the property list (at the screen’s right side), click List then List’s down-arrow. Type the first item you want in the list (such as “United States”); at the end of that typing, tap the ENTER key while holding down the Ctrl key. Type the second item you want in the list (such as “Canada”); at the end of that typing, press Ctrl with ENTER again. Type the third item you want in the list (such as “Mexico”). Continue that process, until you’ve typed all the items you want. (You can type as many items as you wish; as you type, the items automatically scroll up to let you type more.) At the end of the last item, press just ENTER (without holding down the Ctrl key).

On Form1, you see the list box containing some of your choices. Probably your choices are too many or too long-winded to fit in the box completely. Enlarge the box by dragging its handles, until the box becomes wide enough to hold the widest choice and tall enough to hold all or many of the choices. (If the box isn’t tall enough, the computer automatically adds scroll arrows so humans can scroll through the list while the program runs.)

SingleSelect You can give List1 this kind of subroutine:

Private Sub List1_Click()

Select Case List1.Text

Case "United States"

    write here what to do if “United States” clicked

Case "Canada"

    write here what to do if “Canada” clicked

Case "Mexico"

    write here what to do if “Mexico” clicked

End Select

End Sub

If you want the action to be delayed until the human clicks an OK button, do this:


Create the OK button (a command button whose caption is “OK”).

Give List1 no subroutine, but give the OK button this kind of subroutine:

Private Sub Command1_Click()

Select Case List1.Text

Case "United States"

    write here what to do if “United States” clicked

Case "Canada"

    write here what to do if “Canada” clicked

Case "Mexico"

    write here what to do if “Mexico” clicked

End Select

End Sub

MultiSelect If you want to let the human select several items from the list (instead of just one item), do this:

In List1’s property list, click MultiSelect then MultiSelect’s down-arrow.

Click either “1 — Simple” or “2 — Extended”. (If you choose “1 — Simple”, the human can select several items by clicking them, and deselect an item by clicking that item again. If you choose “2 — Extended”, the human can select one item by clicking it, select or deselect extra items by holding down the Ctrl key while clicking them, and select a contiguous bunch of items easily by clicking the bunch’s first item and SHIFT-clicking the last.)

Create an OK button (a command button whose caption is “OK”).

Give List1 no subroutine, but give the OK button this kind of subroutine:

Private Sub Command1_Click()

If List1.Selected(0) Then write here what to do if  the list’s top item (“United States”) clicked

If List1.Selected(1) Then write here what to do if the list’s next item (“Canada”) clicked

If List1.Selected(2) Then write here what to do if the list’s next item (“Mexico”) clicked

End Sub

Label Next to your list box, you should put a message, explaining the list box’s purpose to the human.

To put the message there, you could use Print statements, but it’s hard to get Print statements to print in exactly the right spot (next to the list box). Here’s an easier way to put your message in the right spot: create a label (sneaky command button) whose caption is your message. Drag the label until it’s next to your list box. Give the label no subroutine, so clicking the label makes no difference.

Text box

You already learned that Form1’s subroutine can contain this line:

x = InputBox("What is your name?")

When you run the program, that line makes the computer create an input box. The input box is a pop-up window containing a message (“What is your name?”), a wide white box (in which the human types a response), and an OK button (which the human clicks when finished typing).

That technique works adequately but gives you no control: you have no control over the size or position of the window, the message, the white response box, or the OK button.

To be more professional, get control by creating a text box instead. Here’s how.

Double-click the Text Box tool. That creates a text box (a white box in which the human can type a response). Drag it wherever you wish. Adjust its size by dragging its handles.

The box’s interior temporarily says “Text1”. Make the box’s interior be blank instead, by doing this: in the property list, click Text then press the SPACE bar then the BACKSPACE key.

Above the box (or left of the box), create a label (sneaky command button) containing a message (such as “What is your name?”)

Below the box (or right of the box), create an OK button (a command button whose caption is “OK”). Make the OK button’s subroutine include this line —

x = Text1.Text

and anything else you want the computer to do, such as:

Print "I adore anyone whose name is "; x

Form1 can contain several text boxes. For example, you can include:

a text box for the human’s first name

a text box for the human’s last name

a text box for the human’s address

text boxes for the human’s city, state, and ZIP code

That makes Form1 be truly a form to fill in! Create just one OK button to handle all those text boxes, so the human clicks the OK button after filling in the entire form.


Password character If you want the human to type a password into a text box, do this: in the text box’s property list, click PasswordChar then type an asterisk (the symbol *). That makes the box show asterisks instead of the characters the human is typing. That prevents enemies from discovering the password by peeking over the human’s shoulder.

MultiLine Normally the text box is restricted to holding just one line of text. To let the text box handle several lines of text well, do this:

In Text1’s property list, click MultiLine then press the T key (which stands for True). That lets the text box handle several lines of text, lets the human press the ENTER key at the end of each line, and lets the computer press the ENTER key automatically if there are too many words to fit on a line.

Make the text box taller and wider (by dragging its handles), so it can show more lines of text and more words per line. That reduces the human’s frustration.

In Text1’s property list, click ScrollBars then the ScrollBars down-arrow then “2 - Vertical”. That creates a vertical scroll bar, which helps the human move through the text, in case you didn’t make the text box tall enough to handle all the words.

Combo box

A combo box is a fancy text box that includes a list of suggested responses.

To create a combo box, double-click the Combo Box tool. That creates a combo box. Like a text box, it’s a white box in which the human can type a response; but the combo box’s right edge shows a down-arrow, which the human can click to see a list of suggested responses.


all content copyright © 2002 | This website is provided as a community service by StaffASAP, the best Applicant Tracking System in the World!