我想要驗證用戶在提交表單之前總是在文本框中輸入一個值。但是我已經提供的支票允許用戶輸入空格並繼續提交表單。 那麼,如何把檢查,使用戶無法提交表單,如果在文本框中只有空格。Windows窗體中的文本框驗證
4
A
回答
3
您可以製作自己的自定義驗證功能。這可能很幼稚,但不知何故,它會起作用。
private bool WithErrors()
{
if(textBox1.Text.Trim() == String.Empty)
return true; // Returns true if no input or only space is found
if(textBox2.Text.Trim() == String.Empty)
return true;
// Other textBoxes.
return false;
}
private void buttonSubmit_Click(object sender, EventArgs e)
{
if(WithErrors())
{
// Notify user for error.
}
else
{
// Do whatever here... Submit
}
}
3
在NET4.0有一個很好的功能
if(string.IsNullOrWhiteSpace(textBox1.Text))
{
//raise your validation exception
}
else {
//go to submit
}
1
它可以方便地使用此錯誤提供商的code.Error提供者,你可以在你的工具箱中找到完成。
private void btnsubmit_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(txtname.Text))
{
txtname.Focus();
errorProvider1.SetError(txtname, "Please Enter User Name");
}
if (string.IsNullOrEmpty(txtroll.Text)) {
txtroll.Focus();
errorProvider1.SetError(txtroll, "Please Enter Student Roll NO");
}
}
這裏是輸出圖像
相關問題
- 1. Windows窗體文本框驗證
- 2. Windows窗體中的文本驗證
- 3. 檢查驗證文本在文本框+ Windows窗體
- 4. 如何驗證Windows窗體中電話號碼的文本框?
- 5. 在Windows窗體應用程序驗證文本框
- 6. 在windows窗體應用程序驗證文本框
- 7. Windows窗體中的小數文本框
- 8. Xamarin窗體 - Windows驗證webapi
- 9. 在Windows窗體驗證
- 10. Windows窗體的文本框回車鍵
- 11. 同時驗證C#窗體上的幾個文本框
- 12. Windows窗體 - 驗證DataGridView的輸入
- 13. Windows窗體文本框顯示圖像
- 14. Windows窗體對象和文本框
- 15. 動態Windows窗體文本框
- 16. Windows窗體文本框限制
- 17. Windows C#窗體:關注文本框
- 18. WPF Windows身份驗證文本框
- 19. Symfony窗體文件驗證
- 20. 窗體驗證?
- 21. django中的窗體驗證
- 22. 實體框架 - 驗證文本格式
- 23. SignalR - 驗證Windows窗體服務器上的窗體用戶
- 24. 使文本在Windows窗體中的富文本框中以粗體顯示
- 25. 窗體驗證器對於Windows Phone
- 26. ASP.NET窗體身份驗證與Windows Safari
- 27. windows窗體驗證問題(.net2.0)
- 28. MVC - 窗體和Windows身份驗證
- 29. 驗證在文本框中
- 30. 驗證文本框
您已經標記了問題的WinForms,但它聽起來就好像你問web表單。這是什麼? – 2010-11-17 09:46:54