AddThis Social Bookmark Button

Print

Introduction to VB.NET Windows Forms
Pages: 1, 2, 3

Bringing this to the previous code, we now have:



Imports System.Windows.Forms
Public Class Form1
  Inherits System.Windows.Forms.Form
  
  Public Sub New()
    InitializeComponent
  End Sub

  Private Sub InitializeComponent()
    '
    ' write your initialization code here
    '
  End Sub

End Class

Initialization code includes, for example, setting the form title or resizing the form. In VB.NET a Form's title is represented by its Text property. This property is inherited from the System.Windows.Forms.Control class. For the size, the Form class also has the Width and Height properties, which are also inherited from the System.Windows.Forms.Control class.

Bearing in mind that you refer to the instance of the current class using the keyword Me, you will have the following InitializeComponent sub that writes the form's title and resizes the form.

Private Sub InitializeComponent()
  Me.Text = "Developer Form"
  Me.Width = 400
  Me.Height = 300
End Sub
When you press F5 again this time, you will see a form with a width of 400 pixels and a height of 300 pixels. Still a blank form, but this time its title is changed to "Developer Form." (See Figure 2.)

Screen shot.
Figure 2: A Blank Form with Width=400 and Height=300

Adding Controls

Your form is not really useful without controls. The .NET Framework base type library provides controls that are ready to use. You have button, checkbox, textbox, label... you name it. If you are new to the .NET Framework, you might want to have a look at their object hierarchies. For instance, the Button control hierarchy is given in Figure 3.

Object
  MarshalByRefObject
    Component
      Control
        ButtonBase
          Button

Figure 3: The Button class hierarcy

All controls are indirectly derived from the Control class. Adding a control to a form requires three steps:

  • Dimension a variable for the control.
  • Instantiate the control.
  • Add the control to the form.

Because VB.NET variables have different scopes, it now does matter where a variable is declared. A variable declared inside a function is only visible from the function. If you want your control to be visible from the whole class, declare it in the class level.

Instantiating a control is like instantiating any other object; you use the New keyword. Once you have an instance of the control, you can add that object to the form. You can't add a control directly to a form, because the Form class has no such a method as Add(Control). However, the Form class inherits the Controls property from the Control class. In the Form class, you use this property to obtain the System.Windows.Forms.Form.ControlCollection object of the form. The Form.ControlCollection class represents the collection of controls contained within a form. You can use its Add method to add a control to the form and its Remove method to remove a control from the form. Understanding these three steps, adding a Button control to a form will involve code like the following:

'Declare the button
Dim Button1 As System.Windows.Forms.Button
'Instantiate the button
Button1 = New System.Windows.Forms.Button()
'Add the button to the form
Me.Controls.Add(Button1)

Of course, you can set the control's many properties to get a control that you want. You can set these properties before or after adding it to the form, but after the control is instantiated. A control has properties such as Left, Top, Width and Height for its size and position. A Button control also has the Text property that will appear as the text for the button object.

The code in Listing 2 brings a form to life and adds two controls, a Button and a TextBox, to the control.


Listing 2: Adding controls to a form.

Imports System.Windows.Forms
Public Class Form1
  Inherits Form
   
  ' Control declaration: a Button and a TextBox
  Private Button1 As Button
  Private TextBox1 As TextBox
    
  Public Sub New()
        InitializeComponent()
  End Sub
    
    
  Private Sub InitializeComponent()
        
        
    Me.Text = "Developer Form"
    Me.Width = 400
    Me.Height = 300
    
    Button1 = New Button()
    TextBox1 = New TextBox()
        
    Button1.Left = 200
    Button1.Top = 200
    Button1.Width = 100
    Button1.Height = 40
    Button1.TabIndex = 0
    Button1.Text = "Click Me"
        
    TextBox1.Left = 200
    TextBox1.Top = 30
    TextBox1.Width = 150
    TextBox1.Height = 40
        
    Me.Controls.Add(Button1)
    Me.Controls.Add(TextBox1)
        
  End Sub

End Class

Pages: 1, 2, 3

Next Pagearrow