有什么办法不调用Control Event吗?
假设有类似这样的事件 ComboBox_SelectedIndexChange
private void MyComboBox_SelectedIndexChanged(object sender, EventArgs e)
{
//do something//
}
我有一个更改 ComboBox 值的函数。
Private void MyFunction()
{
MyComboBox.Text = "New Value";
}
我可以让 MyFunction 在更改 MyComboBox 的值时阻止调用事件 MyComboBox_SelectedIndexChanged 吗?
Let's say there is event ComboBox_SelectedIndexChange something like this
private void MyComboBox_SelectedIndexChanged(object sender, EventArgs e)
{
//do something//
}
And i have a function which changes value of ComboBox.
Private void MyFunction()
{
MyComboBox.Text = "New Value";
}
Can i make MyFunction prevent from calling the event MyComboBox_SelectedIndexChanged while changing the value of MyComboBox?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不,你不能。您有两个基本选项,它们都完成相同的任务:
您可以从控件中取消事件处理程序方法,设置值,然后将事件处理程序方法重新附加到控件。例如:
您可以声明一个类级字段,该字段将跟踪该值是通过编程方式更新还是由用户更新。当您想要以编程方式更新组合框时设置该字段,并在
SelectedIndexChanged
事件处理程序方法中验证其值。例如:
No, you cannot. You have two fundamental options, both of which accomplish the same thing:
You can unhook the event handler method from the control, set the value, and then reattach the event handler method to the control. For example:
You can declare a class-level field that will keep track of whether the value was updated programmatically or by the user. Set the field when you want to update the combo box programmatically, and verify its value in the
SelectedIndexChanged
event handler method.For example:
您可以附加或分离事件处理程序。
或者
You may attach or detach an event handler.
Or