Hello World: Your first applicationEdit
Let's switch back to design view with the button in the solutions explorer (IMAGE6). It is easy to create a new button for the program. Click the Toolbox button (IMAGE7). This opens up the toolbox:
Click and drag a button onto your window. It should appear in the window with the name 'Button1':
Every object you place on your window has a set of properties. You can view these by single clicking on an object and clicking the Properties pane (Image10). You may have to hunt around for this. Here are the properties of my button:
You will never use some of these properties, but some you will use all the time. The three most important ones are 'Text', 'Position' and 'Size'. Try changing these and see what happens. Note that Position and Size only accept numbers.
An event is something which happens which causes the computer to run a section of code. This could include clicking on a button, changing the text of a text box, moving the mouse across an image or any number of other things. Each event has a listener process which keeps waiting for the event to happen. As soon as it does, it tells the computer to run the appropriate bit of code. We'll make some of our own listeners later, but for now, let's use the ones built into Visual basic.
Press the lightning bolt icon to open the events window: (Image12). Here are the events for Button 1:
Click is the most common (default) event. To add the Click event to your program, double click in the text box next to 'Click'. The windows will change to enter Code view. Notice that it has added the words:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
The exact meaning of this is not important; essentially it the first line means "This is a bit of code called 'Button1_Click'. It starts here" and the last line means "Stop here if you're trying to run the Button1_Click code".
Let's add some code. The way you use a property in code view is by typing the name of the object (in our case, Button1), followed by a dot, followed by a property. You can change it by typing = and then the new value in "double quotes" So, let's type:
button1.text = "Hello World"
Your complete code should look like this:
Public Class Form1 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Button1.Text = "Hello World" End Sub End Class
Compiling your CodeEdit
Compiling is a process where Visual Basic converts your design and your code into an application which your computer can run. There are two stages to this; debugging and compiling.
Before it makes your program, it has to understand what you've typed. The compiler checks through what you've typed to make sure it makes sense. This is called debugging. If you make a mistake, it will underline it in the code in blue:
Here, I've left out the = symbol. The debugger has looked through the code and underlined it. It has also added a line to the errors list.
Once the code is all valid, it is compiled into a program. A program cannot be compiled if it contains errors.
To compile your code, hit F5, or press the Play (Image15) button. This opens up the program. Press the button and watch it change!