listbox selectedvaluechanged event fires programmaticlly !!! [modified]
-
I'm populating a listbox using it's DataSource property in form's load event what happens is that the selectedValueChanged event or selectedIndexChanged fires twice when starting the form how can i prevent such behaviour i want the event to fire only when user change listbox by mouse there is an event straight for this(SelectionChangeCommitted), but it's availabe only on combbox control i need to use listbox for htis. how can i achive such behaviour secondly: when i first run the form, the selectedValueChanged event always fired once again selecting the firs index in the listbox how to stop this also i want to run the form without any event being fired unless the user change the value manually thanks
modified on Tuesday, October 27, 2009 7:04 AM
-
I'm populating a listbox using it's DataSource property in form's load event what happens is that the selectedValueChanged event or selectedIndexChanged fires twice when starting the form how can i prevent such behaviour i want the event to fire only when user change listbox by mouse there is an event straight for this(SelectionChangeCommitted), but it's availabe only on combbox control i need to use listbox for htis. how can i achive such behaviour secondly: when i first run the form, the selectedValueChanged event always fired once again selecting the firs index in the listbox how to stop this also i want to run the form without any event being fired unless the user change the value manually thanks
modified on Tuesday, October 27, 2009 7:04 AM
Your
ListBox
is behaving as designed and as far as I am aware it is not possible to stop it. However one of the many ways to cope with the problem is as follows: 1) Modify theConstructor
for yourForm
private bool stillInitializing = false; //<========== NEW CODE =================== public MyForm() { this.stillInitializing = true; //<========== NEW CODE =================== InitializeComponent(); this.stillInitializing = false; //<========== NEW CODE =================== }
-
Modify your event handler(s)
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (this.stillInitializing)
{
return;
}// your code goes here
}
Hope this helps! :)
Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.”
-
-
Your
ListBox
is behaving as designed and as far as I am aware it is not possible to stop it. However one of the many ways to cope with the problem is as follows: 1) Modify theConstructor
for yourForm
private bool stillInitializing = false; //<========== NEW CODE =================== public MyForm() { this.stillInitializing = true; //<========== NEW CODE =================== InitializeComponent(); this.stillInitializing = false; //<========== NEW CODE =================== }
-
Modify your event handler(s)
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (this.stillInitializing)
{
return;
}// your code goes here
}
Hope this helps! :)
Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.”
didn't work when applied on form construcor so i tried this
private void Form1_Load(object sender, EventArgs e)
{
this.stillInitializing = true; //NEW CODEunitMeasureCollection.GetMulti(null); vendorCollection.GetMulti(null); shipMethodCollection.GetMulti(null); purchaseOrderDetailCollection.GetMulti(null); /\*PredicateExpression filter = new PredicateExpression(UnitMeasureFields.UnitMeasureCode == "BOX"); unitMeasureCollection1.GetMulti(filter); \*/ comboBoxUnitMeasure.DataSource = unitMeasureCollection; comboBoxUnitMeasure.ValueMember = "UnitMeasureCode"; listBoxVendor.DataSource = vendorCollection; listBoxVendor.DisplayMember = "Name"; listBoxVendor.ValueMember = "VendorID"; listBoxShipMethod.DataSource = shipMethodCollection; listBoxShipMethod.DisplayMember = "Name"; listBoxShipMethod.ValueMember = "ShipMethodID"; dataGridView1.DataSource = purchaseOrderDetailCollection; PopulateCategoriesAndSubCategories(); //To clear textboxes affected by lisbox event that fired automatically ClearTextBoxes(); DisableButtons(); this.stillInitializing = false; //NEW CODE }
and it worked just fine thanks, Henry Minute :)
-
-
didn't work when applied on form construcor so i tried this
private void Form1_Load(object sender, EventArgs e)
{
this.stillInitializing = true; //NEW CODEunitMeasureCollection.GetMulti(null); vendorCollection.GetMulti(null); shipMethodCollection.GetMulti(null); purchaseOrderDetailCollection.GetMulti(null); /\*PredicateExpression filter = new PredicateExpression(UnitMeasureFields.UnitMeasureCode == "BOX"); unitMeasureCollection1.GetMulti(filter); \*/ comboBoxUnitMeasure.DataSource = unitMeasureCollection; comboBoxUnitMeasure.ValueMember = "UnitMeasureCode"; listBoxVendor.DataSource = vendorCollection; listBoxVendor.DisplayMember = "Name"; listBoxVendor.ValueMember = "VendorID"; listBoxShipMethod.DataSource = shipMethodCollection; listBoxShipMethod.DisplayMember = "Name"; listBoxShipMethod.ValueMember = "ShipMethodID"; dataGridView1.DataSource = purchaseOrderDetailCollection; PopulateCategoriesAndSubCategories(); //To clear textboxes affected by lisbox event that fired automatically ClearTextBoxes(); DisableButtons(); this.stillInitializing = false; //NEW CODE }
and it worked just fine thanks, Henry Minute :)
Good stuff! :thumbsup: It is nice to attempt to help someone with the intelligence to see the basis of your answer and apply it for their own circumstances. :)
Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.”