Results 1 to 5 of 5

Thread: Events handling with runtime created controls {RESOLVED}

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Oct 2003
    Posts
    26

    Events handling with runtime created controls {RESOLVED}

    I have a set of runtime created combo boxes which i keep in a collection.
    How do I use withevents or any other method to keep track of the selection changes made to these combo boxes?

    Thanks in advance
    Last edited by weiyen; Jan 14th, 2004 at 08:14 PM.

  2. #2
    -= B u g S l a y e r =- peet's Avatar
    Join Date
    Aug 2000
    Posts
    9,629
    think you can do something like this

    VB Code:
    1. Private WithEvents MyListBox As ListBox
    2.  
    3. Private Sub Form_Load()
    4.     Set MyListBox = Me.Controls.Add("VB.ListBox", "lstNewListBox")
    5.     With MyListBox
    6.         .Move 0, 0, 800, 2000
    7.         .Visible = True
    8.     End With
    9. End Sub
    -= a peet post =-

  3. #3
    -= B u g S l a y e r =- peet's Avatar
    Join Date
    Aug 2000
    Posts
    9,629
    VB Code:
    1. Option Explicit
    2.  
    3. Private WithEvents MyListBox As ListBox
    4.  
    5. Private Sub Form_Load()
    6.     Dim i As Integer
    7.     Set MyListBox = Me.Controls.Add("VB.ListBox", "lstNewListBox")
    8.     With MyListBox
    9.         .Move 0, 0, 800, 2000
    10.         .Visible = True
    11.     End With
    12.     For i = 0 To 100
    13.         MyListBox.AddItem "item " & Format(i, "000")
    14.     Next i
    15. End Sub
    16.  
    17. Private Sub MyListBox_Click()
    18.     MsgBox MyListBox.Text
    19. End Sub
    -= a peet post =-

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Oct 2003
    Posts
    26
    Sorry clicked too fast
    Last edited by weiyen; Jan 2nd, 2004 at 08:19 AM.

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Oct 2003
    Posts
    26
    Thank you for the replies.

    The method you had given needs to have multiple declarations of withEvents for each control created during runtime.

    I've discovered that I can declare a class of event handlers.

    Can't really remember the codes, as i left it in my office, but it goes something like this..

    (Class: comboEvent)

    Public withEvents myCombo as comboBox


    Private Sub myCombo_Change()
    msgBox myCombo.text
    End Sub


    (Usage)

    Dim comboHandler as comboEvent
    Dim eventCollection as collection
    Dim comboCollection as collection

    Set eventCollection = new collection
    Set comboCollection = new collection

    Private Sub CommandButton1_Click()
    set comboHandler as new comboEvent
    eventCollection.AddItem comboHandler

    comboCollection.AddItem Controls.add(ComboBox,,,) ' Can't remeber exact codes here.

    Dim ctrl as comboBox
    Dim i as integer

    for each ctrl in comboCollection
    set eventCollection.item(i).myCombo = comboCollection.item(i) 'Can only remember its something like this. Will provide the working set on monday (Singapore time)

    next ctrl
    End Sub

Posting Permissions

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



Click Here to Expand Forum to Full Width