B
BillE
VB.NET, VS 2005
I am adding combo boxes to a windows form dynamically.
I am first adding the controls to a panel, and then adding the panel to the
form in order to group the controls appropriately for the application.
I need to populate the combo box with values and text, and also SET THE
SELECTED ITEM (cb.SelectedValue = 2).
In the code below, the selected value remains Nothing even though set to 2,
and the displayed text is "One".
It seems that the selected value for a dynamically created ComboBox can't be
set until AFTER the control is added to the Form! In this case, that
doesn't happen until the Panel is added to the Form.
This is very inconvenient, since I am first populating a Panel and then
adding the Panel to the Form, so I'll have to go back and set the selected
values for all the ComboBoxes AFTER the panel has been added to the Form.
I assume I am overlooking something - can somebody set me straight? How can
I set the selected value for the dynamically created ComboBox before it is
actually placed on a form?
'create a Panel
dim p as new Panel
'create the combo box
dim cb as new ComboBox
cb.Name = "testCB"
cb.ValueMember = "value"
cb.DisplayMember = "text"
'create list of Items
dim dt as new DataTable
dt.Columns.Add ("value")
dt.Columns.Add ("text")
dim dr as DataRow
'add rows
dr = dt.NewRow
dr("value") = 1
dr("text") = "one"
dt.Rows.Add(dr)
dr = dt.NewRow
dr("value") = 2
dr("text") = "two"
dt.Rows.Add(dr)
cb.DataSource = dt
'set selected value - IT WILL REMAIN NOTHING!!!
cb.SelectedValue = 2
p.Controls.Add(cb)
me.Controls.Add(p)
'I HAVE TO SET THE SELECTED VALUE HERE, AFTER ADDING THE CONTROL
'TO THE FORM, FOR IT TO WORK
'cb.SelectedValue = 2
I am adding combo boxes to a windows form dynamically.
I am first adding the controls to a panel, and then adding the panel to the
form in order to group the controls appropriately for the application.
I need to populate the combo box with values and text, and also SET THE
SELECTED ITEM (cb.SelectedValue = 2).
In the code below, the selected value remains Nothing even though set to 2,
and the displayed text is "One".
It seems that the selected value for a dynamically created ComboBox can't be
set until AFTER the control is added to the Form! In this case, that
doesn't happen until the Panel is added to the Form.
This is very inconvenient, since I am first populating a Panel and then
adding the Panel to the Form, so I'll have to go back and set the selected
values for all the ComboBoxes AFTER the panel has been added to the Form.
I assume I am overlooking something - can somebody set me straight? How can
I set the selected value for the dynamically created ComboBox before it is
actually placed on a form?
'create a Panel
dim p as new Panel
'create the combo box
dim cb as new ComboBox
cb.Name = "testCB"
cb.ValueMember = "value"
cb.DisplayMember = "text"
'create list of Items
dim dt as new DataTable
dt.Columns.Add ("value")
dt.Columns.Add ("text")
dim dr as DataRow
'add rows
dr = dt.NewRow
dr("value") = 1
dr("text") = "one"
dt.Rows.Add(dr)
dr = dt.NewRow
dr("value") = 2
dr("text") = "two"
dt.Rows.Add(dr)
cb.DataSource = dt
'set selected value - IT WILL REMAIN NOTHING!!!
cb.SelectedValue = 2
p.Controls.Add(cb)
me.Controls.Add(p)
'I HAVE TO SET THE SELECTED VALUE HERE, AFTER ADDING THE CONTROL
'TO THE FORM, FOR IT TO WORK
'cb.SelectedValue = 2