Results 1 to 3 of 3
  1. #1
    4 Star Lounger
    Join Date
    Feb 2001
    Location
    Gillingham, Kent, England
    Posts
    511
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Event Handling of dynamic controls. (VB6 SR5)

    I used the following code on a form to dynamically create a textbox with an on_change event, however, the event handler only works for the last instance of textbox created using the test1 variable.
    Is there a way to handle events for each control added based on the control name, not the variable used to create it?

    Private WithEvents test1 As TextBox

    Private Sub Form_Load()
    Set test1 = Controls.Add("vb.textbox", "MyTextBox", Me)
    With test1
    .Visible = True
    .Width = 1000
    .Height = 200
    End With
    End Sub

    Private Sub test1_Change()
    MsgBox "tested"
    End Sub

  2. #2
    Plutonium Lounger
    Join Date
    Dec 2000
    Location
    Sacramento, California, USA
    Posts
    16,775
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Event Handling of dynamic controls. (VB6 SR5)

    Have you tried making Test1 and array and adding each dynamic textbox to it? I can't say that I've done that, but the way your code is written, each time you add a control, the latest version is what is instantiated as Test1, so it is doing exactly what you told it to. <img src=/S/crazy.gif border=0 alt=crazy width=15 height=15>
    Charlotte

  3. #3
    4 Star Lounger
    Join Date
    Feb 2001
    Location
    Gillingham, Kent, England
    Posts
    511
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Event Handling of dynamic controls. (VB6 SR5)

    If I put any brackets in the Private WithEvents statement it either returns 'expected as' or 'expected end of statement'

    On the bright side, I've just spoken to the higher powers and we decided that if the events of any new tab have to be hard coded anyway, its going to be just as easy to add in the Private Withevents statement with each tab added. Therefore the urgency has disappeared from the question, now its just the curiosity that remains!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •