Assign Events to dynamically generated TextBox

G

Guest

Hi to all,

I am having a problem while trying to assign an event to a generated textbox control. This textbox is generated dynamically, its a variable which name is "txtCamp", for this variable I generate a certain number of instances in the layout screen in some differents positions, to have a binding to the database fields.These textboxes have a different property "Name" assigned dynamically to distinguish them. Then I add a new Event Handler which responds to the "KeyDown" event of the textbox. My problem comes when I want to distinguish between all the "KeyDown" Events of these instances of the variable "txtCamp". The code added to the "KeyDown" event is the typical one:

Private Sub txtCamp_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtCamp.KeyDown

txtCamp.BackColor = System.Drawing.Color.Gold
btModify.Enabled = False
btDelete.Enabled = False
End Sub

The event handlet is added normally like this:

AddHandler txtCamp.KeyDown, AddressOf txtCamp_KeyDown

And IS ONLY ADDED to the instance which I want it responds to. But some strange things happen like for example it raises with the last TextBox created dynamically.

Also I'm having problems like this while assigning properties or methods to distinguish them...

Is it possible to solve this? I tried Reflection but it didn't solve my problems, or I didn't realise in the good way...

Many thanks to all.

Zardoz
 
C

ClayB [Syncfusion]

You might try code like:

Private Sub txtCamp_KeyDown(ByVal sender As Object, ByVal e As
System.Windows.Forms.KeyEventArgs) Handles txtCamp.KeyDown

Dim tb As TextBox = CType(sender, TextBox)
tb.BackColor = System.Drawing.Color.Gold
btModify.Enabled = False
btDelete.Enabled = False
End Sub

======================
Clay Burch, .NET MVP

Visit www.syncfusion.com for the coolest tools


Zardoz said:
Hi to all,

I am having a problem while trying to assign an event to a
generated textbox control. This textbox is generated dynamically, its a
variable which name is "txtCamp", for this variable I generate a certain
number of instances in the layout screen in some differents positions, to
have a binding to the database fields.These textboxes have a different
property "Name" assigned dynamically to distinguish them. Then I add a new
Event Handler which responds to the "KeyDown" event of the textbox. My
problem comes when I want to distinguish between all the "KeyDown" Events of
these instances of the variable "txtCamp". The code added to the "KeyDown"
event is the typical one:
Private Sub txtCamp_KeyDown(ByVal sender As Object, ByVal e As
System.Windows.Forms.KeyEventArgs) Handles txtCamp.KeyDown
txtCamp.BackColor = System.Drawing.Color.Gold
btModify.Enabled = False
btDelete.Enabled = False
End Sub

The event handlet is added normally like this:

AddHandler txtCamp.KeyDown, AddressOf txtCamp_KeyDown

And IS ONLY ADDED to the instance which I want it responds to. But some
strange things happen like for example it raises with the last TextBox
created dynamically.
Also I'm having problems like this while assigning properties or methods to distinguish them...

Is it possible to solve this? I tried Reflection but it didn't solve my
problems, or I didn't realise in the good way...
 
H

Herfried K. Wagner [MVP]

* =?Utf-8?B?WmFyZG96?= said:
Private Sub txtCamp_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtCamp.KeyDown

txtCamp.BackColor = System.Drawing.Color.Gold
btModify.Enabled = False
btDelete.Enabled = False
End Sub

The event handlet is added normally like this:

AddHandler txtCamp.KeyDown, AddressOf txtCamp_KeyDown

And IS ONLY ADDED to the instance which I want it responds to. But
some strange things happen like for example it raises with the last
TextBox created dynamically.

Post the code you use to create the new instances of the 'TextBox' class.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top