Page 1 of 4
The key to writing Visual Basic programs is to understand the ideas of objects, events and properties.
- Objects are the visible "things" that you place on a form using the tools from the toolbox.
- Events are things that objects can respond to.
- Properties are quantities like colour, position and size that apply to objects.
In the first chapter of Mastering Visual Basic 2010 we looked generally at these three ideas.
Now it is time to home in objects and properties - although it is impossible to completely ignore events!
Step One - The properties window
When you place an object on a form it comes complete with a set of properties.
You can see an object's properties listed in the Properties window. If it isn't visible then just press F4 to make it appear. The Properties window always displays the properties of the object that is currently selected - so you can use it to browse around the object that are already on a form.
The list of properties is usually very long and you may need to scroll through the list to see all of it. Don't worry, most of the properties in this list will rarely concern you and most of them have an obvious meaning.
Notice that right at the top of the properties window the object's name and its type are displayed in a drop down list. You can click on the arrow at the side of this list to produce a list of objects that you are currently using and select the one that you want to work with.
Usually it is easier to select the object using the mouse.
Step Two - Design time changes
Properties can be changed while you are designing the layout of the form. They can also be changed as a program is running - exactly how will be described later.
Some properties can only be set at design time or at run time and this is another minor complication we need to look out for. To change a property at design time you can sometimes simply directly adjust the object on the form.
For example, to change an object's size or position properties you simply drag or resize the object using the mouse.
You can also set properties using the Property window. All you have to do is select the property in the list and then type in its new value in box next to it.
Step Three - Name and Text
All objects have a unique Name property which is used to refer to them - this is always displayed near the top of the Property window and always displayed in brackets (Name).
Some objects also have a Text property which is the text that they display on the form. The Name and Text properties usually start off set to the same value and this often causes confusion between the two.
For example, when you create the a button it is given the default Name and Text "Button1".
While you might be happy to leave the button's Name at Button1 you will usually want to change the Text to OK, PRESS ME, CANCEL or whatever message you want to appear on the button.
In most cases you will edit the Text to change the text that an object displays. You can do this by selecting the property in the Property window or by typing while the object is selected - Visual Studio assumes that you want to change the text property in this case.