Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Events on dynamic controls.

Status
Not open for further replies.

160473

IS-IT--Management
Apr 16, 2003
55
FI
I have dynamicly created textboxes in a windows form.
Now I need to create mouse click events to the textboxes and I'm stuck...
I normal cases this would be the solution:

Private Sub TextBox1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.Click
MsgBox(TextBox1.Text)
End Sub

But as they are dynamicly created at runtime this is not an opion.
 
If the event handler exists in the code, you can wire dynamic controls up to event handlers at run time (using the same code that you use to wire up event handlers statically) when you instantiate them.

Rhys
"There are some oddities in the perspective with which we see the world. The fact that we live at the bottom of a deep gravity well, on the surface of a gas-covered planet going around a nuclear fireball 90 million miles away and think this to be normal is obviously some indication of how skewed our perspective tends to be"
DOUGLAS AD
 
Use Addhandler, and then create a Sub to handle the event
Code:
Dim t As New TextBox
AddHandler t.MouseDown, AddressOf textboxMousedown
.
.
private sub textboxMousedown
    'Your Code Here
end sub


Sweep
...if it works dont f*** with it
...if its f****ed blame someone else
...if its your fault that its f***ed, say and admit nothing.
 
Code:
addhandler newdynamictextbox.click, addressof commontextbox.click

where commontextbox.click is the generic event handling all textbox click events

inside this generic event you can use the 'sender' object to define the textbox raising the event
 
Yes this works greate if I create one control and do a AddHandler on that control.
But if I create many controls like this:

Private Sub Create_Label()
for i = 0 to 9
dim MyLabel as new Label
With MyLabel
.Location = New System.Drawing.Point(10, i*20)
.Size = New System.Drawing.Size(150, 13)
.Text = "Some text..."
.Name = "Label" & i
End With
Form1.Controls.Add(MyLabel)
AddHandler MyLabel.Click, AddressOf Me.Click_Event
next
end sub

Private Sub Click_Event(ByVal sender As Object, ByVal e As EventArgs)
MsgBox("Hello World")
End Sub

...nothing happens.
 
Sorry! My misstake, I assigned the event to the wrong label...
When I clicked the right one, everything worked!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top