[英]Is there a way to clear a TextBox's text without TextChanged firing?
在我的應用程序中,我想在某些情況下處理TextBox
輸入(例如,某些條件沒有填充),因為KeyDown
僅對鍵盤輸入有用,但不是從剪貼板實際粘貼(我不想通過無論如何使用Win32調用這樣做的麻煩),我想我只是處理我的主TextBox的TextChanged
事件中的所有內容。 但是,當出現“錯誤”並且用戶無法輸入時,如果我要調用TextBox.Clear();
在它上面,TextChanged第二次觸發,可以理解,因此消息也會顯示兩次。 那有點煩人。 我只能在這種情況下處理TextChanged? 示例代碼(在txtMyText_TextChanged
):
if (txtMyOtherText.Text == string.Empty)
{
MessageBox.Show("The other text field should not be empty.");
txtMyText.Clear(); // This fires the TextChanged event a second time, which I don't want.
return;
}
如何在更改之前斷開事件處理程序並在之后重新連接?
if (txtMyOtherText.Text == string.Empty)
{
MessageBox.Show("The other text field should not be empty.");
txtMyText.TextChanged -= textMyText_TextChanged;
txtMyText.Clear();
txtMyText.TextChanged += textMyText_TextChanged;
return;
}
在更復雜的情況下,最好有一個try / finally並重新啟用finally部分中的TextChanged事件
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.