Question Dynamically addhandler

enveetee

New member
Joined
Nov 15, 2009
Messages
1
Programming Experience
10+
Is it possible to dynamically direct a particular control's event to use a
predefined method.

For example.
I have a form which I dynamically add controls to, let's say, TextBox1,
TextBox2 and TextBox3 and a module which as 3 routines, Method1, Method2,
Method3

When I dynamically add my controls, eg TextBox4, I want to choose which of
the 3 methods I want mapped to which event.

I was thinking of something like this...

.
.
.
Dim objTextBox1 as new TextBox
dim NameOfRoutine$="Module.Method2"
Form.Controls.Add(objTextBox1)

Call AssignMethodToEvent(objTextBox1.GotFocus, NameOfRoutine$)
.
.
.


Can anyone help me on this? Is this possible?

Nigel
 
When you create controls at runtime, you need to use the addhandler statement to route the events you want.
VB.NET:
Dim tb As New Textbox
With tb
...
Addhandler .TextChanged, AddressOf tb_TextChanged
End With

Private Sub tb_TextChanged(ByVal sender As Object, ByVal e As EventArgs)
  'do something...
End Sub
Just remember your event signature must match the event:
(ByVal sender As Object, ByVal e As EventArgs)
 
Back
Top