Microsoft & .NETVisual BasicVisual Basic Tutorial - Part 2

Visual Basic Tutorial – Part 2 content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More.

Welcome to this second instalment of the world’s simplest Visual Basic tutorial!

I’m your amazingly famous and astonishingly handsome host Karl Moore and once again I’ll be showing you the digital ropes to VB success, simplifying difficult programming concepts with easy-to-swallow analogies.

If you haven’t read the first part yet, get yourself acquainted with VB at tutorial 1.

Today we’ll be taking a deeper look at the built-in “controls” you can add to a Form and then get all geeky with terms such as “runtime” and “naming conventions”. Eugh, yuck!

Right, let’s code geek-to-geek, baby…

In the last tutorial, we built a supercool program with just a couple of simple controls. Remember that a control is just an item you can add to your Form.

As an example, last week we added a Command Button. We could then change its properties – perhaps alter its Caption to "Click Me You Annoying Person!" or its Font property to Arial, Bold, Size 18.

But the breadth of controls doesn’t stop there. In fact there are a whole plethora of additional widgets at your fingertips with Visual Basic. And in this section, we’ll be getting intimate with ’em!

Open Visual Basic and create a new Standard Exe. To do that:

  • Click on the Standard Exe icon
  • Click on the Open button

To the left of your screen should be the control toolbox. Right now it’s displaying a standard set of Visual Basic controls.

Let’s play with them! Double click on the button. This is the Check Box control. It should now appear at the centre of your Form.

You’ve probably seen this used in many other programs. You often check and uncheck such boxes to set various options. And yessiree, now you too can easily add such functionality to your applications.

Take a glance at the Properties window. These are all the Check Box properties you can change. For instance, you can alter its Caption, Forecolor or Font property with a few clicks of the mouse!

Don’t worry about all the properties. Some are hardly ever used or only required in very specific circumstances. With practice, you’ll soon get acquainted with those you need to know.

For now, go about randomly changing a few properties and seeing how they affect the control. Don’t worry, you can’t break anything.

Here’s how I got on…

Hmm, you don’t look very impressed. In fact, I saw more excitement emitting from the slug I accidentally sat on last Wednesday evening. But it does get better… honest.

It’s now time to explain a few of the other available controls available and exactly what they do. Then I’ll leave you to your own devices – feel free to fiddle to your hearts content!

Using more built in VB controls.

Image – This control allows you to display a picture on your Form. Perhaps an image of yourself, the supercool programmer! Or maybe your cat. Or dog. Or a rare elephant from the invisible forests of Outer Mongolia. Or whatever. Try playing with the Picture and Stetch properties

Label – You often use this control to display information to the user, such as an explanation of what they should do. Try playing with the Caption and Font properties

TextBox – The TextBox control is a little like McDonalds; common, usually trouble-free and definitely overused. You often utilise this control to collect information from the user or display data from a database. Try playing with the Alignment, BorderStyle, Font, MaxLength, MultiLine, PasswordChars, ScrollBars, ToolTipText, Text and Visible properties

CommandButton – This is a pretty boring yet very common control. As Black Adder would say, "It’s about as exciting as getting an arrow through the neck then realising there’s a gas bill attached". It simply creates a button the user may click to run a slot of code. Try playing with the Cancel, Caption and Default properties

DriveListBox – Whilst this control doesn’t have many properties you can fiddle with, it’s great to observe. You’ve seen this famous drop-down list many times before and now it can be incorporated into your own cool program! Try playing with the related DirListBox and FileListBox controls

Shape – This control draws a shape on your Form. Hmm, not very exciting but try playing with the Shape, FillStyle and FillColor properties for a wild time. Oh yes.

Yikey, what a list! Don’t worry about remembering them all, just observe the description of each property just below your Properties window – and test them by running the application, or “project”.

Don’t forget how to run an application:

  • Press F5 (or click the button)

When designing an application, remember that you’re an artist. The Form is your canvas and the controls, your paints. Just throw them all together and hope for a Van Cough, like my own:

Blow Up!

Hmm, well OK, it won’t win any (major) design awards but it only took me about three seconds. In fact the results of my application design exam were so low they were last spotted by Alice in Wonderland shortly after she pondered, "I wonder what’s down that hole?"

What’s that? You really don’t care? Oh, no problem. We’ll save reader-writer bonding for later.

Before you skip the rest of this section, I have a few final words of gizdom. Every single control you add to a Form has a Name property – it should appear at the very top of the Properties window.

Every time you add a control, an automatic name is assigned – such as Command1. That’s not very neat and professional geeks get pretty upset if you don’t bother following their little rules.

So, let’s try to keep them happy. Every time you add a control (I know, I know… it’s a hassle!) rename it with a more English address. For instance, if you have a Text Box that will hold a date, call it "Date".

But hold on. It gets worse. The Visual Basic community also have their own "naming conventions" where, for instance, Text Box names are preceded by "txt".

Therefore if you had a Text Box that will hold a date, it’s advisable to change its name to "txtDate". Other prefixes include:

ComboBox – cbo (eg cboProductCodes)

CommandButton – cmd (eg cmdOK)

Form – frm (eg frmOrders)

CheckBox – chk (eg chkExtraCheese)

OptionButton – opt (eg optPlanTwo)

That’ll do. Even I can’t remember them all!

Top Tip: You can find a list of more naming conventions than you can wave a very large stick at here.

It’s advisable to keep names pretty unique. There are circumstances when you may want more than one control with the same name, but we’ll save that for another tutorial. Right now, stick to one name per control.

During the next section, we’ll see how this name allows us to control a control programmatically! Try saying that at 3am on a Friday night. After four pints of Guinness. And an extra spicy burn-your-mouth-out curry.

Top Tip: Be sure to check the Controls.vbp sample that ships with the more recent versions of Visual Basic. This demonstrates the neat features of most standard controls. You might also be interested in John Percival’s article on how to design your application – particularly the section on Tab Order.

Writing creative code is easy! Let the head geek explain all…

Before I dive into this next section, let me first air a few geeky terms. Firstly, programmers often refer to "design-time" and "runtime". Why? Because they’re computer nerds and strive on acronyms and technical sounding words.

You see, in reality, these terms hold very simple definitions. Design-time is the period when you’re designing your Form or writing code – basically any time your program isn’t running. And runtime, as its amazingly original name implies is exactly the opposite – the time when your program is running.

You’ve already seen how you can change the properties of controls whilst you are in design-time. But what if you want to change such properties whilst the program is running? For instance, perhaps you want the Caption of your Form to change depending on which button the user clicks. Let’s try it!

  • Open Visual Basic and create a Standard Exe
  • Change the Name of Form1 to "frmTest" and the Caption to "My Test App!"
  • Add two Command Buttons and change the Caption of the first to "Hayley" and the second to "Roy"

Hope you’re following me. Don’t forget that you can add any control to your Form, including Command Buttons, by clicking once on the item in the toolbox then dragging it out on your Form. You can easily change the properties of such controls by altering values in the properties window.

Righto, you now have two buttons with the captions "Hayley" and "Roy". Let’s change a few properties before continuing:

  • Change the Font property of each to something really jazzy
  • Now change their Name properties to something sensible, such as "cmdHayley" and "cmdRoy".


  • Double click on the Hayley-button

The Visual Basic code window should appear, looking something like this:

Private Sub cmdHayley_Click()End Sub

Your cursor should be flashing between the two lines. If not, your computer caught the CIH virus last Tuesday and will dramatically keel over within the next twenty-four seconds. Try reinstalling Windows 98. Err, just joking. I’ll continue before Ed pulls the article…

Now we’re going to tap in some code:

  • Type the following:
frmTest.Caption = "You clicked Hayley - and she isn't happy!"
  • Redisplay the Form by double-clicking on frmTest in the Project Explorer window to the top-right of your screen

  • Double click on the Roy button and type in the following code:
frmTest.Caption = "You clicked Roy - and he likes it!"

Hope you followed all that. Let me explain what’s happening with, say, the Hayley button. The following code will run when the Click event of the cmdHayley control is activated. In other words, whenever you click cmdHayley, this code will run.

Private Sub cmdHayley_Click()frmTest.Caption = "You clicked Hayley - and she isn't happy!"End Sub

You’ve seen the first and third lines before. Allow me to explain the middle portion. And let’s admit it, we all love a portion.

In essence, the second line simply changes the property of an object during runtime. In this instance, the Caption property of frmTest is changed to “You clicked Hayley, etc”. Simple, eh?

That basic syntax can be used to change virtually all properties. It goes something like this:

ObjectName.PropertyName = Value

If the value is a number or true/false, don’t bother enclosing in quotation marks. For all other values however, surround with "these things".

Anyway, enough theory. Try running your application and clicking on the Hayley and Roy buttons. See what happens? Groovy or what?

(Ed: I’ll go for a ‘What’, Bob…)

Play around with setting properties in code. Explore the list of available properties that pop up after entering the period (‘full stop’) following the object name. Try experimenting with the following:

frmTest.BackColor = vbGreencmdHayley.Caption = "Click Hayley?"cmdRoy.ToolTipText = "Hover Your Mouse Over Me!"

Note: Why isn’t vbGreen surrounded by "quotes"? Good question, because it certainly is text. However in Visual Basic, the word vbGreen actually represents 65280, the ‘number’ for green. It’s known as a constant and exists to help you forget about five-digit numbers, like the one above. Try playing with vbYellow and vbBlue, too!

Karl Moore sums up this week’s tutorial.

This week we took a deeper look at the controls available to Visual Basic developers, then fiddled with a few properties.

We went on to figure the difference between design-time and runtime – and changed a few control properties in code. And heck, even if you didn’t enjoy it – I’m sure Roy did.

Before next week, why not have a serious tinker with Visual Basic – using the knowledge you’ve gained so far. Build a few sample programs and play around with properties. It’s such trial and error that’ll turn you into a VB genius, if you’re not one already.

Try opening the samples provided with Visual Basic and taking a look at their design. Don’t worry if you don’t understand it all just yet, simply observe how controls are arranged on a Form and how some of the code changes their properties.

You might also want to try creating your own little test program and then saving it. Take a look at the files Visual Basic creates. Notice how there isn’t just one, but numerous different files?

Next week, we’ll be getting to grips with variables – plus, we’ll be writing our very own password-checking program. Calm yourselves, fellow VB’ers, calm yourselves!

But until then, this is your stunningly stunning host, Karl Moore, saying goodnight for tonight. Goodnight!

Karl’s Visual Basic Tutorial Index

Visual Basic Tutorial
Visual Basic can be confusing, especially for newbie programmers. But don’t worry – here to help is technology journalist and head geek Karl Moore, with the first installation of his up-and-coming Visual Basic tutorial!

Visual Basic Tutorial – Part 2
Karl Moore returns with the third instalment of our exclusive no-geek-speak Visual Basic tutorial. This week, Karl gets his hands dirty with variables and conditional logic, so get out your nerds dictionary!

Visual Basic Tutorial – Part 3
Karl Moore returns with the third instalment of our exclusive no-geek-speak Visual Basic tutorial. This week, Karl gets his hands dirty with variables and conditional logic, so get out your nerds dictionary!

Visual Basic Tutorial – Part 4
Karl Moore gives you the buzz about looping, grabbing information from users and subs, as always without geek-speak!

Visual Basic Tutorial – Part 5
Karl Moore returns with another geek-speak free guide to Visual Basic coding. This week, Karl explains what methods, events and properties actually are, as well as delving into the enigmas that are functions. Lets code geek-to-geek baby!

Visual Basic Tutorial – Part 6
Head geek Karl Moore returns with the final instalment of his popular jargon-free VB tutorial!

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Latest Posts

Related Stories