Using events with controls created in code.

S

Shawn

Hello All,

I am trying to raise enter events for a combobox control that I am building
dynamically at run time. A user enters a number in a textbox and that many
comboboxes are created. I then need to populate them with a database query,
but can seem to find a way to get any more than the last control to respond.
I have them being named differently, but only the base name shows for
events. Below is my code...

Dim WithEvents ctlComboBox As Control

Private Sub frmAddPeopleToGroup_Load(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles MyBase.Load
CreateControls()
MakeTable()
End Sub
Private Sub CreateControls()
Static LocationStart As Integer = 50

Dim vbFont As New Font("Baskerville Old Face", 10)
Dim i As Integer

For i = 0 To frmAddGroup.vbPeopleCount - 1
ctlComboBox = New ComboBox()
With ctlComboBox
.Name = "cmbPeople" & i
.Location = New Point(20, LocationStart)
.Width = 250
.Font = vbFont
End With

LocationStart += 30
Me.Controls.Add(ctlComboBox)
Next
End Sub

Thanks!!
 
S

Shawn

I don't know where my brain went on this one, but your code looks good. I
will adapt it and try it out.

Thanks!!!
 
H

Herfried K. Wagner [MVP]

Shawn said:
I am trying to raise enter events for a combobox control that I am
building
dynamically at run time.

Take a look at the 'AddHandler'/'RemoveHandler' statements:

\\\
Private Sub Test()
Dim Button1 As New Button()
With Button1
.Location = New System.Drawing.Point(56, 88)
.Name = "Button1"
.Size = New System.Drawing.Size(144, 48)
.TabIndex = 0
.Text = "Button1"
End With
AddHandler Button1.Click, AddressOf Me.Button_Click
Me.Controls.Add(Button1)
End Sub

Private Sub Button_Click( _
ByVal sender As Object, _
ByVal e As EventArgs _
)
MsgBox("Hello World")
End Sub
///
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top