Question:
How can I add an event to a control in VB.Net?
Mark Ellers
2011-11-06 12:28:54 UTC
The code below is a function that creates a new button each time it is used . My only problem is that I want to add events like Click, MouseDown, and others to the new buttons that get created. How can I do that? Thanks in advance! :D

Code:

Public Function ADDBUTTON()
Dim intLoop As Integer



For intLoop = 0 To 10

newbutton.Text = "Button"

newbutton.Top = 100 + 100

newbutton.Left = 100 + 100

newbutton.Height = "120"

newbutton.Width = "200"

Me.Controls.Add(newbutton)

Next
End Function
Four answers:
?
2011-11-06 16:22:37 UTC
Use AddHandler to assign an existing event handler to a new control that you have created dynamically.

Here is an example where an event handler called multiButton_click is assigned to new button controls that are placed on the form in the form load event.







Public Class Form1







Private Sub multiButton_Click(ByVal sender As System.Object, _

ByVal e As System.EventArgs) Handles Button1.Click



'do stuff here

Beep()



End Sub



Private Sub Form1_Load(ByVal sender As Object, _

ByVal e As System.EventArgs) Handles Me.Load

'add some buttons dynamically

Dim btn As Button

Dim baseTop As Integer = Button1.Height

Dim baseLeft As Integer = Button1.Left



For x As Integer = 1 To 5

btn = New Button

'copy properties

btn.Height = Button1.Height

btn.Width = Button1.Width

btn.Left = baseLeft

'update the text to display on the button

btn.Text = "dynamic_" & x



btn.Top = baseTop + ((Button1.Height + 10) * x)

'place the button on the form

Me.Controls.Add(btn)

btn.Visible = True

btn.Enabled = True

'

AddHandler btn.Click, AddressOf multiButton_Click



Next





End Sub

End Class
samofcalifornia
2011-11-06 14:47:43 UTC
Here is a trick you can use many times in the future. In a project somewhere, create a form and add a button to the form and then add an event handler. Actually, you probably already have a proect you can look at. Then look at the generated designer code (in the Designer.vb file); there should be code there that does the event handler. You might not be able to open the designer file directly using Visual Studio, but open the file somehow. You will see code that uses the AddHandler statement.
texasmaverick
2011-11-07 03:59:51 UTC
You simply make a new instance of the System.Windows.Form.Button. This inherits all of a normal button properties and events. I used 2 of them for the text and backcolor.





Public Class Form1



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

Dim bttn As New System.Windows.Forms.Button

bttn.Text = "I'm New"

bttn.Top = 150

bttn.Left = 150

bttn.Width = 80

bttn.BackColor = Color.Red

Me.Controls.Add(bttn)

End Sub

End Class





TexMav
anonymous
2016-11-07 10:59:13 UTC
The AddHandler technique is probable ideal. this gadget is termed 'late binding', truly of binding activities at layout time, you do it at runtime, frequently in the fashion.Load experience. regrettably for VB.internet programmers, VB is pretty handicapped because the social gathering you want to do is amazingly undemanding in C#. lots of the code files (consisting of software.VB, which includes the get entry to point, and MyForm.fashion designer.vb) are hidden in the IDE, discouraging you from messing with that code. typically you mustn't attempt this in case you don't recognize what you're doing, yet various examples of layout time ameliorations aren't any further accessible with no need the flexibility to edit them. observe that for most activities that are EventHandler delegates, or stick with that trend, deliver the first parameter, sender as merchandise, because the administration the position the shape is fired. So a regularly occurring experience to regulate drag and drop for say, a given tree administration, ought to confirm which tree administration invoked the shape by casting this first parameter to a TreeView. In VB it really is done utilising the CType operator.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...