previous page main page next page
  • in the Project window, click the View Code icon - if the icon is greyed out, check that the form, frmHello, is highlighted in the window
     

If you click the arrow in the left hand box at the top you should see something like this:
 


 

This is where the code for each of the controls is going to go. You don't have to write code for every control, only the ones where an event is going to occur. An event occurs, for example, when someone clicks a button or a checkbox, or when someone types text into a text box. In other words, an event is when the user does something, or interacts, with your program. Visual Basic is an event-driven language and your job as a programmer is to consider what events might occur and how you want your program to respond when they do.

  • click on cmdExit and type in a line of text so that you have this:
     


 

There now, didn't we tell you that programming was easy? This is the code for the Exit button. To be more specific, it's the code for the Click event of the Exit button. Notice that in the right hand box at the top it says Click and if you click the black arrow you'll see a list of other possible events associated with the cmdExit button.
 

  • run the program and click the Exit button to check that it works
     

One of the properties of the txtMessage text box is called Text and, if you remember, we deleted any text there so that it was blank. It is possible to alter that property while the program is running and, indeed, that's exactly what we want to do when the user clicks the Display button. So we want the Click event of the Display button to alter the Text property of the text box control. Here's how it goes...
 

  • click the View Code icon and then select cmdDisplay
  • type in the code to get this:
     


 

The line of code

txtMessage.Text = "Hello World!"

is the one that sets the Text property of the txtMessage text box to "Hello World!". In other words, it displays your message.
 

  • run the program and check that it works
     

So far, so good. We'll now add code to the Clear button whose job is to clear any text from the text box...

  • add the code to the cmdClear Click event:
     


 

This time, the code

txtMessage.Text = ""

replaces whatever was there - your message - with whatever is between the quotes; in this case, nothing!

At this point, congratulations are in order because you've just written your first Visual Basic program!
 

previous page main page next page ©  ePublish Scotland  1999