Code for Events of Dynamically created ComboBox
Need an help to code events for the dynamically created combobox in VB. As the combobox is created and filled with data at runtime, now if we want to access any event of this combobox how to code for that event. Please if anyone can help me out .... Thanx
# 1 Re: Code for Events of Dynamically created ComboBox
Afraid to say that there isn't any easy way to do this. You might want to look into subclassing the form or using a pre-defined hidden array of ComboBox that you dynamically expand.
# 3 Re: Code for Events of Dynamically created ComboBox
Need an help to code events for the dynamically created combobox in VBHow exactly are you creating the combo box dynamically.
1. If using CreateWindowEx, then subclass the combobox
2. If creating based off of an indexed control array (i.e., Load Combo1(1)) then simply respond to the control's events
3. If creating like Me.Controls.Add(....) then
' declare it in declaration's section using WithEvents, and now it will show up in the top left dropdown window in your form's code page
Private WithEvents myCombo As ComboBox
... now when creating it...
Set myCombo = Me.Controls.Add("VB.ComboBox", "SomeName")
4. Something different?
# 4 Re: Code for Events of Dynamically created ComboBox
Try this code:
Private WithEvents cbo As ComboBox
Private Sub cbo_Change()
MsgBox "Event is fired for dynamically added combo box"
End Sub
Private Sub Form_Load()
Set cbo = Me.Controls.Add("VB.ComboBox", "cboCategory")
cbo.Visible = True 'With Top=0 and Left=0
End Sub