Click to See Complete Forum and Search --> : textbox validation


johnsanthosh
July 20th, 2005, 04:00 AM
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 text box and a insert button
now when iam entering the text it should be inserted when the button is clicked.
now wht i need is that when teh text box is empty and when i clicked teh button , it should show a message saying that textbox is empty

I Think this is very smalll problem. But as ia m new, pls guide me to do it .

j o h n

Shuja Ali
July 20th, 2005, 04:27 AM
This is the code you should write in the Click Event handler of your button

private void button1_Click(object sender, System.EventArgs e)
{
//Check the length of the text
if (textBox1.Text.Length == 0 )
{
//show a message
MessageBox.Show ("Textbox cannot be empty");
//set focus back to textbox
textBox1.Focus();
}
else
{
MessageBox.Show ("You entered : " + textBox1.Text);
}
}