开发者

Is it possible to raise an event of other control on selecting another

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_CheckedChangedon drodown_SelectedIndexChanged. Can any one tel开发者_如何学编程l me the best way to do


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()
{
    // ...
}


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.


Could you just update radio.Checked property so it will trigger an event automatically? The event holder is aware when to raise an event.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜