http://www.developer.com/

Back to article

Visual Basic Tutorial


November 1, 2002

Hello and welcome to the first part of our exclusive Visual Basic tutorial!

I'm your slightly geeky host, Karl Moore. Professionally, I'm a technology journalist and solutions developer from the United Kingdom.

If you choose to join me over the next six weeks, I guarantee to build you up into a Visual Basic professional, step by step. And I'm asking for nothing in return.

Well, except maybe a bit of feedback. And perhaps fame. And hey, if you happen to have a few hundred bucks just lying around...

In this introductory article, we'll be taking a straightforward look at Visual Basic, or 'VB' if you're one of the young, trendy, hip-hop programmer types, like myself. We'll be answering questions such as - who created Visual Basic? What's all the hype about? Why are some versions more expensive than others? Heck, we'll even get down to some juicy coding.

But first, let me reassure all the shy readers out there. Don't worry if you have no idea what VB is all about. I don't either! Errr, just kidding, I guess.

I've done the full-round with VB. I've sat the course, read the book, passed the Microsoft exams and shot the movie ("Three Forms and a Little Module"). And I won't be using complicated terms you don't understand - instead, I'll be simplifying difficult-to-swallow programming concepts with real-life analogies.

And don't worry; I won't be treating you like a kid. You know Windows, you know how to click and double-click. So that's fine. And I won't be spending hours droning on about any one item; instead, I'll cunningly send you off exploring the more interesting areas, weaving little tasks into the tutorial to ensure you gain maximum understanding with plenty of hands-on experience.

And if I ever get too technical or you need assistance, bring me back down to earth with an insulting e-mail. Alternatively, you can stop by the VB forums at www.VBForums.com.

Whoah, I'm blabbling too much already. So, without further ado, let's dive feet first into the wonderful world of Visual Basic...

It's been described as awesome, powerful and a "stunning new miracle". But enough about me. Let's introduce you to Visual Basic.

Why did MS create Visual Basic anyway?

It started with the Windows success story. We all know Windows is used on zillions of computers throughout the Universe and possibly beyond. One day Microsoft chairman Bill Gates had an idea - why not create a really easy-to-use programming language? In other words, one real big program you use to create smaller programs?

Gates started to think. Should he? Heck, it'd increase: (a) the popularity of Windows, (b) the number of programs on the market, (c) his bank balance. So he did... and it was christened Visual Basic. Everyone say "Arrrr...."

However the words "easy-to-use" in those golden days of Visual Basic 1.0 don't quite reflect the modern interpretation. An "easy-to-use programming language" meant you only required one Oxford degree before attempting to run it. But times have changed and Visual Basic has since graduated into the highly-respectable application it is today. In fact, it's the most popular programming language in the world. Ever.

If you don't have Visual Basic yet, get out your shopping list. It's advisable to purchase either versions five or six, the latest at time of writing. Why? The features in these releases will make your life much easier. Trust me, I'm a programmer.

But your decisions don't stop there - it's not as simple as walking into Staples and demanding your own copy. VB is available in four scrumptious flavours - Control Creation, Learning, Professional and Enterprise editions. I'd recommend either the Professional or Enterprise versions because they offer a significant number of vital advantages over the other two. I won't delve into the nitty-gritty right now - but if you're boring enough to want more information, visit http://msdn.microsoft.com/vbasic/

You'll probably notice a drastic price difference between Professional and Enterprise editions. If you're stuck in turmoil, go with the cheapest - Professional - unless you'll be getting real intimate with VB in a corporate environment.

But enough explanation, let's create our first program!

If you've not already installed Visual Basic, now's a really good time to do so. Otherwise this tutorial will be about as useful as a very large hole in a rather small head. Don't worry - it's pretty straight forward.

How to start your own groovy program!

To setup Visual Basic:

  • Insert the Visual Basic setup CD-ROM
  • Click Start
  • Click Run
  • Type in "x:\setup.exe", where 'x' is the letter of your CD drive
  • Follow the on-screen wizard until complete

Let's run Visual Basic now. The process of opening VB varies from version to version, but in general...

To open Visual Basic:

  • Click Start
  • Select Programs
  • Highlight the Microsoft Visual Basic or Microsoft Visual Studio folder
  • Click the Microsoft Visual Basic icon

After a few seconds of frantic whirring, you should be presented with a screen similar to the following:

If you don't have all the fancy icons I have, don't worry. It's not my fault you've been a real cheapskate and bought some cut-down, fell-off-the-back-of-a-lorry version. Ahem.

All those pretty little pictures depict the different types of programs you can create with Visual Basic. But right now, we're not bothered with all the frills - we just want to get our hands dirty with a little code!

  • Highlight "Standard EXE" and click Open

A mini-screen with a title of "Form1" should appear. This is the starting block for your application - the screen users will see when they finally run your program.

At the moment, your screen should look something like this...

Let's jump straight into creating your own little application!

To the left of your screen you should see a lot of little icons under the heading 'General'. These are called components - or if you want to be really geeky, ActiveX controls. Basically, they're "things" you can add to your program (technical description, that) - such as a button or picture. Let's see one in action...

So what are these 'things' I can add to my program?

  • Double-click on the component represented by a black 'A'

An item called 'Label1' should appear in the centre of 'Form1'.

This is an example of a component - albeit not very impressive for something that holds the title of "ActiveX control". But don't worry, it gets better. Trust me, I'm a programmer. Oh, I've done that joke haven't I?

To the bottom-right of your screen you should have a box entitled "Properties - Label1". If it doesn't say 'Label1', click on the label in the middle of your form.

The items you see here are "properties" of the label. They tell it how to work - what text to display, what colour it should be, whether it should have a border, and so on. You can easily change these by typing in or selecting new values from the list.

Let's try it now:

  • Click on the box to the right of the 'Caption' property
  • Replace the existing "Label1" value with "My First Wizzy Program"
  • Press Enter

Done that? Well done, you've just changed your first property! Did you notice how the label altered too?

Now:

  • Click on the label and hold your mouse down
  • Drag your mouse to the upper left hand corner of "Form1" and release the mouse button

The label should have moved. Smashing! If you're still following me, I'm really pleased - you're obviously a natural born VB'er. In fact, you should be writing this article.

Your screen should look something like this right now...

Let's go one step further. Double click on the button in the "General" toolbar. This is a Command Button - you'll find them in virtually all Windows programs, usually in the form of "OK", "Apply" or "Cancel" buttons.

Let's play around with this button...

  • Change its Caption property to "Click Me!"
  • Move down in the Properties list and change the Font to something wacky!
  • Resize the Command Button using the eight blue squares surrounding it

My Form looks like this right now...

Finally, let's add our first line of code:

  • Double click on the Command Button

That should bring up a pretty boring looking screen. This is where you "code", the most popular pastime among geeks and geekesses worldwide.

Your screen should look something like this...

Private Sub Command1_Click()

<Cursor flashing here>

End Sub

If it does, that's brilliant!

  • Now type in...
MsgBox "Welcome to my wizzy program!"

I'll explain what we're doing here. The "Private Sub Command1_Click()" tells Visual Basic to run this line of code when someone clicks on the Command Button called Command1. When they do, the line of code we tapped in runs - telling Visual Basic to display the designated sentence in a message box (MsgBox). The "End Sub" merely tells VB to stop running the code.

Simple as that.

Running your very first VB program...

Let's test run our program! Click on the (Start) button on the toolbar. Your application should start running in all its digital glory.

Click on your amazingly bright "Click Me!" button... and stare in amazement as your first line of code executes.



Click here for larger image

OK, OK. I know it ain't Microsoft Office - but you have to start somewhere!

This week, we've taken a lightning tour of Visual Basic. We've found out about the different versions, checked out a couple of neat 'controls' and even created our first mini-program!

Head geek Karl Moore sums up this weeks tutorial...

In the next tutorial, we'll chat about the other control widgets available to you, plus take a peek at even more code.

It gets a heckuva lot better. Trust me, I'm a progr... DOH!

But until then, I'm your eternally groovy yet ever-so-slightly-nerdish host, Karl Moore saying goodnight for tonight. Goodnight!

Next: Thirsty for more VB knowledge? Check out part 2 of the VB tutorial!

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!

Sitemap | Contact Us

Thanks for your registration, follow us on our social networks to keep up-to-date