简体   繁体   中英

Firing a OnSelectedIndexChanged event within code

As well as this event firing from the drop down, I would also like to be able to fire it from my code. Is this possible and if so how?

Thanks

Stu

The best way to do this is to wrap the code from the event into a method, and call it from both places.

BUT if you did want to call the event directly (which I don't recommend), you can do it like this:

MyComboBox_SelectedIndexChanged(null, null);

(Unless you use either the sender or EventArgs within the method, then you will need to supply these too)

If you want to fire the SelectedIndexChanged event from code you could either call the event handler like CmbBox_SelectedIndexChanged(null, null); or do

   if(CmbBox.SelectedIndex <> index)//let index is a int variable
      CmbBox.SelectedIndex = index;       
   else
   {
      CmbBox.SelectedIndex = -1;
      CmbBox.SelectedIndex = index;
   }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM