Click here to Skip to main content
15,868,016 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I am completely new to c# .so i am just into basics.Just now i am beginning to work with windows forms.Now i have to add a 2 text boxes and 1 insert button now when i'm entering the textboxes it should be inserted when the button is clicked.Now what i need is that when the text boxes is empty and when i clicked the button , it should show a error.I Think this is very small problem. But as i'm new, pl guide me to do it .

Regards
Balamurugan
Posted
Updated 4-Oct-20 0:57am

Lets say you textbox name ID is txtBox. then try this in your submit button code:

C#
if(txtBox.Text.Trim() == string.Empty)
{
 Messagebox.Show("Please enter something in the textbox");
 return; // return because we don't want to run normal code of buton click
}

// your normal button code will some here
 
Share this answer
 
Comments
islam_ashraful 14-Sep-13 3:30am    
Thanks Rahul
EdH2307 23-Oct-20 15:52pm    
best solution
try this out,
C#
if (TextBox1.Text == "")
                    MessageBox.Show("Enter Some Text in TextBox1");
 
Share this answer
 
v2
Comments
Balamurugan1989 21-Nov-12 1:16am    
It shows the error i.e "Object Reference not set to an instance of object"
[no name] 21-Nov-12 1:18am    
I have updated the soultion, try that one..
Balamurugan1989 21-Nov-12 1:20am    
I tried ur code only...it is not working...
[no name] 21-Nov-12 1:24am    
Tell me,Where are you writing this code ??
And in my code, its working.. ok Try to put Breakpoint and debug it...
fjdiewornncalwe 21-Nov-12 9:58am    
+5. Correct. (I would suggest using string.Empty instead of "" in the comparison though.)
For Empty textbox validation we need to follow the below code:
C#
if (textBox1.Text == "")
{
   string myStringVariable1 = string.Empty;
   MessageBox.Show("User Name is requierd");
}
else if (textBox2.Text == "")
{
   string myStringVariable2 = string.Empty;
   MessageBox.Show("Password is requierd");
}
//For combobox validation we should follow the below code.
else if (comboBox1.Text == "--Choose--")
{
   string myStringVariable3 = string.Empty;
   MessageBox.Show("Select User Type");
}
 
Share this answer
 
v2
Comments
Silvabolt 11-Jul-13 12:21pm    
This would only show one error at a time. Rather than doing this, you can check all fields at once, save them and notify user all of the errors at once. Furthermore, string.Empty should be used instead of "". Also, all of the solutions including these ones will break if textbox happens to be null. Solution 3 makes a good remark about the 'return', because this is probably handled in an event handler method. I know this is old, but none of these solutions are really complete.
//all the ways are right...........

/*
if(pswrd_textbox.Text== " ") //pswrd.textbox is a password textbox
{
MessageBox.Show("This field cannot left empty!");
}
*/

/*
if(pswrd_textbox.Text.Trim() == string.Empty)
{
MessageBox.Show("This Field cannot left empty!");
}
*/

/*
if( string.IsNullOrEmpty(pswrd_textbox.Text))
{
MessageBox.Show("Don't left this field Empty!");
}
*/
 
Share this answer
 
Comments
CHill60 5-Oct-20 5:04am    
You are going to start getting downvotes (not from me yet) for several reasons
1. The main reason - this question was asked, answered and answers accepted nearly 8 years ago. You have resurrected a really old post and that is not popular in this forum.
2. You have repeated solutions that have already been posted, most people will miss the fact that the use of string.IsNullOrEmpty was not mentioned previously
3. Your code is unformatted which just makes the points above seem worse

Stick to answering more recent questions where the OP (Original Poster) still needs help and make sure you are not repeating what someone else has already said. If you must repeat something then call it out / draw attention to the original. And please learn how to format your code - see the menu on the top of the solution box and use the highlighted "code" formatting.

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