Welcome to Dream.In.Code
Getting Help is Easy!

Join 136,808 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 2,264 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!


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

 

Lo-Fi Version Time is now: 12/3/08 02:29PM

Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month