Mastering VB Properties
Article Index
Mastering VB Properties
Common properties
Working with properties
Example - Mortgage Calculator

Step Six - Getting properties

As you might guess from the method used to set a property it is very similar to the method used to find out what the current value of a property is.

For example, to discover where on a form a button called Button1 is you can use

t=Button1.Location.Y

or

t=Button1.Top

What this instruction does is to store the current value of the Location.Y or Top property of Button1 in a variable called t.

This, of course raises the question of what a variable is. Variables are an essential part of programming but for now you can you can think of it as something that can store values that you want to use later in the program.

The only complication is that you can't just use a variable. You have to state the fact that the variable exists and you want to use it. There is a way to avoid this but by default you have to "declare" the variables you are going to use so that storage space can be allocated to hold the data you store in them. Variables and data are big topics, not difficult but there are a lot of aspects that take time to understand and they are something that we will have to return to later.

For now all you need to know is that to declare a variable you have to use the Dim keyword the first time you use the variable. The reasons for the use of Dim is that it is short for Dimension and this is the word used in the first versions of Basic to declare variables of a particular type - more of this later.

So our earier command that retrieved the value of Top and stored it in a new variable t should have been:

Dim t=Button1.Top

Notice that you only declare a variable the first time you use it and if you try and declare it a second time the result is an error.

You can also use a properties value on both sides of the equals sign. For example

Button1.Width=Button1.Width*2

in the button's click handler results in a button that doubles its width each time you you click on it!


Step Seven - The text box

The most obvious use of properties is making the form and its layout look exactly as you want it to. In this role properties are mostly used at design time and set using the Property window or by direct manipulation.

However properties have another, more important, role in life. They are used to allow the user to provide input to your programs and generally to interact with it. In this role properties are generally used at runtime.

This brings us to the first of the many additional user interface or UI "controls" we need to know how to use: the TextBox. The most important property of the TextBox is Text which is simply the text that it displays or the text that the user types in.

That is when the program is running the user can type into a Textbox and whatever they type changes the value of the Text property. In other words, after the user has finished typing what they have typed can be obtained from TextBox1.Text.

 

textbox

 

Notice that the Textbox tool is the one that looks as if someone is typing into a box and not the RichTextBox which does a similar but more sophisticated job.


Step Eight - Showing output

The text box can be used to get user input but how to you show the results to the user?

One way although not always the best is to use the Label control. This too has a Text property that can be set either at design or run time. In most cases you use the Label control to provide labels on the form but it can be used to display results simply by setting its Text property at run time.

A Label object cannot be modified by the user in the same way that the TextBox can, there is nothing stopping your program from changing its Text property.

For example,

Label1.Text=100*10

will work out 100 time 10 and store the result in the Text property of the Label1 object. The result is that the user will see the value 1000 appear while your program is running.

This technique can be used to display any results that the user needs to see. Notice that an alternative way of displaying results is to use a TextBox - but then you have the problem that the user can modify what it displayed. There is an argument that a TextBox should be used for input and a Label for output.

<ASIN:0470502223>

<ASIN:0521721113 >

<ASIN:0136113400 >



Last Updated ( Thursday, 07 April 2011 )
 
 

   
RSS feed of all content
I Programmer - full contents
Copyright © 2014 i-programmer.info. All Rights Reserved.
Joomla! is Free Software released under the GNU/GPL License.