Standard Controls in Visual Basic

Hello, and welcome back to our weekly series where we explore Visual Basic. Last week we looked at the environment in which we will be working. This week, we will discuss a few of the common controls that are installed automatically when you install Visual Basic. These include, but are not limited to the controls in the Toolbox.

We’ll discuss the controls in the Toolbox first. If we start at the upperleft, we see the pointer.

The pointer is not really a control. It basically is for moving or resizing objects on the form, dragging new objects from the toolbar to the form, and provides no other real functionality.

Right next to it is the PictureBox.

The Picturebox can be used to display graphics within your application. Graphics can be of various formats, sizes, etc.

The next control is a Label.

Labels are used to display static text in an application. With static, I mean that while the application is running, the text cannot be altered. (Well, technically it could be, but I can’t really see why anyone would do that).

There’s also the Textbox.

The Textbox is an inputbox. It can be used to gather information from a user (such as a filepath, or a name), or to display this information (Although that’s usually what labels will be used for).

Right under the label is the Frame.

Frames are used to group certain controls together. This can be both for functionality , or to make sure that a user will recognise that controls are related.

Next to the frame, we will see a control everyone has seen before: The Command Button.

A command button is used to make a user acknowledge to the program that he wants something to happen. Examples include submitting information, cancelling a process, or exiting a program.

The next control is the CheckBox.

A CheckBox has two values. Checked, or unchecked. Quite simple, huh? If you add multiple checkboxes together (For instance grouped in a frame), every single CheckBox can be either checked or unchecked.

Right next to the CheckBox is the OptionButton.

An OptionButton is similar to a CheckBox in that it can be either ‘checked’ (With an OptionButton, this is called ‘True’) or ‘unchecked (False). However, if grouped together, only ONE of the OptionButtons can be true. The OptionButton that previously was true will be false as soon as a new OptionButton is set to True.

Under the CheckBox you will find the ComboBox.

A ComboBox holds a list of items. It basically shows up as a pull-down menu. A user could select a single value from within the list. An example is when you’re asked to fill out which country you’re from online. You click on the pull-down menu, and will be offered a list of countries to select from. Users can also enter data themselves. (This depends on the style you define for the ComboBox).

Lastly, there’s the listbox.

A listbox ALSO lists items, but it will show more than one. It can be scrollable, and users could select multiple items from it at the same time. It’s just a different way of presenting data than the ComboBox.

For the sake of getting on with the article, I’m going to discuss just ONE more control. The other controls are ones that you won’t be using as much, anyway, and I want to delve a little deeper into the whole controls.

The last control we’ll discuss this week is the Timer.

A timer can be used can be used to make the application deal with pre-set intervals. This might sound vague, but we will see how it works soon enough.

Alright. So now we know what a few of the most-used controls can be used for. A control consists of a lot of back-ground code though. Let’s take a look at what makes up a control then.

We’re going to use the CommandButton as an example. Double- click on the Commandbutton icon in the Toolbox, and you’ll notice a button being added to the form (In the Form Designer).

By default, a control you add to your project will have a name assigned to it by Visual Basic. For a CommandButton, this name is Command1. You will also see that on the Button itself, Command1 will be showing. Hmmm. Doesn’t quite look right, does it? Let’s play around with the CommandButton’s Properties a bit.

Click the button, go to the Property Window, and scroll down to ‘Caption’. Caption is the text that will be shown on the button. If you look at Caption, you will see it’s current Value is: “Command1”. Let’s change that to “&Click Me”. (The Ampersand (‘&’) makes sure that the letter after the ampersand will be underlined. Looks neat, but doesn’t do much, does it? That’s where the second part of the Ampersand comes in. If a letter is underlined, you can hold down the ALT-key, and if you press the underlined letter (In this case: ALT+C), the program will act as if you clicked the button itself).

You’ll notice that while you are typing the caption, the button on the form will change it’s text to reflect whatever the current value for Caption is.

Another property of the CommandButton is ‘Backcolor’. Currently, the button’s BackColor’s value is set to: ‘&H8000000F&’. This seems like a real obscure value, but it’s just an internal number Windows uses for colors. Luckily, you will see the color matching the value on the left of the number. On the right you will notice a little triangle that points down (A combo-box). If you click on it, you will see a few pre-set values. The values you are presented with, are dependant on your color-scheme in windows. If you use the values offered in the ComboBox, you can make sure your application will be consistent with the color-schemes as the user always sees them. If not, you can still use a different color to enforce your own color scheme. Mind though that people have different tastes, and not everyone shares the idea that pink background with bright yellow text looks neat.

For now, change it to Desktop. (Notice you can pick your own color by clicking on the ‘Palette’ tab after folding out the ComboBox). You’ll notice that nothing happens. Hmm… What went wrong here?

Actually, nothing went wrong. Buttons are just meant to stay the way they are (color-wise), except for when another property is changed. That propety is ‘Style’. So locate Style, and select the dropdown list. You will see two options:

# 0 – Standard

# 1 – Graphical

Pick Graphical, and the color of the button will change to the background color of your desktop. Quite neat, huh?

There’s another way to change the style. Double click on ‘Style’. You’ll notice the value for Style changes again.

Well, that’s it for this article. Next week we will delve a little deeper into the properties, and use a few controls to build a small application. Untill then, feel free to fiddle around with the properties for various controls a little. That’ll get you known with their options. Be sure to check out the descriptions for the properties as well.