Setting and Adding Properties to Windows Form
There are three ways for setting properties to windows forms and its controls.
1. Using the Visual Designer
2. Programatically
3. Using Visual Inheritance
Using the Visual Designer to set Windows Form Properties
The properties of the Widows properties can be edited in the properties window visually. Both the
inherited properties and also those added in the current class can be edited using the property
window. If this window is not visible,it can be selected from view menu.
Setting Windows Forms Properties programmatically
If the value of the property of several items cannot be identified at design time and they have to
be set at run time based on several considerations. Visual Basic provides facility to edit and
initialize or substitute the value of the properties programmatically.
Step 1: Start a new project and add a form to the project. This form will be the starting form by
default. Add one text box and a command button to the form and place them as shown below:
Using Visual Inheritance
Public Class Controls
Dim lblFirst As New System.Windows.Forms.Label
Private Sub Controls_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Me.lblFirst.Visible = True
Me.lblFirst.Text = "Enter your Name"
Me.lblFirst.Size = New System.Drawing.Size(88, 24)
Me.lblFirst.Location = New System.Drawing.Point(110, 75)
Me.Controls.Add(Me.lblFirst)
Me.Controls.Add(Me.lblSecond)
End Sub
Private Sub btnOk_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnOk.Click
Dim firstNm As String
firstNm = Me.TextBox1.Text
addNames(firstNm, secondNm)
End Sub
Public Sub addNames(ByVal firstNm As String, ByVal SecondNm As String)
Me.lblFirst.Text = firstNm
Me.TextBox1.Text = ""
Me.lblFirst.Font = New System.Drawing.Font("Micorsoft Sans Serif", 8.25!,
System.Drawing.FontStyle.Bold, Drawing.GraphicsUnit.Point, CType(0, Byte))
End Sub
End Class
Inheritance allows you to derive one class from another class. Visual Basic provides a facility by
which, while deriving a class from another class, you can also inherit the visual aspects of the
class.
To derive a form from a Form1 which will inherit all aspects of Form1, including the controls on
the form. Let us create a new project VisualInheritance and add a form Form1 to it. Now add a
button botton1 to the form. Now click on the Build menu and click on Build VisualInheritance.
This will enable the form to be available for being inherited.
Now right click on the project and you choose Add Inherited Form. You will see a window named
inheritance picker. Choose the form Form1 in the window. The new inherited form will have the
controls that were placed on the base form Form1.