Adding combobox to windows form dynamically

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
 
C

Cor Ligthert[MVP]

Bill,

I assume that you have events in your code and probably something as
CB_SelectedIndex_changed.

That will fire as soon as you select something in your combox.
Are you sure that on that place your selection is not eliminated again.

This kind of things are mostly the problem areas with the combobox.

Cor
 
B

BillE

That's a good point - I do have an event handler hooked up.
How do I tell a dynamically created combo box what the selected value is?

Thanks!
Bill
 
B

BillE

Hi Cor
Thank you for taking the time to address my problem.

What I am trying to do is
- dynamically create a Combo Box
- add items to the Combo Box
- set the Selected Item

I am unable to set the Selected Item successfully.

Thanks!
Bill
 
C

Cor Ligthert[MVP]

Bill,

I never use the selectedvalue, but always the index of a combobox.

Cor
 
B

BillE

I can't set the selected index either on a dynamically created combo box,
until after the panel is added to the form controls collection.

This makes it very difficult to build a panel, populate it with controls,
and then add the panel to the form.
 
C

Cor Ligthert[MVP]

BillE.

I don't see your problem why it is difficult. You are right, the combox
needs to be a child of a valid control, however what does that makes things
difficult, therefore I am very curious what that can be.

In VB2008 the code can even be
\\\
Option Infer On
Option Strict On
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
Controls.Add(New Panel)
Dim myPanel = Controls(0)
myPanel.Controls.Add(New ComboBox)
End Sub
End Class
///
Cor
 
B

BillE

Hi Cor

I create a combo box and add the combo box to a panel. The combo box needs
to have a value selected.

Later I add the panel to the form controls collection.

The problem is that I am not able to set the selected item (selected value
or selected index) for a combo box until AFTER the panel is added to the
form's controls collection.

I want to set the selected item when the combo box is created, not later
when the panel container is added to the form.

If I attempt to set the SelectedIndex property for the combo box when it is
created, it returns an ArgumentOutOfRange exception, as if there were no
items in the combo box yet. After the Panel is added to the form, THEN I
can set the SelectedIndex.

I apologize for not explaining well.

Thanks
Bill
 
C

Cor Ligthert[MVP]

BillE,

I understand what you want and that is not working, however why do you want
first to put the comboboxes on the panel and then the panel on the form,
instead of first setting the panel to the form and then the comboboxes?

You can forever make your panel visible and not visible in my idea?
(In fact where you use a panel for).

Cor
 
B

BillE

Hi Cor
That is a good idea, I will do it that way.
I will have to later change the location of the panel, but that should not
be a problem.
Thanks
Bill
 

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