Visual Basic Tutorial - Part 6
In this section, we'll be answering a few top questions from the second tutorial. That week, we looked at controls and naming conventions
Q1. When I started my Visual Basic project, I got a standard 21 or so controls in my toolbox. Can I add more - or is that it?
Before I answer this question, let's take a couple of seconds to remember exactly what a control is. A control is something you see in the toolbox that adds functionality to your project.
For instance, a simple textbox is a control. Users can enter information into the textbox and you can then read from that textbox. You can even put text into the textbox, change its colour or make it invisible.
Another example of a control is the Picture control. You use this to display images in your application.
However the answer to your question is quite simple. You are not limited to the 21 items you see in your toolbox. There are indeed many, many more available. A fair number ship with Visual Basic, but you can also buy hundreds of third party controls too!
Try right clicking on a blank bit of your toolbox and selecting "Components..." from the popup menu. You'll see a long list of available controls. Simply ticking one and clicking OK will make it available to your project. Many of the extra controls that ship with Visual Basic start with the name "Microsoft". What an extraordinary surprise!
Just to prove my answer, here's my toolbox after working on an exceptionally tricky project...
Q2. How can I make a textbox invisible while my program is running then?
You can affect an object on your form by changing its properties. Don't forget - a property is simply that - something that tells you about an object. For instance, the Visible property will tell you whether your textbox is visible or not. You can also change this property in code.
For instance, you could add a command button to your form. Every time, the user clicked the command button you could run this...
If Text1.Visible=True then Text1.Visible=False MsgBox "Your textbox - Text1 - is now invisible!" Else Text1.Visible=True MsgBox "Your textbox - Text1 - is now visible!!!" End If
The first line checks to see whether the box is visible. If it is, it makes it invisible by changing the property. If Visible is not equal to True, Visual Basic moves onto the Else bit and makes the textbox visible!
In brief, to change the properties of a control in code, use the format:
ControlName.Property = NewValue
To change the property of a control at design time - ie, when you're not actually running your program - use the Properties window.
Q3. Karlos honey sugar. Is that photo of you real? Are you really soooo musclely?
Of course!! Do you honestly think I'd use a photo editing package (such as, I don't know - PhotoFantasy, #24.99 available from software shops near you) to give readers the idea that I'm a muscle-bound hunk - when I'm not? Tish, tish. Who do you think I am? Some kind of fraud?
Q4. Here's a question about those naming conventions. What if I don't know the naming convention for a particular control?
Naming conventions only exist to make your life easier. Let's pretend I have a form with a label and a textbox. The label may have a caption of "Date:" and I might want the textbox to hold a particular date.
Now, I obviously can't name them both "Date". If I do, I have the problem of distinguishing between the two different controls when I refer to them in code. Therefore, I call the label "lblDate" and the textbox "txtDate".
Naming conventions aren't enforced. They're simply there to help you recognise what kind of a control something is. If its name starts with "txt", I know it's a textbox. If it begins with "lbl", I know it's a label. And so on.
Page 3 of 7