The Football Scoreboard example

For the next few lessons we're going to go with a Football theme. As I write this it's NFL Draft season so, it's sort of appropriate. My only problem with football is that once the NFL starts it basically means that summer is over and, as much as I love football, I love summer even more!

Here I must apologize to all my millions of readers outside of North America who are wondering what the heck I'm talking about. I know that to you, football is played with a round ball that you hit with your head. Over here that's called soccer and even though it's growing wildly, it's still far behind the other sport. For the moment, to do the exercices, use rugby. I won't penalize you for it!

The first exercice we'll do is called "the Scoreboard". We'll create a scoreboard that will post the scores of two teams in a game. It's a very elementary program. It simply uses buttons to change the score whenever one of the teams does something.

All you have to know is that the game lasts 4 quarters and that each team can score 1, 2, 3 or 6 points on a play. (I know that you can score 2 points with a safety or a convert, but that doesn't matter here. In Canada you can score 1 point on a punt, too.)

To begin, download the sample code:
Football example

If you don't have a separate folder for your VB apps, create one.

Just extract the folder from the .zip file into your VBNetApps folder.

I'll just give you a quick rundown of how the app was created, with pictures, and then we'll use it to illustrate the next lesson. Eventually we'll improve it by adding subroutines, a timer, validations and so on.

Creating the form

As you can see I used a background image when creating the form. The image is a .jpeg file included in the .zip file. You can modify the image if you want but then you have to remove it and re-insert it in the form.

We simply create all the controls on the form.

It's important to give each control a (Name), as we'll see in the next lesson. The (Name) property will be used to identify each control in code. We use a system of prefixes to name the type of control - btn is a button, for example.

So, btnHome6 is the button clicked when Home scores 6 points.

The form will look something like this:

Writing the code

Then, we write code to get it to do something. You can Run the app at any time to test things as you go along.

The code will be explained in the next lesson.

The "Build error"

The first time you Run the program, it's possible you'll get a "Build error" message.

Before it can execute the program VB will compile the source code into an executable module. The "Build error" message means that it could not build the executable because there were errors in the code or something else.

The most common source of the error the first time the program is run is that the name of the form was changed from the default Form1, but the properties were not changed accordingly. So this is what you do:

Warning! Here's a common scenario: you run the program and it works fine. You continue writing code but you make a coding mistake. When you run the program again it gives you a "Build error" message and asks if you wish to continue. You say "Yes". The program will run. The problem is: it's not the latest version - it's the last good version you built, before you had a coding error.

Whenever you get a "Build error" message, the executable wasn't created. You have to correct your mistakes and re-build. Mistakes aren't hard to see - they're underlined in red in the code! Whenever there's something underlined in red, it's certain that you'll get the "Build error".

Visual Basic .Net for Rookies                vb .net