Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dropdownlist check if index changed by code or by selection

Tags:

c#

winforms

I have a DropDownList in a project. This DropDownList contains a SelectedIndexChanged event:

private void cbo_SelectedIndexChanged(object sender, EventArgs e){......}

Is it possible to check if the index was changed in the code, like:

cbo.SelectedIndex = placering;

, or if the change happened by user interaction?

like image 353
Moelbeck Avatar asked Apr 14 '16 11:04

Moelbeck


1 Answers

Since DropDownList doesn't have property Focused as it is for ComboBox control in WinForms it's not that easy. One way is to add custom flag, and change its value before changing the SelectedIndex property. Inside of event handler you can check for this flag and reset its value :

private volatile bool isAutoFired = false;

Then somewhere in code :

isAutoFired = true;
cbo.SelectedIndex = placering;


private void cbo_SelectedIndexChanged(object sender, EventArgs e)
{       
   if(!isAutoFired)
   {
      // event is fired by user 
   }

   isAutoFired = false;
}
like image 144
Fabjan Avatar answered Nov 15 '22 06:11

Fabjan