也许我只是个白痴,但我似乎找不到在离开的同时为文本框触发的事件,但只有在文本框的内容发生变化时才会触发。有点像 textchanged 和 leave 的组合。我不能使用 textchanged,因为它会在每次击键时触发。现在,我将文本框的当前值存储在一个变量中,并在离开事件时对其进行比较,但这看起来真的很老套。
谢谢
最佳答案
您可以创建自己的(派生的)类来覆盖 OnEnter、OnLeave 和 OnTextChanged 以设置标志并触发“您的”事件。
像这样:
public class TextBox: System.Windows.Forms.TextBox {
public event EventHandler LeaveWithChangedText;
private bool textChanged;
protected override void OnEnter(EventArgs e) {
textChanged = false;
base.OnEnter(e);
}
protected override void OnLeave(EventArgs e) {
base.OnLeave(e);
if (textChanged) {
OnLeaveWithChangedText(e);
}
}
protected virtual void OnLeaveWithChangedText(EventArgs e) {
if (LeaveWithChangedText != null) {
LeaveWithChangedText(this, e);
}
}
protected override void OnTextChanged(EventArgs e) {
textChanged = true;
base.OnTextChanged(e);
}
}
关于c# - Windows 窗体文本在离开事件时更改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/808847/