C# 文本更新时组合框的验证事件
我有一个comboxbox(好的,在现实中,我有一个ToolStripComboBox),我想要一个在特定条件下触发的可取消事件:C# 文本更新时组合框的验证事件,c#,winforms,C#,Winforms,我有一个comboxbox(好的,在现实中,我有一个ToolStripComboBox),我想要一个在特定条件下触发的可取消事件: 焦点丢失 获得焦点 从框中选择的项目 按回车键 这是一个“正常”的验证事件,但当我执行以下操作时 this.speedSelector.Validating += new System.ComponentModel.CancelEventHandler(this.speedSelector_Validating); 此事件仅在我尝试通过[X]关闭应用程序时触
- 焦点丢失
- 获得焦点
- 从框中选择的项目
- 按回车键
this.speedSelector.Validating
+= new System.ComponentModel.CancelEventHandler(this.speedSelector_Validating);
此事件仅在我尝试通过[X]关闭应用程序时触发。此外,当出现无效文本时,我不能离开应用程序,这是可行的,但如何在我的上述条件下触发该事件
关于,将焦点从对话框中CausesValidation属性设置为true的控件移动到另一个CausesValidation属性设置为true的控件时,将调用Validating,例如从TextBox控件移动到OK按钮。关闭窗口时可能会发生验证,因为您在窗口上设置了CausesValidation,而不是在相应的控件上
您还可以将所有验证移到控件的OnBlur事件中,并以这种方式执行。您可能需要将初始值存储在某个位置(例如控件的通用标记字段) 您可以在以下任何事件上验证控件:SelectedIndexChanged、SelectionChanged、TextUpdate等 当控件获得或失去焦点时,控件中存储的值不应更改
public Form1() {
InitializeComponent();
speedSelector.Tag = speedSelector.Text;
speedSelector.SelectedIndexChanged += new System.EventHandler(this.speedSelector_Changed);
speedSelector.SelectionChangeCommitted += new System.EventHandler(this.speedSelector_Changed);
speedSelector.TextUpdate += new System.EventHandler(this.speedSelector_Changed);
}
private void speedSelector_Changed(object sender, EventArgs e) {
if (validData(speedSelector.Text)) {
speedSelector.Tag = speedSelector.Text;
} else {
speedSelector.Text = speedSelector.Tag.ToString();
}
}
private static bool validData(string value) {
bool result = false;
// do your test here
return result;
}