Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / WinForm General / June 2004

Tip: Looking for answers? Try searching our database.

Assign Events to dynamically generated TextBox

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Zardoz - 30 Jun 2004 11:10 GMT
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
ClayB [Syncfusion] - 30 Jun 2004 12:47 GMT
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

> 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
>
[quoted text clipped - 8 lines]
>
> 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...
>
[quoted text clipped - 3 lines]
>
>                                                      Zardoz
Herfried K. Wagner [MVP] - 30 Jun 2004 13:05 GMT
* =?Utf-8?B?WmFyZG96?= <Zardoz@discussions.microsoft.com> scripsit:
> Private Sub txtCamp_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtCamp.KeyDown
>        
[quoted text clipped - 10 lines]
> 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.

Signature

Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/


Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.