Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > How can I validate the textbox control
 

How can I validate the textbox control

Hi,

I am building windows forms application using VS2008 targeting .net framework 3.5. I want to validate 3 textbox controls for values. I just want to validate that if these 3 text box have values then they then user can proceed further on button click and if values are not filled for any of the three textboxes, then the button on form does nothing and displays error using error provider.

Currently when i am using validating event for all textboxes, then even of value is not filled and error provider provides description of error, but still user is able to click the button and processing continues.

Please help.
Rahul_IT_Developer  Wednesday, August 19, 2009 9:59 AM
Add key prees event for each textbox:
Intialy disable your button in form_load
e.g:
private
void textBox1_KeyUp(object sender, KeyEventArgs e)
{
if (String.IsNullOrEmpty(textBox1.Text.Trim()) && String.IsNullOrEmpty(textBox2.Text.Trim()) && String.IsNullOrEmpty(textBox2.Text.Trim()))
{
//disable your button
}
else
{
//enable your button
}

Please mark the post as answer if it is helpfull to you because it boosts the members to answer more and more.
_SuDhiR_  Wednesday, August 19, 2009 11:18 AM
Hi,

I hope it will help you

 private void btnOk_Click(object sender, EventArgs e)
{
	if (Validate())
	{
		Form2 frm = new Form2();
		frm.Show();
	}
	else
	{
		DiableOk();
	}
  
}
private bool Validate()
{
	if (String.IsNullOrEmpty(textBox1.Text.Trim()) && String.IsNullOrEmpty(textBox2.Text.Trim())
	&& String.IsNullOrEmpty(textBox2.Text.Trim()))
	{
		errorProvider1.SetError(textBox1, "Please Enter Value");
		
		return false;
	}
	return true;
}

private void EnableOK()
{
	btnOk.Enabled = true;
	btnClear.Enabled = false;
}
private void DiableOk()
{
	btnOk.Enabled = false;
	btnClear.Enabled = true;
}
private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
	EnableOK();
	errorProvider1.Clear();
}

private void btnClear_Click(object sender, EventArgs e)
{
	EnableOK();
	errorProvider1.Clear();
}

Best Regards, C.Gnanadurai ----------------------- Please mark the post as answer if it is helpfull to you
Gnanadurai  Thursday, August 20, 2009 6:57 AM
Hi,
I hope it will help you

if (String.IsNullOrEmpty(textBox1.Text.Trim()) && String.IsNullOrEmpty(textBox2.Text.Trim()) && String.IsNullOrEmpty(textBox2.Text.Trim()))
{
	errorProvider1.SetError(textBox1, "Please Enter Value");
}

Best Regards, C.Gnanadurai ----------------------- Please mark the post as answer if it is helpfull to you
Gnanadurai  Wednesday, August 19, 2009 10:12 AM
Hi,
I hope it will help you

if
 (String.IsNullOrEmpty(textBox1.Text.Trim()) && String.IsNullOrEmpty(textBox2.Text.Trim()) && String.IsNullOrEmpty(textBox2.Text.Trim()))
{
	errorProvider1.SetError(textBox1, "Please Enter Value"
);
}

Best Regards, C.Gnanadurai ----------------------- Please mark the post as answer if it is helpfull to you

hi,

this is what i did in the first place but still also i want to stop user from clicking the button or executing the code on button click.
Rahul_IT_Developer  Wednesday, August 19, 2009 10:53 AM
Add key prees event for each textbox:
Intialy disable your button in form_load
e.g:
private
void textBox1_KeyUp(object sender, KeyEventArgs e)
{
if (String.IsNullOrEmpty(textBox1.Text.Trim()) && String.IsNullOrEmpty(textBox2.Text.Trim()) && String.IsNullOrEmpty(textBox2.Text.Trim()))
{
//disable your button
}
else
{
//enable your button
}

Please mark the post as answer if it is helpfull to you because it boosts the members to answer more and more.
_SuDhiR_  Wednesday, August 19, 2009 11:18 AM
Hi,
I hope it will help you
 private void button1_Click(object sender, EventArgs e)
{
	if (Validate())
	{
		Form2 frm = new Form2();
		frm.Show();
	}
	else
	{
		button1.Enabled = false;
	}
  
}
private bool Validate()
{
	if (String.IsNullOrEmpty(textBox1.Text.Trim()) && String.IsNullOrEmpty(textBox2.Text.Trim()) && String.IsNullOrEmpty(textBox2.Text.Trim()))
	{
		errorProvider1.SetError(textBox1, "Please Enter Value");
		
		return false;
	}
	return true;
}

Best Regards, C.Gnanadurai ----------------------- Please mark the post as answer if it is helpfull to you
Gnanadurai  Wednesday, August 19, 2009 11:54 AM
Hi,
I hope it will help you
 private


 void


 button1_Click(object


 sender, EventArgs e)
{
if (Validate())
{
Form2 frm = new Form2();
frm.Show();
}
else
{
button1.Enabled = false ;
}

}
private bool Validate()
{
if (String.IsNullOrEmpty(textBox1.Text.Trim()) && String.IsNullOrEmpty(textBox2.Text.Trim()) && String.IsNullOrEmpty(textBox2.Text.Trim()))
{
errorProvider1.SetError(textBox1, "Please Enter Value" );

return false ;
}
return true ;
}

Best Regards, C.Gnanadurai ----------------------- Please mark the post as answer if it is helpfull to you
The code has a issue, when the button gets disabled if validate returns false and even if user fill the correct values for textboxes, the button will remain disabled so the button click event won't work. Please advise?
Rahul_IT_Developer  Thursday, August 20, 2009 6:21 AM

Have you tried my Solution I tested it hope that will help you?

Let me know if you have any issues.


Please mark the post as answer if it is helpfull to you because it boosts the members to answer more and more.
_SuDhiR_  Thursday, August 20, 2009 6:51 AM
Hi,

I hope it will help you

 private void btnOk_Click(object sender, EventArgs e)
{
	if (Validate())
	{
		Form2 frm = new Form2();
		frm.Show();
	}
	else
	{
		DiableOk();
	}
  
}
private bool Validate()
{
	if (String.IsNullOrEmpty(textBox1.Text.Trim()) && String.IsNullOrEmpty(textBox2.Text.Trim())
	&& String.IsNullOrEmpty(textBox2.Text.Trim()))
	{
		errorProvider1.SetError(textBox1, "Please Enter Value");
		
		return false;
	}
	return true;
}

private void EnableOK()
{
	btnOk.Enabled = true;
	btnClear.Enabled = false;
}
private void DiableOk()
{
	btnOk.Enabled = false;
	btnClear.Enabled = true;
}
private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
	EnableOK();
	errorProvider1.Clear();
}

private void btnClear_Click(object sender, EventArgs e)
{
	EnableOK();
	errorProvider1.Clear();
}

Best Regards, C.Gnanadurai ----------------------- Please mark the post as answer if it is helpfull to you
Gnanadurai  Thursday, August 20, 2009 6:57 AM
Hi Sudhir, Gnanadurai

I am implementing a different solution for this stuff. The solution is somewhat similar to how Gnanadurai suggested but different in processing. Thank you all for your help.
Rahul_IT_Developer  Thursday, August 20, 2009 7:09 AM

You can use google to search for other answers

Custom Search

More Threads

• Why is a cell ALWAYS selected in my DataGridView
• Datagridview Printing
• DataGridView Stop auto scrolling selection/current into view
• Datagrid select
• Update Expression Column in DataGrid
• Datagrid checkbox and update
• simple question about cell parsing
• Why is this working the way it does?
• DataGridView thinks it has more rows than it has - and exceptions are not catched
• Combine datatable common rows into single row with comma seperated cell