G
Guest
I have created a sub routine to programmatically add an indefinite number of
combo boxes to a form. The combo boxes are bound in code to a view for
displaying the available options. This routine works good until you add the
second combo box. At that point it changes the first combo box's selected
item. A third changes the second and first and so on. I've tried setting
the combo box name and tag to a unique name, but that doesn't help. How can
I make each iteration behave separately?
Here's the code for the routine:
Private Sub SetNewCombo(ByVal CControl As Control, ByVal ControlName As
String, ByVal Datafield As String, ByVal DisplayField As String, Optional
ByVal SourceField As String = "", Optional ByVal DataSource As DataView =
Nothing, Optional ByVal DefaultValue As String = "")
Try
Dim lCombo As New ComboBox
With lCombo
.Name = ControlName 'Predefined as "Filter1Combo" and the
number of the added control
.Font = gFont
.Size = New Size(140, 20)
.Location = New Point(6, mintHeight)
.DisplayMember = DisplayField
.ValueMember = Datafield
.Tag = ControlName
.Visible = True
Dim lCol1, lCol2 As New DataColumn
lCol1.DefaultValue = DisplayField
lCol2.DefaultValue = Datafield
.Items.Add(lCol1)
.Items.Add(lCol2)
'Set Data
.DataSource = DataSource
'Set default value
If DefaultValue.Length > 0 Then
.SelectedValue = DefaultValue
End If
End With
CControl.Controls.Add(lCombo)
Catch ex As Exception
MsgBox("SetNewCombo: " & ex.Message)
End Try
End Sub
As I stated, the creation of the controls works great, but after selecting
the value and you add another combo box, it resets the previous box's value
to the new box value. What am I missing?
combo boxes to a form. The combo boxes are bound in code to a view for
displaying the available options. This routine works good until you add the
second combo box. At that point it changes the first combo box's selected
item. A third changes the second and first and so on. I've tried setting
the combo box name and tag to a unique name, but that doesn't help. How can
I make each iteration behave separately?
Here's the code for the routine:
Private Sub SetNewCombo(ByVal CControl As Control, ByVal ControlName As
String, ByVal Datafield As String, ByVal DisplayField As String, Optional
ByVal SourceField As String = "", Optional ByVal DataSource As DataView =
Nothing, Optional ByVal DefaultValue As String = "")
Try
Dim lCombo As New ComboBox
With lCombo
.Name = ControlName 'Predefined as "Filter1Combo" and the
number of the added control
.Font = gFont
.Size = New Size(140, 20)
.Location = New Point(6, mintHeight)
.DisplayMember = DisplayField
.ValueMember = Datafield
.Tag = ControlName
.Visible = True
Dim lCol1, lCol2 As New DataColumn
lCol1.DefaultValue = DisplayField
lCol2.DefaultValue = Datafield
.Items.Add(lCol1)
.Items.Add(lCol2)
'Set Data
.DataSource = DataSource
'Set default value
If DefaultValue.Length > 0 Then
.SelectedValue = DefaultValue
End If
End With
CControl.Controls.Add(lCombo)
Catch ex As Exception
MsgBox("SetNewCombo: " & ex.Message)
End Try
End Sub
As I stated, the creation of the controls works great, but after selecting
the value and you add another combo box, it resets the previous box's value
to the new box value. What am I missing?