ComboBox problem windows application
-
Hi friends i am developing windows application . I have two combobox on form. I added comboBox1_SelectedIndexChanged event for both combobox. In form load i bind both combobox with ArrayList arr and arr2 object ..The problem is that when i give combobox1.DataSource = arr it call combobox1_SelectedIndexChanged event why it call .... i didn't call it...because i have diff functionality for combobox1_SelectedIndexChanged event and it call on form load when i give combobox1.DataSource and my logic is failed how to avoid this ...... Thanks and regards
-
Hi friends i am developing windows application . I have two combobox on form. I added comboBox1_SelectedIndexChanged event for both combobox. In form load i bind both combobox with ArrayList arr and arr2 object ..The problem is that when i give combobox1.DataSource = arr it call combobox1_SelectedIndexChanged event why it call .... i didn't call it...because i have diff functionality for combobox1_SelectedIndexChanged event and it call on form load when i give combobox1.DataSource and my logic is failed how to avoid this ...... Thanks and regards
This happens because when the combobox is populated, the first item is automatically selected, and therefore, the SelectedIndexChanged event is called. Use a boolean flag to prevent this from happening:
bool AllowSelectedIndexChange = true;
void Form1_Load()
{
...
AllowSelectedIndexChange=false;
combobox1.DataSource = arr;
AllowSelectedIndexChange=true;
...
}
void ComboBox1_SelectedIndexChanged(...)
{
if (AllowSelectedIndexChange)
{
//current SelectedIndexChanged code goes here
}
}Hope this helps, DigitalKing
-
This happens because when the combobox is populated, the first item is automatically selected, and therefore, the SelectedIndexChanged event is called. Use a boolean flag to prevent this from happening:
bool AllowSelectedIndexChange = true;
void Form1_Load()
{
...
AllowSelectedIndexChange=false;
combobox1.DataSource = arr;
AllowSelectedIndexChange=true;
...
}
void ComboBox1_SelectedIndexChanged(...)
{
if (AllowSelectedIndexChange)
{
//current SelectedIndexChanged code goes here
}
}Hope this helps, DigitalKing
-
This happens because when the combobox is populated, the first item is automatically selected, and therefore, the SelectedIndexChanged event is called. Use a boolean flag to prevent this from happening:
bool AllowSelectedIndexChange = true;
void Form1_Load()
{
...
AllowSelectedIndexChange=false;
combobox1.DataSource = arr;
AllowSelectedIndexChange=true;
...
}
void ComboBox1_SelectedIndexChanged(...)
{
if (AllowSelectedIndexChange)
{
//current SelectedIndexChanged code goes here
}
}Hope this helps, DigitalKing
hi DigitalKing & yogsworld! :) you can also try these options: option 1:
void Form1_Load()
{
...
combobox1.SelectedIndexChanged -= new EventHandler(SelectedIndexChanged_Handler);
combobox1.DataSource = arr;
combobox1.SelectedIndexChanged += new EventHandler(SelectedIndexChanged_Handler);
...
}option 2: remove the attaching of the SelectedIndexChanged event handler in the InitializeComponent, then just do it in the Form1_Load after the setting of DataSource property of the combobox. just alternatives instead of using a flag. :) microsoc :cool: