School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become an Expert!

Join 300,466 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,711 people online right now. Registration is fast and FREE... Join Now!




Build a control without using the GUI Designer

 
Reply to this topicStart new topic

> Build a control without using the GUI Designer

AdamSpeight2008
Group Icon



post 27 Jul, 2008 - 12:53 PM
Post #1


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 )

This post has been edited by AdamSpeight2008: 28 Jul, 2008 - 10:26 AM
Go to the top of the page
+Quote Post


Register to Make This Ad Go Away!

mineeric123
*



post 28 Dec, 2008 - 04:11 PM
Post #2
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

Go to the top of the page
+Quote Post

AdamSpeight2008
Group Icon



post 28 Dec, 2008 - 04:52 PM
Post #3

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.
Go to the top of the page
+Quote Post


Fast ReplyReply to this topicStart new topic
2 User(s) are reading this topic (2 Guests and 0 Anonymous Users)
0 Members:

 


Lo-Fi Version Time is now: 11/8/09 02:33AM

Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month