我正在製作一個winform
應用程序C#
與visual studio 2013
。在我的表格上,我有幾個text-boxes
和幾個buttons
。C#Winform文本框驗證中斷取消按鈕
更具體地說,我有一個cancelButton
,當點擊將關閉應用程序,這是與這個事件在這裏處理。
private void cancelButton_Click(object sender, EventArgs e)
{
//close the application
Application.Exit();
}
我有一個TextBox_Validated
事件看起來是這樣的
private void aTextBox_Validated(object sender, EventArgs e)
{
int matchIntPosition;
string someString = aTextBox.Text.ToUpper();
//check a string array to see if the value entered exsists
matchIntPosition= Array.IndexOf(someStringArray, someString);
if (matchIntPosition > -1)
{
string someString = someStringArray[matchIntPosition];
}
else
{
MessageBox.Show("This value does not exist, Please try again.");
aTextBox.Focus();
}
}
現在我強迫用戶,以確保繼續我的形式之前,這個領域是正確填寫(這是第一次我的表單上的文本框)。
這是我的問題所在,當我點擊我的cancelButton時,它只是在我的aTextBox_Validation
事件中點擊了else
。強制我輸入正確的值,然後才能點擊取消按鈕。
我已經做了一些谷歌試圖找出我如何解決它。我試過的一些東西是:
cancelButton.CausesValidation = false;
在我的cancelButton_Click
事件中,沒有工作。
試圖設置一個bool
標誌做與^幾乎相同的事情,但似乎也沒有工作。
任何想法或推到正確的地方?
哪裏聲明someStringArray變量?它是否包含someString變量值? – yosbel
我會嘗試驗證提交按鈕單擊上的所有文本框。這是很正常的形式行爲。它可以解決您的問題,並防止最終客戶對您繼續關注它們感到沮喪。 – Jonesopolis
@yosoy陣列在課堂上,你錯過了我的問題。 – Zoxac