[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 272
  • Last Modified:

Handling / creating events for run-time created controls

Hi guys,

I've got a form that creates several textboxes at run-time.  I've got an array of textboxes called Times(15).

Now, since these are created at run time, how am I able to determine, say, if the text was changed?  There is no text_changed event associated with it, since it wasn't created during design time.  So how am i able to associate events with these run-time created controls?

Thanks so much!

-Brad
0
bmvincent
Asked:
bmvincent
  • 2
1 Solution
 
gregoryyoungCommented:
you would use ...

textbox.text_changed+= new eventhandler(yourroutine)
0
 
ptakjaCommented:
I believe gregoryyoung's comment is the C# syntax...

Since you created the textboxes at runtime, you need to "wire" the events to an event handler.   Try this:


    ' Loop through textboxes adding the single event handler for the TextChanged event
    For ii As Index = 0 To Times.Count -1
        AddHandler Time(ii).TextChanged, AddressOf TxtChanged
    Next

You can use the same technique to "wire" other textbox events to generic event handlers


    Public Sub TxtChanged(ByVal sender As Object, ByVal e As System.EventArgs)
           'Put your code here to deal with the txtChanged event
           You can determine the textbox in here by casting the sender object
          Dim txtBox As TextBox = CType(sender, TextBox)
         txtBox will be the textbox that triggered the event.          
    End Sub

Jeff
0
 
gregoryyoungCommented:
oops you're right wrong forum :)

you are correct addhandler is the vb.net format/
0
 
bmvincentAuthor Commented:
Jeff, excellent response.  Makes alot more sense now.  I appreciate it.
0

Featured Post

Learn to develop an Android App

Want to increase your earning potential in 2018? Pad your resume with app building experience. Learn how with this hands-on course.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now