Page 1 of 1

Build a control without using the GUI Designer Rate Topic: -----

#1 AdamSpeight2008   User is offline

  • MrCupOfT
  • member icon

Reputation: 2298
  • View blog
  • Posts: 9,535
  • Joined: 29-May 08

Posted 27 July 2008 - 01: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.

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.

        ' 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.
        ' 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.

    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.
    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: )

This post has been edited by AdamSpeight2008: 28 July 2008 - 11:26 AM


Is This A Good Question/Topic? 1
  • +

Replies To: Build a control without using the GUI Designer

#2 mineeric123   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 36
  • Joined: 12-November 08

Posted 28 December 2008 - 05:11 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


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


Was This Post Helpful? 0
  • +
  • -

#3 AdamSpeight2008   User is offline

  • MrCupOfT
  • member icon

Reputation: 2298
  • View blog
  • Posts: 9,535
  • Joined: 29-May 08

Posted 28 December 2008 - 05:52 PM

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.
Was This Post Helpful? 0
  • +
  • -

#4 camckee316   User is offline

  • D.I.C Regular

Reputation: 15
  • View blog
  • Posts: 294
  • Joined: 29-August 08

Posted 03 August 2010 - 10:23 AM

Do you recommand building you applications this way or do you use the drop and drag to make you controls?
Was This Post Helpful? 0
  • +
  • -

#5 CharlieMay   User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1734
  • View blog
  • Posts: 5,710
  • Joined: 25-September 09

Posted 17 September 2010 - 08:26 AM

camckee316,
Adding the controls from the designer is fine, however, there are times that you will discover this method is needed. For example, I have a touch-screen timeclock that lists each employees as a button. Since I only want a button for each employee that uses the time clock, I can build them at run-time and not have to make changes and re-compile for any employee change. This is just an example and there are other ways to accomplish the same thing (listboxes, comboboxes etc) but I wanted a button, which I placed in a panel and this was the best method for doing it. The program querys the database, creates a button for each employee and stores the employeeID in the tag of the button. When the user clicks their name and either the clock in or clock out button, the id along with the type and time are stored in the database.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1