Validating TextBox in Form
I am validating textbox1 of below attached project, but i am getting error. Please check it.
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Iftikar HussainRank #423 · 5.9K reputationPosted Aug 11, 2013, 6:26 AM EDT
Regards,
Iftikar
Anurag SarkarRank #1,049 · 1.8K reputationPosted Aug 10, 2013, 3:30 AM EDT
{
if(textBox1.text=""){MessageBox.Show("Enter Number");return;}
if(textBox2.text=""){MessageBox.Show("Enter Second Number");return;}
int s1, s2, s3;
string s;
s1 = Convert.ToInt32(textBox1.Text);
s2 = Convert.ToInt32(textBox2.Text);
s3 = s1 + s2;
s = Convert.ToString(s3);
frmTest2 myObjViewer = new frmTest2();
// hiding the first form.
this.Hide();
myObjViewer.Controls["label2"].Text = s;
// displaying the second form
myObjViewer.ShowDialog();
}
Just Put this Code in your form 1.
Preeti AgrawallaRank #6,677 · 259 reputationPosted Aug 10, 2013, 3:31 AM EDT
http://www.codeproject.com/Articles/13922/Validate-user-input-in-Windows-Forms
But i m getting error.
Preeti AgrawallaRank #6,677 · 259 reputationPosted Aug 10, 2013, 3:33 AM EDT
Anurag SarkarRank #1,049 · 1.8K reputationPosted Aug 10, 2013, 3:34 AM EDT
Anurag SarkarRank #1,049 · 1.8K reputationPosted Aug 10, 2013, 3:35 AM EDT
Preeti AgrawallaRank #6,677 · 259 reputationPosted Aug 10, 2013, 3:37 AM EDT
Anurag SarkarRank #1,049 · 1.8K reputationPosted Aug 10, 2013, 3:58 AM EDT
private void button2_Click(object sender, EventArgs e)
{
if (textBox3.Text == "")
{
this.errorProvider1.SetError(textBox3, "This Cannott be Blank");
return;
}
if (textBox4.Text == "")
{
this.errorProvider1.SetError(textBox4, "This Cannott be Blank");
return;
}
int s1, s2, s3;
string s;
s1 = Convert.ToInt32(textBox3.Text);
s2 = Convert.ToInt32(textBox4.Text);
s3 = s1 + s2;
s = Convert.ToString(s3);
frmTest2 myObjViewer = new frmTest2();
// hiding the first form.
this.Hide();
myObjViewer.Controls["label2"].Text = s;
// displaying the second form
myObjViewer.ShowDialog();
}
Just run the application you will get the result.
While you will be creating new project you need to drag a error provide control from the toolbox.
Have a look at the sample.
Preeti AgrawallaRank #6,677 · 259 reputationPosted Aug 10, 2013, 7:35 AM EDT
Iftikar HussainRank #423 · 5.9K reputationPosted Aug 10, 2013, 7:59 AM EDT
Try like this
this.textBox1.KeyPress += new KeyPressEventHandler(this.textBox1_KeyPress);
Regards,
Iftikar
Anurag SarkarRank #1,049 · 1.8K reputationPosted Aug 10, 2013, 10:08 AM EDT
{
if (char.IsDigit(e.KeyChar) || char.IsControl(e.KeyChar))
{
e.Handled = false;
}
else
{
e.Handled = true;
}
}
You should try to this code.
Preeti AgrawallaRank #6,677 · 259 reputationPosted Aug 11, 2013, 1:56 AM EDT
Satyapriya NayakRank #47 · 31.5K reputationPosted Aug 11, 2013, 4:11 AM EDT
Preeti AgrawallaRank #6,677 · 259 reputationPosted Aug 11, 2013, 4:50 AM EDT
Preeti AgrawallaRank #6,677 · 259 reputationPosted Aug 11, 2013, 9:17 AM EDT
Iftikar HussainRank #423 · 5.9K reputationPosted Aug 11, 2013, 9:21 AM EDT
http://www.c-sharpcorner.com/uploadfile/mahesh/maskedtextbox-in-C-Sharp/
Regards,
Iftikar