Problem with programmatically adding combo boxes

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?
 
C

Cor Ligthert[MVP]

Hi,

Most probably are you using all the same the build in dataview from the
datatable (the defaultview).

This is a common asked question, just create for every combobox a seperate
New dataview and you will se it working as you want.

Cor
 
G

Guest

Yes, I am. I had wondered if that was the problem. I will try that. Thanks.
I'll let you know if it works!
 
G

Guest

That worked. Thank you so much.
--
Cindy


cdmunoz said:
Yes, I am. I had wondered if that was the problem. I will try that. Thanks.
I'll let you know if it works!
 

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