Control properties in visual basic.net

CONTROL PROPERTIES

Introduction



Different controls have different properties.

Text property

Text property is want we see on the control as text eg Button1 This property can be changed using the properties window.

Exercise

open visual basic editor and select the project that we had. Once the project loads click on the Button1 to select it.

On the properties window look for text property and change it to “Equals” without the quotes then click on the form. True the text now is Equals on the button.

Name property

The name property is the name used when assigning values to or from a control.

Exercise

With our project still displayed click on the button once to select it with the button selected, select the name property in the property window and change it to Equalsbtn.

Exercise

with the form still loaded click on the textbox1 and change its name to Answer.

 

Text boxes

Text boxes are used to input data by a user or even to display output from a program.

Example

Dim number1 As Integer

number1 = TextBox1.Text

In this example we have declared a variable called number1 of type integer.

Then we have told vb that number1 will come from what the user enters in the textbox1.

TextBox1.Text = number1

In this case we are telling vb that the value that should in textbox1 is number1. note Textbox.text comes first.

 

To ensure that always a value is gotten from a textbox and not a text

This syntax is used

Val(Textbox1.Text)

Example

number1 = Val(TextBox1.Text)

 

 

Message box

Message boxes offers a quick way of displaying results

Syntax is:

 

MsgBox(number1)

 

We are  telling vb to display a message box showing whatever is in number1.

 

Labels

Labels offer a convenient way of labeling

Labels cannot be resized manual. The text that is typed(text property) in it determines its size.

 




EXERCISE

Let us try out this in the following exercise.

Create a new project. Once the form loads and two textboxes in the form

Add also a button to the form and change its text property to equals.

Also add a label to the form and change its text property to “+” and

Place it between the two textboxes. While the label is still selected locate the font property in the properties window and click on the default to change it to a bigger font.

control properties 1

You should come up with something like what is shown in figure  above. Note that the font property is what is circled in red in the properties window.

Now double click on the equals button and add the following code that is highlighted in red below.

Public Class Form1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Dim number1 As Integer

        Dim number2 As Integer

        Dim answer As Integer

        number1 = Val(TextBox1.Text)

        number2 = Val(TextBox2.Text)

        answer = number1 + number2

        MsgBox(answer)

End Sub

End Class

 

Run your program and while your program is running type a value in text box1 and another value in textbox2 as shown in the figure below, then click equals button.

control properties 2

Once the equals button is clicked a message box pops up

With answer for the addition of the two numbers as shown below

control properties 3

Click ok from this message box and try your program with other values in the text boxes.

The code we entered

The code we type was the following.

Dim number1 As Integer

Dim number2 As Integer

Dim answer As Integer

We were declaring three variables number1, number2 and answer of type integer.

number1 = Val(TextBox1.Text)

number2 = Val(TextBox2.Text)

 

we are telling vb to get number1 from textbox1 and number2 from textbox2.

answer = number1 + number2

we are telling vb to perform the addition of number1 and number2 and store it in the variable answer.

  MsgBox(answer)

We are telling vb to display a message box with whatever is in the variable answer displayed in it.