12. VB Events

Now that we’ve created a GUI and named our objects carefully we can get into the actual programming required to get Tic Tac Toe to work. This immediately introduces us to the idea of Events.

Here is a picture of what the game should look like when we first start it:

You will notice there are a few differences between this form and the one we initially created. The big question is how did it change?!?

frmTicTacToe Events – “Load”
When we ran our program by pressing the play button:

…Visual Basic triggered an event known as “Load” which is basically code that will run as the program is being loaded. This is the perfect place to put any code you want to be executed before anyone sees your program.

In order to set up this area where we can place code we need to create that event. Visual Basic makes this process very easy, all you have to do is double-click on the object that you want to create an event for. Often this might be a button (which we’ll see later) but in our case it is for the form itself. So if you go to your design of the Tic Tac Toe game and double click anywhere on the form (it must be somewhere that doesn’t contain another object, otherwise you’ll create an event for that object), you should then be taken to a window like this:

Figure 1: frmTicTacToe_Load event

Now we’re getting into the real crux of programming!

Code vs. Design
Notice at the top of Figure 1 we have two tabs, one has the word “[Design]” next to it? These are the two facets of our program, the visual representation of our form and the code that makes it do something. You can get either of these windows open at any time by selecting which one you want to see from the “Code” or “Designer” button at the top of the Solution Explorer, i.e:

Public Class
In Figure 1 you might notice our first and last line. They are wrapping our code inside what’s known as a class. This is something you really don’t have to concern yourself with at this early stage of the programming journey. Simply note for now that those two lines are not to be tampered with or altered in any way, all the code you will play with and manipulate will be located inside those two lines. If you do happen to alter one of those lines, there’s a very good chance your program will not only stop working but you won’t be able to see all your work on the form itself! So be very careful with the first and last line of your code!

Private Sub … End Sub
We’re again referring to Figure 1 for this section, specifically the code Visual Basic has created for us that will be executed when the program is first started. You may notice that where I have “( ... )“, your code has a whole bunch of other stuff. Once again, at this early stage of your programming journey, you don’t need to be concerned with all that. You can’t change it…but you also don’t have to worry about it. The important thing to note is that anything you place between this particular “Private Sub” line and it’s associated “End Sub” line is executed when the “Load” event is triggered.

Okay, now we have an event that gets triggered but inside that event we have no code?!? Now its time to contemplate what might go in there…

Leave a Comment

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>