I am having a drop down and a radio button on my form. What i would like to do is i would like to call radio_CheckedChanged
on drodown_SelectedIndexChanged
. Can any one tell me the best way to do
4 Answers
It's not possible to raise an event from outside the class the defines it, unless you inherit from it.
If your goal is to perform the same actions on both the RadioButton.CheckedChanged and DropDownList.SelectedIndexChanged events, you could extract the code in a separate method and invoke it from both event handlers:
public void radioButton_CheckedChanged(object sender, EventArgs e)
{
DoSomething();
}
public void dropDownList_SelectedIndexChanged(object sender, EventArgs e)
{
DoSomething();
}
private void DoSomething()
{
// ...
}

- 1
- 1

- 56,676
- 12
- 126
- 154
I may be over simplifying this but in your dropdown_SelectedIndexChanged you can just set the radio button value, which will fire the checked change event or if you don't want the value to change just call radio_CheckedChanged from within your dropdown_SelectedIndexChanged event.

- 6,703
- 28
- 26
Could you just update radio.Checked
property so it will trigger an event automatically? The event holder is aware when to raise an event.

- 61,540
- 22
- 104
- 156
Just write radio_CheckedChanged(sender, EventArgs.Empty)
. Its very much like calling a method which is infact what it is.
EDIT
This is not an elegant way to do this so it would be better if you could encapsulate the code logic into seperate methods and then call these in the respective handlers based on your conditions

- 37,194
- 9
- 78
- 82
-
1IMO this is not a good solution. Event handlers should only be invoked as a consequence of an event being fired, as defined by the [Observer Design Pattern](http://sourcemaking.com/design_patterns/observer), not directly. – Enrico Campidoglio Jul 01 '11 at 09:22
-
Agreed with upper comment, in further support of this code there will be a lot of troubles – zabulus Jul 01 '11 at 09:27
-
So can any one give the better way then – Vivekh Jul 01 '11 at 09:30
-
@user552236 you can go with what Enrico suggests – V4Vendetta Jul 01 '11 at 09:33