Click here to Skip to main content
15,904,416 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I am using a textbox to input a name in a window application. I want to make this a required field. How do I catch the exception if the textbox is left blank.
Thanks,
Jyoti Bhatnagar
Posted

There is no exception if a TextBox is left blank - you have to check for it yourself, normally either in the LostFocus event, or on a button press or similar action.
 
Share this answer
 
Empty input is not an exception. It is a matter of input validation. I suppose your input field is empty by default, but you want to validate the form on a specific event. Let's suppose on a button click.
You have several ways to do that, but start with these articles:
http://www.devx.com/dotnet/Article/33506[^]
Validate user input in Windows Forms[^]
Validators for Windows Forms - ValidationProvider Control[^]
 
Share this answer
 
Hi you can use following condition to check if text box is empty,

if(String.IsNullOrEmpty(txtYourTextbox.Text))
{
//if textbox is empty, you can show proper message using MessageBox
MessageBox.Show("Please enter value in text box", "TextboxLabel is Required", MessageBoxButtons.OK, MessageBoxIcon.Information);
}


Hope this solves your problem.

Thanks,
Sanjeev Choudhary
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900