By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,849 Members | 2,361 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,849 IT Pros & Developers. It's quick & easy.

Problem with programmatically adding combo boxes

P: n/a
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?

--
Cindy
Sep 26 '07 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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
"cdmunoz" <cd*****@discussions.microsoft.comschreef in bericht
news:16**********************************@microsof t.com...
>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?

--
Cindy
Sep 27 '07 #2

P: n/a
Yes, I am. I had wondered if that was the problem. I will try that. Thanks.
I'll let you know if it works!
--
Cindy
"Cor Ligthert[MVP]" wrote:
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
"cdmunoz" <cd*****@discussions.microsoft.comschreef in bericht
news:16**********************************@microsof t.com...
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?

--
Cindy
Sep 27 '07 #3

P: n/a
That worked. Thank you so much.
--
Cindy
"cdmunoz" wrote:
Yes, I am. I had wondered if that was the problem. I will try that. Thanks.
I'll let you know if it works!
--
Cindy
"Cor Ligthert[MVP]" wrote:
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
"cdmunoz" <cd*****@discussions.microsoft.comschreef in bericht
news:16**********************************@microsof t.com...
>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?
>
--
Cindy
Sep 27 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.