Dynamically created ComboBoxes not functioning properly on SelectedIndexChanged

W

Wade

Hi,

I am dynamically creating X number of combo boxes, all bound to the same
data source. However, even though these combo boxes are all seperate
instances with unique names, when I change the value of one at runtime they
all change to reflect the newly selected value (i.e. I have 3 combo boxes, i
change the first one from nothing to "ABC", and the other two combo boxes
also change to "ABC").

Any clue as to why this might be? I've created a SelectedIndexChanged
event, and this event fires for all the combo boxes when just one of the
combo box indexes are changed.

Here's some of the code I use:

// this is what is called X number of times ...

cmb = new QuickComboBox(
"cmbType_" + iControlNum.ToString(),
pnlPoints,
ptControls.X,
ptControls.Y,
185,
20);

cmb.SelectedIndexChanged += new
EventHandler(cmb_SelectedIndexChanged);

if (dsElementTypes != null)
{
cmb.DisplayMember = "elementType";
cmb.ValueMember = "elementTypeID";
cmb.DataSource = dsElementTypes.Tables[0];
}

// here's the event code ...

private void cmb_SelectedIndexChanged(object sender, EventArgs e)
{
ComboBox c = (ComboBox)sender;

MessageBox.Show(c.Name + ":" + c.SelectedValue.ToString());
}

// this is the code for the QuickComboBox ...

public class QuickComboBox : System.Windows.Forms.ComboBox
{
public QuickComboBox(string name, Control oParent, int x, int y, int
w, int h)
{
this.Parent = oParent;
this.Bounds = new Rectangle(x, y, w, h);
this.Name = name;
}
}
 
W

Wade

Also note ...

I changed the databind from:

if (dsElementTypes != null)
{
cmb.DisplayMember = "elementType";
cmb.ValueMember = "elementTypeID";
cmb.DataSource = dsElementTypes.Tables[0];
}

to:

cmb.Items.Add("test1");
cmb.Items.Add("test2");
cmb.Items.Add("test3");
cmb.Items.Add("test4");

And now this problem doesn't occur. For instance, if I change the first
combo box to "test3", the remaining combo boxes do not automatically change
to "test3".

It looks to be related to using a DataSource, but I still fail to see what
I'm doing wrong.

Thanks.
 
G

Guest

Sure, if they're all bound to the same data source, when youi move the
cursor by setting its position in one, they'll all move to reflect the new
cursor position. If you want them to reflect separately, they each need
their own cursor to the data (i.e their own data source).

-Chris
 
W

Wade

Damn ... I should have realized that.

Thanks ... I have resolved this by changing:

cmb.DataSource = dsElementTypes.Tables[0];

To:

cmb.DataSource = dsElementTypes.Tables[0].Copy();

Is it beer o'clock yet?

Sure, if they're all bound to the same data source, when youi move the
cursor by setting its position in one, they'll all move to reflect the new
cursor position. If you want them to reflect separately, they each need
their own cursor to the data (i.e their own data source).

-Chris


Wade said:
Hi,

I am dynamically creating X number of combo boxes, all bound to the same
data source. However, even though these combo boxes are all seperate
instances with unique names, when I change the value of one at runtime
they all change to reflect the newly selected value (i.e. I have 3 combo
boxes, i change the first one from nothing to "ABC", and the other two
combo boxes also change to "ABC").

Any clue as to why this might be? I've created a SelectedIndexChanged
event, and this event fires for all the combo boxes when just one of the
combo box indexes are changed.

Here's some of the code I use:

// this is what is called X number of times ...

cmb = new QuickComboBox(
"cmbType_" + iControlNum.ToString(),
pnlPoints,
ptControls.X,
ptControls.Y,
185,
20);

cmb.SelectedIndexChanged += new
EventHandler(cmb_SelectedIndexChanged);

if (dsElementTypes != null)
{
cmb.DisplayMember = "elementType";
cmb.ValueMember = "elementTypeID";
cmb.DataSource = dsElementTypes.Tables[0];
}

// here's the event code ...

private void cmb_SelectedIndexChanged(object sender, EventArgs e)
{
ComboBox c = (ComboBox)sender;

MessageBox.Show(c.Name + ":" + c.SelectedValue.ToString());
}

// this is the code for the QuickComboBox ...

public class QuickComboBox : System.Windows.Forms.ComboBox
{
public QuickComboBox(string name, Control oParent, int x, int y,
int w, int h)
{
this.Parent = oParent;
this.Bounds = new Rectangle(x, y, w, h);
this.Name = name;
}
}
 
Top