Full Version: Build a control without using the GUI Designer
Dream.In.Code > Programming Tutorials > VB.NET Tutorials
AdamSpeight2008
Up to now you've probably being designing controls by dragging the from the toolbox and dropping them on to a form.
No more!
I'll show you how to achieve the same only using code.

vb

Public Class Form1
Dim myButton1 As Button

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
' Create s new button, associating it with the variable myButton1
myButton1 = New Button

We just created a new button object.

vb

' Assign it some properties
With myButton1
.Left = 0
.Top = 10
.Text = "Press me"
.Width = 100
.Height = 100
.BackColor = Color.White
End With

Assigned it some properties.

Next is giving it the power to do and react to stuff.
vb

' The following tells the control which peice of code to use the that event.
' Format: {Control Name}.{Event Name}, AddressOf {Routine name}
AddHandler myButton1.Click, AddressOf Clicked_On_MyButton1
AddHandler myButton1.MouseHover, AddressOf MouseOver_MyButton1
AddHandler myButton1.MouseLeave, AddressOf MouseLeaving_MyButton1
' Add it to the form
Me.Controls.Add(myButton1)
End Sub

The line Me.Controls.Add(myButton1) places it on to the form.

Add in the procedure to process the events.
vb



Private Sub Clicked_On_MyButton1(ByVal sender As System.Object, ByVal e As System.EventArgs)
' The user has click on mybutton1, do something
MessageBox.Show("You clicked on myButton1")
End Sub

Private Sub MouseOver_MyButton1(ByVal sender As Object, ByVal e As System.EventArgs)
' mouse is over myButton1, do something
myButton1.BackColor = Color.Blue

End Sub
Private Sub MouseLeaving_MyButton1(ByVal sender As Object, ByVal e As System.EventArgs)
' The mouse is leaving myButton1, something
myButton1.BackColor = Color.White
End Sub
End Class


You may have noticed the difference between gui control code and coded.
vb

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

End Sub

The coded control is missing
Handles Button1.Click

Now you can create controls with using the designer. (Code Ninja Style ph34r.gif )
mineeric123
QUOTE(AdamSpeight2008 @ 27 Jul, 2008 - 12:53 PM) *

Up to now you've probably being designing controls by dragging the from the toolbox and dropping them on to a form.
No more!
I'll show you how to achieve the same only using code.

vb

Public Class Form1
Dim myButton1 As Button

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
' Create s new button, associating it with the variable myButton1
myButton1 = New Button

We just created a new button object.

vb

' Assign it some properties
With myButton1
.Left = 0
.Top = 10
.Text = "Press me"
.Width = 100
.Height = 100
.BackColor = Color.White
End With

Assigned it some properties.

Next is giving it the power to do and react to stuff.
vb

' The following tells the control which peice of code to use the that event.
' Format: {Control Name}.{Event Name}, AddressOf {Routine name}
AddHandler myButton1.Click, AddressOf Clicked_On_MyButton1
AddHandler myButton1.MouseHover, AddressOf MouseOver_MyButton1
AddHandler myButton1.MouseLeave, AddressOf MouseLeaving_MyButton1
' Add it to the form
Me.Controls.Add(myButton1)
End Sub

The line Me.Controls.Add(myButton1) places it on to the form.

Add in the procedure to process the events.
vb



Private Sub Clicked_On_MyButton1(ByVal sender As System.Object, ByVal e As System.EventArgs)
' The user has click on mybutton1, do something
MessageBox.Show("You clicked on myButton1")
End Sub

Private Sub MouseOver_MyButton1(ByVal sender As Object, ByVal e As System.EventArgs)
' mouse is over myButton1, do something
myButton1.BackColor = Color.Blue

End Sub
Private Sub MouseLeaving_MyButton1(ByVal sender As Object, ByVal e As System.EventArgs)
' The mouse is leaving myButton1, something
myButton1.BackColor = Color.White
End Sub
End Class


You may have noticed the difference between gui control code and coded.
vb

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

End Sub

The coded control is missing
Handles Button1.Click

Now you can create controls with using the designer. (Code Ninja Style ph34r.gif )





QUOTE(mineeric123 @ 28 Dec, 2008 - 04:09 PM) *


Hello,

I tried this example out but it did not work for me below is my code can you show me what I am doing wrong?

Thanks

Eric


CODE

Public Class Form1

    Dim myButton As Button

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        'create new button and assocaite it with myButton variable
        myButton = New Button

        'create the properties of myButton
        With myButton
            .Left = 0
            .Top = 10
            .Text = "Press Me"
            .Width = 100
            .Height = 100
            .BackColor = Color.White
        End With

        'the following tells which peice of code to use for each event for myButton
        'format (control name).(event name).(routine)
        AddHandler myButton.Click, AddressOf Clicked_On_myButton

    End Sub

    Private Sub Clicked_On_myButton(ByVal sender As Object, ByVal e As System.EventArgs)
        MessageBox.Show("You pressed my button ")
    End Sub

End Class

QUOTE(AdamSpeight2008 @ 27 Jul, 2008 - 12:53 PM) *

Up to now you've probably being designing controls by dragging the from the toolbox and dropping them on to a form.
No more!
I'll show you how to achieve the same only using code.

vb

Public Class Form1
Dim myButton1 As Button

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
' Create s new button, associating it with the variable myButton1
myButton1 = New Button

We just created a new button object.

vb

' Assign it some properties
With myButton1
.Left = 0
.Top = 10
.Text = "Press me"
.Width = 100
.Height = 100
.BackColor = Color.White
End With

Assigned it some properties.

Next is giving it the power to do and react to stuff.
vb

' The following tells the control which peice of code to use the that event.
' Format: {Control Name}.{Event Name}, AddressOf {Routine name}
AddHandler myButton1.Click, AddressOf Clicked_On_MyButton1
AddHandler myButton1.MouseHover, AddressOf MouseOver_MyButton1
AddHandler myButton1.MouseLeave, AddressOf MouseLeaving_MyButton1
' Add it to the form
Me.Controls.Add(myButton1)
End Sub

The line Me.Controls.Add(myButton1) places it on to the form.

Add in the procedure to process the events.
vb



Private Sub Clicked_On_MyButton1(ByVal sender As System.Object, ByVal e As System.EventArgs)
' The user has click on mybutton1, do something
MessageBox.Show("You clicked on myButton1")
End Sub

Private Sub MouseOver_MyButton1(ByVal sender As Object, ByVal e As System.EventArgs)
' mouse is over myButton1, do something
myButton1.BackColor = Color.Blue

End Sub
Private Sub MouseLeaving_MyButton1(ByVal sender As Object, ByVal e As System.EventArgs)
' The mouse is leaving myButton1, something
myButton1.BackColor = Color.White
End Sub
End Class


You may have noticed the difference between gui control code and coded.
vb

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

End Sub

The coded control is missing
Handles Button1.Click

Now you can create controls with using the designer. (Code Ninja Style ph34r.gif )


AdamSpeight2008

You've missed the important part of adding the control to the form. Me.Controls.Add(myButton).
P.S. Can you edit your entry to just include your code and question, there isn't a need to quote the tutorial twice.
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2009 Invision Power Services, Inc.