April 20, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

Visual Basic Tutorial - Part 2, Page 4

  • November 2, 2002
  • By Karl Moore, Karl Moore
  • Send Email »
  • More Articles »

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".

Now:

  • 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!





Page 4 of 5



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Sitemap | Contact Us

Rocket Fuel