What Is Visual Basic and Why do I need it? Visual Basic is Easy to learn Programming language.

With Visual Basic you can develop Windows based applications and games. Visual Basic is much more easier to learn than other language (like Visual C++), and yet it's powerful programming language. Visual Basic suit more for application developing than for Games developing. You can create sophisticated games using Visual Basic, But If you want to make a really advanced professional game like Quake 2, You may choose other language (like C++), that would be much more harder to program with. However, Visual Basic will be probably powerful enough to suit all your application and games programming needs. The advantages of Visual Basic: 1) It's simple language. Things that may be difficult to program with other language, Can be done in Visual Basic very easily. 2) Because Visual Basic is so popular, There are many good resources (Books, Web sites, News groups and more) that can help you learn the language. You can find the answers to your programming problems much more easily than other programming languages. 3) You can find many tools (Sharewares and Freewares) on the internet that will Spare you some programming time. For example, if you want to ping a user over the internet in your program, Instead of writing the ping function yourself, you can download a control that does it, and use it in your program. Compare to other languages, Visual Basic have the widest variety of tools that you can download on the internet and use in your programs. The disadvantages of Visual Basic: 1) Visual Basic is powerful language, but it's not suit for programming really sophisticated games. 2) It's much more slower than other langauges. Click Forward to start writing now your first Visual Basic program! Getting Started Note that all the images in this tutorial taken from Visual Basic version 6.0. If you using other version of Visual Basic, the images that you will see may be a little different. Run the Visual Basic program. The first thing you see is: Figure 1

Here you choose what is the kind of the program you want to create. For creating a simple Visual Basic program, choose the Standard EXE, and press the Open Button. (If Figure 1 is not the first thing you see when you run Visual Basic, choose from the Visual Basic menu File->New Project (Figure 2)) Figure 2

After you've clicked the Open button, you will see: Figure 3

Getting Started (Continue) Look at the form with the title bar Form1. This is how your program will look like. Everything you will place on this form will appear in your program. As you can see, your form is currently empty. You didn't program anything yet, but lets run the program! Click on the Play button in the toolbar (Figure 4) Figure 4

Now your program is running (Figure 5): Figure 5

As you can see, the form is empty. You can move the form, minimize and maximize it. To stop the program from running, you can click on the Stop button in the Toolbar (Figure 6), or click the form's X button (Figure 7). It's very recommended that you will always close your programs using the

form's X button instead of the Visual Basic Stop button. It's like shutting Windows From the Start button, instead of Pressing the Power button. Figure 6

Figure 7

Learning about Properties Every component (form is component for example) has properties, that determine its look and its functioning. Properties can be the component color, width, height and more. To see the form properties, select from the menu View->Properties Window (Figure 8). Figure 8

The properties window looks like this: Figure 9 .

For example. and the form's name is Form1. You can change the form's properties.3D. The column marked by the black circle contains the form's properties values: The form's Name is Form1. lets change the form's Caption property. and so on. What is the Caption property? The Caption is the text that appears on the Form's title bar. Figure 11 . The column marked by the blue circle contains the form's properties names: The form has Name property. To change the Caption property. the current Caption is Form1.In the red circle you can see the component name: These are the properties of a Form. and more. simply click on the Caption property in the form's properties names column (Figure 10). Right now the text that appears on the form's title bar is Form1 (Figure 5). Appearance property AutoRedraw property. Figure 10 As you see. The form's Appearance property is 1 . Delete the "Form1" text and type instead of it "Hello" (Figure 11).

You can see all these controls in the Toolbox. To see the Toolbox. Select from the menu View->Toolbox (Figure 13). Change the Icon property to change the icon that appears on the form's title bar. Text Boxes. and in this way learn what does it do. Pictures.Now run the program using the Play button (Figure 4). Figure 13 . Adding Controls to the Form There are many controls you can use with your program: Buttons. Figure 12 Try to change others properties: Change the form's BackColor property to change the form's background color. Scroll Bars and more. You will see that the text on the form's Title bar is Hello (Figure 12). You can try and change every property.

And you will see the Toolbox (Figure 14): Figure 14 .

Buttons in Visual Basic called "Command Buttons".Now lets add a button to your form. Figure 16 . the Command Button icon will look pressed (Figure 16). Figure 15 As results. To add a button. click on the Command Button icon in the Toolbox (Figure 15).

and hold the button down while moving the cursor. You can see that you have a button on your form. Run the program by clicking the Play button.Now click on the form with the left mouse button. Figure 18 . click on it with the right mouse button and select Properties for the pop-up menu (Figure 18). But it still doesn't do anything. and in the place of the rectangle you will see a button (Figure 17). Release the mouse button. To see the Command Button's Properties window. and change its size by dragging the Blue Hot Spots that found on each one of its conrners. that you can click on it. You will see a rectangle. You can change its location by dragging it. Figure 17 Changing the button's Properties Now you have a button on your form.

because this is this component type. Command1 . . appears "Command1 CommandButton". Note that two components can't have the same name.because it's the name of this specific Command Button (look at the name property). If you will add another Command Button to your form. its name will be "Command2" by default. right under the title bar.The Command Button's Properties window (Figure 19): Figure 19 As you can see. In the top of the properties window. CommandButton .

768 to 32. and you will see that "Click Here" appears on your Command Button. The first type called Integer. Lesson 1 Learning about Variables Using Variables is one of the most basic and important subjects in programming. so this lesson is very important. You can also delete the text string that found inside the drawer and put instead of it other text string. For example. The change won't take affect. you can open the drawer with the sticker "TheUserName" and get the text string that found inside the drawer.768) The second type called String. Change the Command Button's BackColor property to blue (or other color). you can have one variable that holds the Text "Hello and Goodbye". untill you will set the Command Button's Style property to 1 -Graphical. Variables are destined to save data. Integer variable can store an Integer number (round number without any fraction) between -32. .5) or the number 100.5 (Because of the . You can put in each one of the drawers one number or one text String. this is the best way to learn what every property does. and have another variable that holds the number 623882 You can think about variables as a cabinet's drawers. You can also add other controls from the Toolbox to your form. by simply changing the component's Name property.000 (Because It's bigger than 32767) or the number -50. The caption property is the text that appears on the Command Button. You can store in String variable any text that you want. You can store in Integer variable the number 0 or the number 375 or the number -46.042 (Because it's smaller than -32. Every drawer has a sticker on it with its unique name. You can save in variable Text or number. but you can't store the number 4.If you want you can change the Name of any component.767. You can now play a little bit with the Command Button's properties. Now lets change the Command Button's Caption property. Change the Caption property to "Click Here". Right now. During your program. And play around with their properties. we are going learn about 2 variable types.

Blah!" You can program the events. you can change the Form's color (for example). You've pressed a key? The "KeyPress" event has been launched. The code that belongs to the Form_Load event should be placed between Private Sub Form_Load() and End Sub The Form_Load event should look like this: Private Sub Form_Load() (The beginning of the Form_Load event) This is the code that belongs to the Form_Load event End Sub (The end of the Form_Load event) The Command Button's Events Now lets program some of the Command Button's events. You will see the "Code Window" (Figure 20). What does it mean? Everything that happening. So the code that you will enter to the Form_Load event will be launched when the program is being started. You can play a MP3 file. double click on the form. and when a key is pressed. Figure 20 The Code Window opened with the Form_Load event. Check which events the Command Button has by clicking . launch an event. To start programming the events. and this happening when you start the program.For example "Hello" or "abcDDefGhIjk123456 blah blah %$#@!??? Learning about Events Visual Basic is "Event Driven" language. When the mouse moves. Select "Command1" from the components list (Figure 23). You've moved the mouse? The "MouseMove" event has been launched. The Form_Load event occurs when the form is loaded.

the Events list (Figure 24). every one of them has its own unique Click event. The Command1_Click event is being executed. where appears right now the text "Form" (Figure 23). the Command Button's Click event called "Command1_Click". and a message box with the text "You have Clicked on the button!" is popping. How can you find them? Click on the Drop-Down List that found in the upper left corner of the Code Window. therefore if you had 5 Command Buttons on your form. If we had program the Click event of a Command Button with the name "BlahBlah7". We want to execute a code when the user is clicking on the button. Select "Click" from the Events list. So lets program the Command Button's Click event. Every component has its own unique events. Insert the following line to the Click event: MsgBox "You have Clicked on the button!" After you inserted this line to the Click event the Click event should look like this: Private Sub Command1_Click() End Sub MsgBox "You have Clicked on the button!" As you can see. because the name of the specific Command Button that we program its Click event is "Command1". the Click event would be called "BlahBlah7_Click". More Events The Form has more events besides the Form_Load event. Run the program. Figure 23 . and click the button. When you clicking the Command Button with the name "Command1".

Which events the form has? Click on the Drop-Down List that found in the upper right corner of the Code Window.You will see a list of the components that found in your program. You have 1 command button with the name "Command1" and 1 Form. MouseMove and more. The Form_Unload event occurs when the form is being unloaded. MouseDown. Select "Unload" from the form's events list. Figure 24 You will see the complete list of the form's events: Load. So the code that you will write in the Form_Unload event will be launched when you close the program. . LostFocus. so select "Form" from the list (Figure 23). and this happening when you close the program (Using the form's X button (Figure 7)). Lets program the Form_Unload event. where appears right now the text "Load" (Figure 24). We want to program a form's event. Here you select which component's event you want to program.

When the form is being loaded at the very start. . We want to execute a code when the user is clicking on the button. If we had program the Click event of a Command Button with the name "BlahBlah7". the Form_Unload event should look like this: Private Sub Form_Unload(Cancel As Integer) End Sub MsgBox "GoodBye" Run the program. Check which events the Command Button has by clicking the Events list (Figure 24). therefore if you had 5 Command Buttons on your form. The Form_Load event is being executed and a message box with "Hello" text is popping. Select "Click" from the Events list. So lets program the Command Button's Click event.Insert the following line to the Form_Unload event: MsgBox "GoodBye" After you've inserted this line to your Form_Unload event. the Form_Unload event is being executed and a message box with "GoodBye" text is popping. the Command Button's Click event called "Command1_Click". every one of them has its own unique Click event. because the name of the specific Command Button that we program its Click event is "Command1". When you close the program by clicking the form's X button. the Click event would be called "BlahBlah7_Click". Select "Command1" from the components list (Figure 23). Every component has its own unique events. The Command Button's Events Now lets program some of the Command Button's events. Insert the following line to the Click event: MsgBox "You have Clicked on the button!" After you inserted this line to the Click event the Click event should look like this: Private Sub Command1_Click() End Sub MsgBox "You have Clicked on the button!" As you can see.

and click the button.Run the program. Here you select which component's event you want to program. The Command1_Click event is being executed. Figure 24 . Figure 23 You will see a list of the components that found in your program. where appears right now the text "Load" (Figure 24). More Events The Form has more events besides the Form_Load event. We want to program a form's event. where appears right now the text "Form" (Figure 23). and a message box with the text "You have Clicked on the button!" is popping. Which events the form has? Click on the Drop-Down List that found in the upper right corner of the Code Window. You have 1 command button with the name "Command1" and 1 Form. When you clicking the Command Button with the name "Command1". How can you find them? Click on the Drop-Down List that found in the upper left corner of the Code Window. so select "Form" from the list (Figure 23).

LostFocus. MouseDown. When you close the program by clicking the form's X button. For example.You will see the complete list of the form's events: Load. The Command Button's Events (Continue) To learn about more events. Select "Unload" from the form's events list. and this happening when you close the program (Using the form's X button (Figure 7)). Lets program the Form_Unload event. Insert the following line to the Form_Unload event: MsgBox "GoodBye" After you've inserted this line to your Form_Unload event. The "Print" command simply writing a text on the form. the Form_Unload event should look like this: Private Sub Form_Unload(Cancel As Integer) End Sub MsgBox "GoodBye" Run the program. When the form is being loaded at the very start. the following line: . The Form_Unload event occurs when the form is being unloaded. MouseMove and more. the Form_Unload event is being executed and a message box with "GoodBye" text is popping. So the code that you will write in the Form_Unload event will be launched when you close the program. we will use the "Print" command. The Form_Load event is being executed and a message box with "Hello" text is popping.

The New Command Button's name is "Command2" by default (Figure 26) Figure 26 Now. And select "Click" from the Command2 events List.Print "Hello" Will write "Hello" on the form (Figure 25) Figure 25 Add another Command Button to your form. rewrite the Command1 Click event and insert the following line to it: Print "This is Command1" Select "Command2" (This is the name of the second Command Button) from the Components list (Figure 23). .

Now the MyTest variable stores the Number 10. and when you are clicking on Command2 Button.Enter the following line to the Command2_Click event: Print "This is Command2" After you've done so. your code should look like this: Private Sub Command1_Click() End Sub Private Sub Command2_Click() End Sub Print "This is Command1" Print "This is Command2" Run the program. When you are clicking on Command1 Button. but how can you access this value from your program? You can do that using the variable name. the text "This is Command2" appears on the form. Lesson 2 Working With Integers The process of creating variable called "Declaring" To Declare (=create) Integer variable simply write: Dim MyTest As Integer What does the line above do? It creates an Integer variable with the name MyTest. Now you can put a number inside this variable. Dim = Declare MyTest = the name of the new variable As Integer = The new variable type will be Integer. the text "This is Command1" appears on the form. Example: Print MyTest The line above will write 10 on the form. Pay attention to the differents between . You can do that by simple write: MyTest = 10 The line above will insert the number 10 into the MyTest variable.

. Anyway. and press the button at run-time. and will print the value that found in the variable. and we . The Print MyTest Line will print the value that found in the MyTest variable. and will print it As-Is.. Where you putting a text Without quotes (Like in the upper line). Visual Basic treat it as a variable name. Working With Integers (Continue) Question: What will be printed on the form after executing the following code: Dim Blah As Integer Print Blah Blah = 10 Blah = 20 Print Blah Blah = 30 Print "Blah" Print Blah After you've thinking about the answer. Visual Basic treat it as a Text. the Answer can be found on the next page. you can check it by inserting the code above into a Command Button's Click event. The Print "MyTest" Line will print MyTest on the form. therefore it will print 10 on the form. Working With Integers (Continue) Answer: 0 20 Blah 30 Why is that? Lets pass over the code line after line: Dim Blah As Integer A new Integer with the name Blah has been declared Print Blah Will print the Value that found in the Blah variable.Print MyTest and Print "MyTest" Where you putting a text inside quotes (Like in the bottom line). But there is nothing in the Blah variable! The Blah variable has just been declared.

the old value is being immediately deleted. The default value of any integer variable is 0.didn't put inside it any value yet. and when you put in it a value.20 Blah = 30 Now the Blah variable holds the number 30 Print "Blah" Will print the Text that found between the quotes .0 Blah = 10 Now the Blah variable holds the number 10 Blah = 20 Now the Blah variable holds the number 20 What's happened to the 10 that was inside it? It has been deleted! A variable can holds only one value.Blah Print Blah Will print the value that found right now in the Blah variable .30 Working With Integers (Continue) Question: What will be printed on the form after executing the following code: Dim Blah As Integer Blah = 2 . So what is the differents between Blah = 20 and Blah = 10 Blah = 20 ? There is no differents! In both cases the Blah variable will hold the number 20 Print Blah Will print the value that found right now in the Blah variable . When you write : Dim Blah As Integer It's like you've written: Dim Blah As Integer Blah = 0 So it will print the value that found right now in the Blah variable .

So the string 2 + 3 will be printed on the form. and then Visual Basic will execute the command Print 5 So in other words. and then will execute the command on the evaluation result.. . Everything that found inside quotes is being treated as a string. Print "2 + 3" will print the text string 2 + 3 Print Blah + 3 Now the expression is Blah + 3. Print "2 + 3" As I said before. In this case we execute the command Print on the expression 2 + 3.Print 2 + 3 Print "2 + 3" Print Blah + 3 Print Blah Blah = Blah + 1 Print Blah Blah = Blah + Blah Print Blah The Answer can be found in the next page. The evaluation result is 5. Print 2 + 3 is equivalent to Print 5 after executing the Command Print 5 . 5 is been printed on the form. Print 2 + 3 will print the value of the expression 2 + 3. Working With Integers (Continue) Answer: 5 2+3 5 2 3 6 Why is that? Lets pass over the code line after line: Dim Blah As Integer A new Integer with the name Blah has been declared Blah = 2 Now Blah holds the value 2 Print 2 + 3 When you execute command (the Print command in the case) on expression.. Visual Basic will evaluate the expression first. The expression will be evaluated: 2 + 3 = 5.

The expression value is 6. After the replacement the computer evaluates the expression 3 + 3. the value of the expression Blah + Blah The computer is first evaluate the expression Blah + Blah Blah is being replaced with its value: 3. In this case the value of the variable Blah is 2. Because the Blah value is 2. after the "Blah + Blah" expression evaluation. Blah = Blah + 1 The line above simply says: Put in the Blah variable. So now. this command will put the value 3 in the Blah variable. The expression value is 3. Print Blah Will replace the Blah variable with its value: 3. the command is: Blah = 3 As you know by now. the value 3 will be inserted into the Blah variable. So the Blah in the expression is being replaced with 2. the value 6 is being inserted into the Blah variable. After the replacement the computer evaluates the expression 2 + 1. Print Blah Will replace the Blah variable with its value: 6. So the command that will be eventually executed is Print 6 More Complicated Expressions You can use the following operators in expressions: . the command is: Blah = 6 After executing the command Blah = 6. Print Blah Will replace the Blah with its value. it's being replaced with its value. the value 2 will be printed on the form. So the command that will be eventually executed is Print 3 Blah = Blah + Blah Will Put in the Blah variable.when a variable is found inside expression. So now. Summary: After executing the command Blah = Blah + 1. after executing this command the value 5 will be printed on the form. the value of the expression Blah + 1 The computer is first evaluate the expression Blah + 1 Blah is being replaced with its value: 2. after the "Blah + 1" expression evaluation. After the replacement the new command is Print 2 + 3 As we saw earlier. After the replacement the new command will be Print 2 After executing this command.

this is a valid expression: (Blah + 5) * MySecondVariable . and 1.Can store only Integer numbers between 0 to 255 Integer . and at last "+" and "-" For example: 2 + 3 * 4 is equal to 14 (2 + 3) * 4 is equal to 20 More Variable Types What will be printed on the form after executing the following code? Dim ABC As Integer ABC = 4.401298E-45 for negative values.483.483. So how can you store a number with fraction in a variable? For this purpose you have other variable types that can store round numbers like the Integer type.647 Single . So the computer will round the 4.648 to 2.401298E-45 to .+ Plus .767 Long .(4 + 10 / Blah) The order of the operators are like in math: First "(" and ")" .147. then "^". then "*" and "/".Can store only Integer numbers between -2.8 to 5.402823E38 to -1. The Variable types list: Byte . and in addition can store numbers with fractions.Can store only Integer numbers between -32.Can store Non-Integer (numbers with fractions) Numbers between 3. and will Insert 5 to the ABC variable.768 to 32.147.Minus * Multiply / Division ( Openning parenthesis ) Closing parenthesis ^ Power For example. So the Command Print ABC will print 5 on the form.8 Print ABC Answer: The Integer variable can't store a number with fraction.

declare it as Byte variable and save a little amount of memory.Can store Non-Integer Numbers between 4.402823E38 for positive values.94065645841247E-324 to Why would I use Byte variable that can store Integer numbers between 0 to 255. declare it as Single or as Double.79769313486232E308 for positive values -1.79769313486231E308 to and 4. When you assign a text to a String variable. The Declaring command is very simple: Dim Abc As Double Dim Blah As Single Dim Popeye As Byte Working With Strings String variables are meant to store Text.3. Double . If you need to store Non-Integer numbers in variable. For example: Dim Abc As String Abc = "Good Morning" Question: What will be printed on the form after executing the following code? Dim kuku As String kuku = "Hello!!!" Print "kuku" Print kuku . while I can use Integer variable that can store numbers between -32.94065645841247E-324 for negative values. 1. So if you have variable that will not store numbers greater than 255 or less than 0.768 to 32. you must put the text inside quotes.767? The Answer is: Because the Byte variable is taking less memory.

Example: Dim gogo As String Dim popo As String gogo = "Hello" popo = "world" gogo = gogo + " !!! " + popo Print gogo The code below will print Hello !!! world on the form. Print kuku Will replace the kuku with its value (Hello!!!). the command that will be executed is gogo = "Hello !!! world" Scope of Variables Insert two Command Buttons to your form (with the names Command1 and Command2). So it will print kuku on the form. The expression gogo + " !!! " + popo is equal to Hello + " !!! " + world and that's equal to "Hello !!! world" So eventually. The computer Is NOT associate the text "kuku" with the variable kuku. and Add the following code to your program: Private Sub Command1_Click() End Sub Dim gogo As Integer gogo = 100 . and after the replacement will execute the new command Print "Hello!!!" Back Working With Strings (Continue) You can join two Strings together using the "+" operator. because of the quotes.Answer: kuku Hello!!! Why is that? Lets pass over the code line after line: Dim kuku As String Will create a new String variable kuku = "Hello!!!" Now the kuku variable holds the text Hello!!! Print "kuku" Everything that found inside quotes is being treated as text String.

"Exist" only in the Sub or function that he was declared in. no matter from which sub you call it. Choose "(General)" From the components List in the code window (Figure 1). the code that found in the Command1_Click event will be executed. and it will store the value 10. and it's not declared in the Command2_Click event. because it begins with Sub and ends with End Sub For Example: Private Sub Command1_Click() End Sub MsgBox "Hello" Every line of code that found between the Sub Command1_Click() and the End Sub is belong to the sub Command1_Click() So because we've declared the gogo variable in the Commad1_Click event. So if you want that a specific variable will be "exist" in your whole program. But instead of displaying the value of the gogo variable (100). is the scope of the variable. Scope of Variables (Continue) As you saw in the previous page. Every variable that been declared. To declare variable that will be exist in all subs..Private Sub Command2_Click() End Sub MsgBox gogo Run the program. it exist only within the sub. like if the gogo variable hasn't been declared at all! The reason for all of this. In result. It shows nothing. Now press on the Command2 button. you have to declare it in the "Declarations area" of your code. If you declare variable in a sub. What is Sub? (We will learn about functions later) Sub is a Block of code that starts with Sub and ends with End Sub Every event is a sub. and click on the Command1 button. The gogo variable will be declared. the MsgBox gogo Line will be executed. it's declared only within the sub. what can you do? The Answer is in the next page.. Figure 1 .

Simply write: Dim gogo As Integer And delete the old statement that found in the Command1_Click event. but we didn't assign any value to it yet.0.a message box with the value 100 is popping. A message box with the number 0 is popping. Lets check it out: Run the program. After you've done so.Put the gogo declaration statement in the "Declarations area". and should be available from every part of your code. . Press the Command2 Button. Press the Command2 Button . Press the Command1 Button. The value 100 is being assigned to the gogo variable. It is because the gogo variable has been declared. The gogo variable is being declared immediately when the program is being started. your code should look like this: Dim gogo As Integer Private Sub Command1_Click() End Sub Private Sub Command2_Click() End Sub gogo = 100 MsgBox gogo Now the gogo variable is being declared in the Declarations area of your code. so right now its value is the default value .

For example you can't use the name Print. aBhguKJhUJYf. the following names are NOT 4)You can't call your variable with a name that is a Visual Basic Command or Function (these names are been called "saved names"). MsgBox Examples for valid names: a. KeyDown and KeyUp Events The events that will be mentioned in the following pages are commonly used. valid: A B. Sub. numbers and the character _ (the underscore character) You can't use other characters like: ` ! @ # % ^ &*() Therefore. A. abc123. BB$. arrays and more. _aba Therefore. 3. All the saved names are being painted with blue color. You can know easily if a name is a saved name by typing it anywhere in your code. tot#o 2)The Name Must begin with a letter: You can't 3)You can't use space in the variable's name. you choose its name. AaAaA. What you've learnt by now is necessary for continuing with the next lessons. End. Go to Lesson 3 to learn more about Events and properties Lesson 3 The Command Button's KeyPress. The names you choose for your variables don't have to have any meaning. hello world use the names: 2abc. 2345. abc123abc123. The Rules: 1)The only characters that can appear in the name are letters. booleans. and without learning about them . but still there are some limitations about the names you can call your variables. For example: Dim Tuti As Integer The example above will set the variable name to be "Tuti". like constants. More advanced variables subjects will appear during the next lessons. hello_world123 There is much more to learn about variables. the following names are NOT valid: AB!. Few examples for saved names: Print.Choosing A Valid Name For Variable When declaring on variable. because there is command in Visual Basic that called Print (which we used pretty much in this tutorial).

Key_Press. Your form will look like this: Figure 1 . start a new project (as being taught in Lesson 1). and when the Command Button's KeyUp event will be executed. "KeyPress" will be printed on the form. Add 1 Command Button to your form. "KeyUp" will be printed on the form. The Command Button is called by default Command1. "KeyDown" will be printed on the form. Nothing has happened. and then release it. Shift As Integer) Print "KeyDown" End Sub Private Sub Command1_KeyPress(KeyAscii As Integer) End Sub Print "KeyPress" Private Sub Command1_KeyUp(KeyCode As Integer. Run the program.you really can't go anywhere in Visual Basic programming. When the Command Button's KeyPress event will be executed. hold it down for few seconds. To try these examples. Shift As Integer) Print "KeyUp" End Sub When the Command Button's KeyDown event will be executed. Now press any key on the keyboard. Copy the following code to the code window (you can copy and paste it using Ctrl + C for copying and Ctrl + V for pasting): Private Sub Command1_KeyDown(KeyCode As Integer. and click the button with the mouse. It's because the KeyDown. and KeyUp events are being executed Only when you press a key on the keyboard.

After every KeyDown event that been executed. Look at the first line of the Command Button's Click event: Private Sub Command1_Click() And at the first line of the Command Button's KeyPress event: Private Sub Command1_KeyPress(KeyAscii As Integer) The Click event's first line is ended with empty parentheses () and the KeyPress event's first line is ended with (KeyAscii As Integer) What is the (KeyAscii As Integer) ? It's a parameter that been passed to the KeyPress event. and then again KeyDown. Learning about Parameters Parameters are variables that being passed to a Sub. When you release the key.Lets see: The first event that been executed is the KeyDown event. The second event was KeyPress. until the key is up again. because "KeyDown" was the first text that been printed on the form. . the KeyUp event is being executed once. the KeyDown and the KeyPress events are being executed in this order over and over again. We learnt that when a key is being holded down. a KeyPress event had been executed.

You will see the Ascii value of every key you're pressing. Learning about Parameters (Continue) Look at the first line of the Command Button's KeyDown Event: Private Sub Command1_KeyDown(KeyCode As Integer. The KeyPress event is being executed when the user press a key. With This Ascii value you can know on which key the user has pressed. How can I know which character's Ascii value is 98? Use the Chr command. Like if you've declared Dim KeyAscii As Integer Why do we need this variable? Because its value is very useful. Every characters has its own KeyAscii value. and This variable holds the Ascii value of the key that been pressed. and 2 characters that are the same letter. More About Ascii How can I know what is the Ascii value of a specific character? Use the Asc command. Shift As Integer) . Insert the following line to the Command1 KeyPress event: Private Sub Command1_KeyPress(KeyAscii As Integer) End Sub Print KeyAscii Run the program and press several keys. but have different case. the following line: Print Chr(98) Will print on the form the character that its Ascii value is 98. Lets see an example. the Ascii value of the "A" character is 65. For example. For example. The KeyAscii parameter value will be 65. have different KeyAscii value.This parameter is an Integer variable with the name KeyAscii. Notice that the KeyAscii values of "A" and "a" are differents. the following line: Print Asc("b") Will print on the form the Ascii value of the character "b". For example. If the user has pressed the key "A" on the keyboard.

for example: Alt. the KeyCode value of the Ctrl Key is 17. For example. If the user has pressed the Alt key and the Shift key together. The left arrow key. the Shift value will be 4 (for the Alt key) + 1 (for the Shift key) = 5 Examples: If the user has pressed the "A" key. @. can get one parameter (like the KeyPress event) or can get more than one parameter. The second parameter is Shift. For example: Private Sub MySubName (Parameter1 As String. |. and the value 4 if the user has pressed the Alt Key. and more) but there are some keys that don't represent any character. The Shift holds the value 1 if the user has pressed the Shift key. The KeyCode parameter will hold the number 65. but they have KeyCode value. Ctrl or Alt keys. The different between KeyCode and Ascii. and the Shift parameter. The first is KeyCode. the characters "a" and "A" have different Ascii value. but they have the same KeyCode value. . Parameter3 As String) Lets check out the KeyDown event's parameters. Because of that.As you can see. while KeyCode represent Keyboard's keys. The KeyCode parameter will hold the number 65. and its value helps you to determine if the user has pressed the Shift. and the Shift parameter will hold the value 0. Parameter2 As Integer. both are Integer type. the value 2 if the user has pressed the Ctrl Key. Every Sub can get no parameters (like the Click event). the KeyDown event gets two parameters: The KeyCode parameter. the parameters have to be separated with commas. F4. =. These keys don't have Ascii value. Ctrl. and it holds the KeyCode value of the pressed key. and the Shift parameter will hold the value 1. because the same key is typing "a" and "A". If a sub is getting more than one parameter. If the user has pressed Shift and "A" together. The KeyCode value is usually different from the Ascii Value. Ascii represent characters. is that every character has Ascii value (for example G.

Y As Single) Print "MouseMove" End Sub Private Sub Command1_MouseUp(Button As Integer. Shift As Integer. For example. the Shift value will be 1. Shift As Integer. Y As Single) Print "MouseDown" End Sub Private Sub Command1_MouseMove(Button As Integer. Copy the following code to your program: Private Sub Command1_MouseDown(Button As Integer. X As Single. Y As Single) Print "MouseUp" End Sub Run the program.The KeyUp event's parameters are the same as the KeyDown event's parameters. Release the button. and the MouseUp event is being executed. Click on one of the mouse buttons when the mouse is over the button. .nothing is happening. Shift. Y As Single) Each one of these events gets the same parameters: Button. MouseMove. and hold the button down.The Button's MouseMove event is being executed every time you move the mouse over it. MouseDown and MouseUp events. MouseMove. Shift As Integer. X As Single. therefore you see that "MouseMove" is being printed on the form every time you move your mouse over the button. if you'll press the Shift button while clicking the mouse. by inserting different Print commands to every event. MouseDown and MouseUp Events (Continue) Lets check out these events parameters. X As Single. X As Single. Private Sub Command1_MouseDown(Button As Integer. Move the mouse over the Button . The MouseDown event is being executed. Shift As Integer. X and Y. Move the mouse over the form . The Shift parameter is the same as the KeyDown event's Shift paramater. MouseDown and MouseUp Events Lets check out the Command Button's MouseMove. We will do that like we've done before.

the line: Print "Hello". Figure 3 . relative to the upper left button corner. and change the following properties to see what they do. Additional Command Button's Properties Add 1 Command Button to your form. and next to it the Y coordinates (For example.the text that appear on the button Font . 0) The coordinates of the Bottom Right button corner are (Button Width. and 2 if you've clicked the right one. "World" will print: Hello World). Figure 2 ToolTipText . Caption . The X and Y parameters are the X and Y coordinates of the mouse cursor.The Caption's font (Figure 2).Insert into this property the text that will appear when the mouse is stand still on the button (Figure 3). The coordinates of the upper left button corner are (0. Button Height) You can try a little example. Y This line will print the X coordinate.The Button parameter value is 1 if you've clicked the left mouse button. Put the following line in the Command Button's MouseMove event: Print X.

Graphical Figure 5 Picture . and the user will no be able to press on it (Figure 4). .The Picture that appear on the button (Figure 6). Figure 4 BackColor .Setting this property to "False" will make the button be gray.Enabled .This is the background color of the button (Figure 5). This property will take affect only after you will set the Button's Style property to 1 .

If you will set this property to "False".The Picture that will appear on the button when the button is being pressed.The Button X coordinate. Left . the Button will not appear on your form at run-time. Visible . Icon . Height . Additional Form's Properties The Form has many of the Command Button's properties (MousePointer.The Icon that appear in the form's title bar (Figure 7) and in the Task bar (Figure 8).The text that appear in the Form's title bar. BackColor. Hourglass. The mouse pointer will be the icon that you will select in the MouseIcon property. and more) when it will be over the button. Figure 7 . relative to the form left border. like in Figure 4).The Button height. Top . MousePointer . He also has additional properties: Caption .The Button Y coordinate.This property and all the following Picture related properties will take affect only after you will set the Button's Style property to 1 . relative to the form Top border.The Picture that will appear on the button when it disabled (When the Enabled property is "False".Choose here the mouse pointer (Arrow. DownPicture . Visible and more). and in the taskbar. If you will set the property to be 99-Custom.Graphical Figure 6 DisabledPicture .The Button width. Width .

The Form's KeyPreview Property To understand this property. ShowInTaskbar . WindowState . lets look on the following example: .Set this property to "False" to disable The title bar Minimize Button.Setting this property to "False" will cause the form not showing in the task bar. MinButton . Maximized.Set this property to "False" to disable The title bar Maximize Button.The initial appearance style of the form: Minimized. Figure 9 MaxButton .Set this property to "False" to remove the title bar's Close. or normal. Minimize and Maximize buttons (Figure 9).Figure 8 ControlBox .

Add the following code to your program: Private Sub Command1_KeyPress(KeyAscii As Integer) End Sub Private Sub Form_KeyPress(KeyAscii As Integer) End Sub Print "Button Pressed" Print "Form Pressed" The code above will print "Button Pressed" on the form when the Command Button's KeyPress event will be executed. To allow the Form's Key events be executed. Notice That the form's KeyPress event executed before the Button's event. Now we will learn how to do that during program's Run-Time. . KeyDown and KeyUp) instead of the form. Setting Properties At Run-Time By now you know how to set a component property before running your program (=at "Design Time"). It will get all the Key events (KeyPress. "Button Pressed" is appearing on the When the KeyPreview property is "False". The Form's KeyPress event hasn't been executed. run the program again and press any key. Lets try it. in addition to the Button's KeyPress event that been executed too. verify that the Form's KeyPreview property is set to "False". The Form's KeyPress event has been executed. form. Run the program and press any key on the keyboard.Start new project. if any control is found on the form (the command button in this case). set the KeyPreview property to "True". and print "Form Pressed" when the Form's KeyPress event will be executed. and add 1 Command Button (named Command1) to your form. Set the KeyPreview property to "True". but "Form Pressed" isn't appearing.

And how do you set a Form caption property at run-time? exactly the same: Setting Properties At Run-Time (Continue) Not all the properties get text values. To do that. the Command Button's Style property can get 2 values: 0 .To change a property. You don't have to use the quotes.Standard 1 . You can assign a variable value to a property: Dim MyVar As String MyVar = "Hello" Command1.Caption = "Hello" To test it. when you assign these values to property. and we want to set its Caption property to be "Hello". . suppose we have Command Button With the name "Command1". omit the string. For example. simply copy the line above to your Command1 Click event.String For example. use the following syntax: TheControlName.ThePropertyName = TheNewPropertyValue For example. Note that the Hello is inside quotes because it's a String.Visible = False Form1.Caption = MyVar This code will do exactly the same as the code line above it. Visible property can be "True" or "False" False and True are not text strings. but Visual Basic commonly used values that called "Boolean" Therefore. For example: Command1. Note that MyVar is without quotes. we will use the following code: Command1. run the program and click the button at run-time. because it's variable.Enabled = True Some of the properties values has the following syntax: Number .Graphical To set these properties value.

WindowState = 2 Some properties represent graphics. Figure 10 Then click on the Palette Tab (Figure 11).Style = 1 Form1. For example. The numeric value of the red color is &H000000FF& If you want to set the Form BackColor property to red.ico" to the Picture property of Command Button with the name "MyButton" MyButton.Caption = "Hello" Setting the BackColor Property You can set the BackColor property at run-time in 2 ways. The example below will load the ICO file "d:\games\toto. Figure 11 .ico")Form1. Way 1: Using the Color's numeric value Every color has numeric value.For example: Command1. To set these properties at run-time.BackColor = &H000000FF& You can find what is the numeric value of every color by making the following simple steps: Click on the BackColor property arrow button in the properties window (Figure 10). for example The Picture property.Picture = LoadPicture("d:\games\toto. You can simply assign this value to the BackColor property. use the LoadPicture Command. use the following code: Command1.

Figure 13 And once again. if you setting a Command Button's color. Figure 12 The color value will be displayed in the BackColor Property cell (Figure 13). what is Constant? . To learn the second way.Select your desirable color (Figure 12). All of this inSetting the BackColor Property (Continue) Way 2: Using the Colors Constants First of all. you'll have to learn a little bit about Constants. don't forget to set its Style property to 1 .Graphical.

vbGreen. a partial list: vbRed. For example.. The constant declaration syntax: Const MyVariableName = MyVariableValue For example. because vbRed = &H000000FF& Where are these constants came from? Visual Basic automatic declare them when the program starts.Constant is a variable that its value can not be changed. the following code is NOT allowed: vbBlue = 5 There are more Color constants. Imagine it like as the following code is automatic being entered to your program: Dim vbRed As Long vbRed = &H000000FF& Dim vbBlue As Long vbBlue = &H00FF0000& And so on.&H000000FF& Instead of writing in your code: Command1. the following code will declare a const variable with the name Piano and the (const) value "abcdef" . vbYellow You can declare your own constants. For example. the Constant vbRed holds the value of the red color . vbBlack. vbWhite. Constant holds a common used value. But with one exception: You can't change the constants value.. vbBlue.BackColor = &H000000FF& You can write: Command1.BackColor = vbRed The 2 statements above are identical.

go to the Conditional Statements Tutorial.you want to end the program.. the following code line will not be allowed: Piano = "gggg" Because it's been declared as a Const. and Integer Variables Store numbers. You ask from the user to enter the password.Const Piano = "abcdef" After this declaration. One of the basics of Conditional statement are Boolean variables. you have to use conditional statement because the code you will execute (let the user in or end the program) is depend on what is the password that the user has entered. For example: Dim Kuku As Boolean Kuku = True Dim YoYo As Boolean YoYo = False What are the True and False stand for? They are the result of every "Boolean expression". if not . String variables store text. the next page. the Print Piano code line will print abcde on the form. To learn more advanced programming techniques like conditional statements. and you have to understand them before continuing with conditional statements. Boolean variable stores one of the following constant values: "True". Boolean Expressions Boolean expression is like a question that the answer to it is "True" or "False" For example: Is 4 Plus 6 is 10? True Is 2 bigger than 4? False But the question . or "False". In addition. If the password is correct . Boolean Variables As we learnt. To do this.. What Are Conditional Statements? Suppose you want to protect your program with password. and Const value can not be changed.you want to let the user in. Boolean variables are commonly used in programming.

because its answer is 10 (and not True or False) Examples of Boolean expressions : Examples Of Boolean Expressions The following code: Dim ABC As Boolean ABC = (3 > 4) Print ABC Will print "False" on the form. . because the value of the boolean expression (A + 1 = B) is "True".How much is 4 Plus 6? Is not a boolean expression. In Boolean expressions you can use the following signs: = Equal to <> Not Equal to > Greater than < Smaller than >= Greater than or Equal to <= Smaller than or Equal to InputBox In the next examples we want to receive a value from the user when the program is running. because 3 is not greater than 4. The value of the boolean expression 3 > 4 is "False". because "abf" is equal to "abf" The following code: Dim ABC As Boolean Dim A As Integer Dim B As Integer A = 1 B = 2 ABC = (A + 1 = B) Print ABC Will print "True" on the form. The value of the boolean expression "abf" = "abf" is "True". The following code: Dim ABC As Boolean ABC = ("abf" = "abf") Print ABC Will print "True" on the form.

The InputBox command syntax is: VariableName = InputBox ("Text to Display") After executing this command. Your name will be printed on the form. Example: Put 1 Command button on your form and enter the following code to its click event: Dim Elvis As String Elvis = InputBox("Please Enter your name") Print Elvis Run the program and click the button. A Message Box is appearing with the text "Please Enter your name" (Figure 1). We want to prompt the user to enter the password .To do so we will use the InputBox command. Figure 1 Type your name in the text box. Conditional Statements The syntax of the conditional statement: If (boolean expression) Then The code to execute if the boolean expression is equal to True Else The code to execute if the boolean expression is equal to False End If Lets make a password protected program. and press OK. the variable will get the value that the user has entered.

the code that will be executed is the code that located between the Then and the Else (MsgBox "The Password is correct!") If this boolean expression's value is False. If this boolean expression's value is True. To end the program. An InputBox is appearing. Type "let me in" (without the quotes) in the InputBox. This command is simply shut down the program. the code that will be executed is the code that located between the Else and the End If (MsgBox "Incorrect Password!") If you enter a wrong password. a message box is appearing.at the very start of the program. and this happens when the Password variable value is NOT "let me in". Note that the text is case sensitive. Lets understand how this code works: The boolean expression (Password = "let me in") value is True ONLY if the Password variable value is "let me in". so put the following code in the Form_Load event: Dim Password As String Password = InputBox("Please enter the password") If (Password = "let me in") Then MsgBox "The Password is correct!" Else MsgBox "Incorrect Password!" End If Run the program. Put the End command in the "Else Section": Dim Password As String Password = InputBox("Please enter the password") If (Password = "let me in") Then MsgBox "The Password is correct!" Else MsgBox "Incorrect Password!" End End If Using "And" and "Or" In Boolean Expressions . but the program is continue running. use the command End . and if you will type "LET ME IN" you will get a message box with the text "Incorrect Password!".

In the last example we used the boolean expression Password = "let me in". If you'll type any other text the program will shut down. How can we make a boolean expression that will be "True" if the Password is "let me in" or "sam sent me". The "Or" operator is being executed on "True" or "False": False Or True = True True Or False = True True Or True = True False Or False = False The 4 examples above are the only options of using the "Or" operator. How the "+" operator works we already know: 5 + 6 = 11 But how the "Or" Operator works? The "+" operator is being executed on numbers: Number + Other Number = The sum of both numbers. and "False" If the Password is not. the boolean expression will be "False"? To create this boolean expression we will use the "Or" operator: (Password = "let me in") Or (Password = "sam sent me") Put the following code in your Form_Load event: Dim Password As String Password = InputBox("Please enter the password") If (Password = "let me in") Or (Password = "sam sent me") Then Password is correct!" Else MsgBox "Incorrect Password!" End End If MsgBox "The Run the program. and if the Password is other than the two above. Lets see what effect has the "Or" operator on the boolean expression we used: .We can use "And" and "Or" operators to make more complicated boolean expression. the same as "+" is operator. This boolean expression is "True" if the Password is "let me in". The "Or" is operator. If you'll type "let me in" or if you'll type "sam sent me" the password will be correct.

except it has different Effects on boolean expressions: True And True = True True And False = False False And True = False False And False = False When using the "And" operator. the left and right boolean expressions are being evaluated (Figure 2). Then the boolean expression is look like this: (True) Or (False) As we saw in the 4 examples above.(Password = "let me in") Or (Password = "sam sent me") First. Using "And" and "Or" In Boolean Expressions (Continue) The "And" operator is similar to the "Or" operator. if the Password is "let me in" then (Password = "let me in") is True and (Password = "sam sent me") is False. For example. the expression will be False if the password is NOT "let me in" and NOT "sam sent me" because False Or False = False. copy the following code to your Form_Load event: Dim UserName As String Dim Password As String . the expression will be True if the password is "sam sent me" because False Or True = True. Figure 2 (Password = "let me in") Or (Password = "sam sent me") True Or False True This boolean expression will be True if the password is "let me in" because True Or False = True. the expression will be True only if both boolean expressions are True. For example. True Or False = True So the final result of the expression is True (Figure 2).

because there is no "Else". Why? . I'll mention that you don't have to include For example: Dim Password As String Password = InputBox("Please enter the password") If (Password = "let me in") Then MsgBox "The Password is correct!" End If "Else" in the "If" statement. and any other combination is equal to False.UserName = InputBox("Please enter the user name") Password = InputBox("Please enter the password") If (Password = "let me in") And (UserName = "elvis") Then login is correct!" Else MsgBox "Incorrect Login!" End End If MsgBox "The This code will pop up two InputBoxes. If the password is correct. Using The "Not" Operator The "Not" operator has very simple function: Not True = False Not False = True Before we continue. Lets see an example of using the operator "Not". and (UserName = "elvis") is True. The first will ask you to enter the user name. The login will be correct only if both user name and password are correct. the code will do nothing. Otherwise. and the second will ask you to enter the password. The boolean expression (Password = "let me in") And (UserName = "elvis") is True only if (Password = "let me in") is True. Copy the following code to your Form_Load event: Dim Password As String Password = InputBox("Please enter the password") If Not (Password = "let me in") Then MsgBox "Incorrect Password!" End If End The boolean expression: Not (Password = "let me in") is True only if the Password is not "let me in". The code above will display a message box with the text "The Password is correct!". because True And True = True.

Not False = True. the expression value is True (Figure 3). The code above will display a message box with the text "Incorrect Password!" ONLY if the password is different from "let me in" CONDITIONAL STATEMENTS TUTORIAL LESSON 1 More Advanced Boolean Expressions You can combine the operators "Not". using the parentheses "(" and ")" For example: (True And False) Or (Not False) = True Figure 5 (True And False) Or (Not False) False Or True True The priority of the "Not" operator is higher than the priority of "And" and "Or" operators. For example: Not True And False = False. because The first operater to be executed is the "Not" (Figure 6): .If the Password is not "let me in". Figure 3 Not (Password = "let me in") Not False True In conclusion. (Password = "let me in") is False (Figure 3) . and we get the boolean expression: Not (False). so eventually. "And" and "Or" in the same boolean expression.

Not True = False. we would get WRONG answer (Figure 7). Figure 6 Not True And False False And False False If we will executed the "And" operator before the "Not" operator. Then we get the following boolean expression: False And False = False. Figure 7 Not True And False Not False True A Teaser: What will be printed on the form after executing the following code? Dim Elvis As Boolean Elvis = (Not False And True) And Not ((True And Not False) Or False) Print Elvis Answer: .

But what if we want to write more complicated conditional statements: If the password is xxx . If it's equal to False. the first boolean expression is being evaluated: (Number > 50) If it's equal to True then the line that following it (MsgBox "you are older than 50") will be executed.do something.do something else.do other thing. To do that we'll use "ElseIf": Dim Number As Integer Number = InputBox("Please enter your age") If (Number > 50) Then MsgBox "you are older than 50" ElseIf (Number < 20) Then MsgBox "you are younger than 20" Else MsgBox "your age is between 20 and 50" End If How does that code above work: First. and the "If statement" will be ended here. so the next boolean expression (Number < 20) will not be checked.do something else.do something else. and if the password is none of the above . we could program simple conditional statements: If the password is xxx .Back Forward Nested Conditional Statements Untill now. and if not . If the password is yyy . the line that following it . If the password is zzz .do something. the next boolean expression will be checked: (Number < 20) If it's equal to True.

and the "If statement" will be ended here. If the password is yyy . and if the password is none of the above . For example: Dim Name As String Name = InputBox("Please enter your name") If (Name = "elvis") Then MsgBox "your name is elvis" ElseIf (Name = "tim") Then MsgBox "your name is tim" ElseIf (Name = "john") Then MsgBox "your name is john" ElseIf (Name = "steve") Then MsgBox "your name is steve" Else MsgBox "I don't know you" End If Nested Conditional Statements Untill now. If it's equal To False. the first boolean expression is being evaluated: (Number > 50) If it's equal to True then the line that following it .do something. we could program simple conditional statements: If the password is xxx . and if not .do something.do something else. the "Else Statement" will be executed: MsgBox "your age is between 20 and 50" Only 1 of the lines will be executed! You can use more than one "ElseIf" in your "If Statement".do something else. But what if we want to write more complicated conditional statements: If the password is xxx . To do that we'll use "ElseIf": Dim Number As Integer Number = InputBox("Please enter your age") If (Number > 50) Then MsgBox "you are older than 50" ElseIf (Number < 20) Then MsgBox "you are younger than 20" Else MsgBox "your age is between 20 and 50" End If How does that code above work: First.do something else.do other thing.(MsgBox "you are younger than 20") will be executed. If the password is zzz .

so the next boolean expression (Number < 20) will not be checked. For example: Dim Name As String Name = InputBox("Please enter your name") If (Name = "elvis") Then MsgBox "your name is elvis" ElseIf (Name = "tim") Then MsgBox "your name is tim" ElseIf (Name = "john") Then MsgBox "your name is john" ElseIf (Name = "steve") Then MsgBox "your name is steve" Else MsgBox "I don't know you" End If .(MsgBox "you are older than 50") will be executed. and the "If statement" will be ended here. If it's equal to False. the next boolean expression will be checked: (Number < 20) If it's equal to True. the line that following it (MsgBox "you are younger than 20") will be executed. If it's equal To False. and the "If statement" will be ended here. the "Else Statement" will be executed: MsgBox "your age is between 20 and 50" Only 1 of the lines will be executed! You can use more than one "ElseIf" in your "If Statement".