- Creating a Program ... in a Nutshell
- Step 1: Creating the User Interface
- Step 2: Adding the Program Source Code
- Step 3: Creating Your Program's Executable File
Step 2: Adding the Program Source Code
You now have a button in a form, which is all fine and good except for the fact that the button does nothing. Don't believe it? Click the Start button (the button that looks like a small, blue triangle) on the Visual Basic .NET toolbar (not the toolbox). (When your mouse pointer is over the right button, a little box appears containing the word "Start.") When you click the Start button, Visual Basic .NET runs your modest little program. You'll see a small window, as shown in Figure 3.7.
Figure 3.7 Your first Visual Basic .NET program.
Go ahead and click your program's button. Nothing happens, right? OK, so the button looks like it pops in and out. All buttons do that. What's important to notice is that clicking the button doesn't make the program do anything useful. Of course, you're about to fix that little snafu.
Close your program's window, and get back to the Visual Basic .NET main window. Now, double-click your form's Button1 button. Presto! Your project's code window pops up, as shown in Figure 3.8.
Figure 3.8 The code window appears whenever you double-click an object.
When you double-clicked the Button object, Visual Basic .NET not only displayed the code window, but also started some program source code for you. This piece of source code is called a procedure. To put it simply, whenever the user clicks the Button1 button, Visual Basic .NET goes to the Button1_Click procedure and performs all the commands the procedure contains. You can also think of Button1_Click as an event, which is an action that Windows passes to your program for handling. When you ran the program previously, and clicked the button, Visual Basic .NET did exactly what it was supposed to do and went to the Button1_Click procedure. Unfortunately, you hadn't yet placed commands in the procedure, so nothing happened.
You can change that easily, by adding the source code line
MessageBox.Show("Visual Basic rocks!")
to the Button1_Click procedure, as shown in Figure 3.9.
Figure 3.9 You type your own program code between the start and end of a procedure.
Now, run the program again (by clicking the Start button). When you click the button this time, a message box appears with the message "Visual Basic rocks!" as shown in Figure 3.10.
As you may have already guessed, the MessageBox.Show command tells Visual Basic .NET to display a message box. The text in the quotes after the MessageBox.Show command is the text the message box should display. You'll see the MessageBox.Show command a lot throughout this book.
Figure 3.10 Now your Button1 button actually does something.