deostroll
IT diaries...
Entry for January 08, 2007
VB.Net : Me.Controls

Today, I was looking at windows forms. Why forms? Isn't it something that can be created by default when you are writing a windows application in vb.net? Its very simple to do this in vs 2003. But the focus here is on oop. When you start a windows application project in visual basic, you will be provided a form. You drag or drop controls at design time, and attach code to their events in the code editor window.

Now try this: when you load a windows application project in vb.net, go to the code editor window, select everything, and delete.

Why the hell am I doing this?

VB.net is an object oriented; this means that everything is an object. This means the form is an object; the button that lies on the form again is an object; every control you can put on a form is an object. If you are familiar with object oriented terminology you would say that all objects must be initialized. The form is something that is initialized. And once that is done you are able to view it.

But strangely the form is the only thing that behaves this way. All the other controls can be initialized, but they don't appear on the form...

Now if you have been following this post really carefully you should be asking: then how is the button added to the form? But I guess the most important question is how is a button event linked with code?

The answer lies in the System.Windows.Forms namespace. A namespace is nothing but a logical name for related classes and datatypes. Most of the class definitions for controls in vb.net lies in this namspace. But they are hidden from the user. (For now I can live with that!).

If you want a windows form you have to load that namespace using the imports statement.
<Remember your code window is blank>

imports System.Windows.Forms

Next add your class. Make this class inherit the form; i.e. the class that has the definitions for the basic form you see.

imports System.Windows.Forms

Public Class MyForm: Inherits Form

End Class

Add a constructor.

imports System.Windows.Forms
Public Class MyForm: Inherits Form
Sub New()

End Sub
End Class

Since only one instance of this form is being created you don't have to worry about this constructor being private. Next how to add a button:

imports System.Windows.Forms
Public Class MyForm: Inherits Form
Sub New()
Dim btn As Button
End Sub
End Class

This will just create a variable of type Button. You have to instance it as follows:

imports System.Windows.Forms
Public Class MyForm: Inherits Form
Sub New()
Dim btn As Button
btn = New Button
End Sub
End Class

But this as you know won't make the button appear on the form. So now what do you do?

That is where the Me.Controls comes into play. It is a property of the form class. It has a method called Add() thats adds the control to the form thereby making it visible. You have to position and resize your button after this. Or you can position/resize your button before call this method; it depends on your wish:

imports System.Windows.Forms
Public Class MyForm: Inherits Form
Sub New()
Dim btn As Button
btn = New Button

btn.Top = 40
btn.Left = 50
btn.Height = 40
btn.Width = 100

Me.Controls.Add(btn)
End Sub
End Class


Now how do you display a message box on clicking the button This button was created at runtime. In vb design mode you have the properties window that takes care of this. You can give an alias name for a button that you drag and drop on the control using its Name property. But this property cannot be edited at runtime. That is just how the language is configured. Otherwise it would have been havoc. But since things are like that in vb you can simply double click on the button and write code for the default event that is triggered - the button click event.

Objects are responsible for creating events. Now if you want to create code that handles this object's event event you have to initialize this object with WithEvents keyword.

Dim WithEvents btn As Button

Here you have to know that the Button has a Click event. You now would want this event to be linked to some procedure (i.e. a sub). The Button's click event takes 2 arguments - a System.Object variable, and a System.EventArg variable. If these two arguments are not part of the sub we are about to code, it will show an error. The most important point here is linking this to an event that is raised by an object: this is done by the Handles keyword as shown in the following snippet. One thing to note here is that the above line of code is dimensioned outside as a private variable.

imports System.Windows.Forms
Public Class MyForm: Inherits Form
Dim WithEvents btn As Button

Sub New()
Dim btn As Button
btn = New Button

btn.Top = 40
btn.Left = 50
btn.Height = 40
btn.Width = 100

Me.Controls.Add(btn)
End Sub

Private Sub MyBtnEventCode(ByVal sender As System.Object, _
ByVal e as System.EventArg) Handles btn.Click
Msgbox("Button was clicked")
End Sub
End Class


2007-01-08 16:54:04 GMT
Hosted by www.Geocities.ws

1