honar abubakr

honar abubakr

  • NA
  • 73
  • 110.8k

check all textboxes to be non-empty on button click

Aug 12 2013 3:36 AM
hi friends i have a form in c# with 3 textboxes and a button ... i there to validate textboxes to be non-empty on button ... i want to make each textbox change bgcolor if is empty on button click and stop the button click event?

Answers (5)

0
Satyapriya Nayak

Satyapriya Nayak

  • 0
  • 39.3k
  • 13.3m
Aug 12 2013 4:08 AM
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace Validation_on_textbox { public partial class Form2 : Form { public Form2() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { foreach (Control c1 in this.Controls) { if (c1 is TextBox) { if (c1.Text == "") { MessageBox.Show("Name Field cannnot be blank"); c1.BackColor = Color.Orange; textBox1.Focus(); } else { MessageBox.Show("Correct"); c1.BackColor = Color.White; } } } } } }
0
Anubhav Chaudhary

Anubhav Chaudhary

  • 27
  • 41.8k
  • 20m
Aug 12 2013 3:58 AM
Just write this code on the click of button and your problem will be solved.

        private void button1_Click(object sender, EventArgs e)
        {
            if(textBox1.Text=="")
            {
                textBox1.BackColor = Color.Red;
            }

        }
0
Mahesh Babu

Mahesh Babu

  • 0
  • 6
  • 2.1k
Aug 12 2013 3:50 AM
this only console program.......
0
Iftikar Hussain

Iftikar Hussain

  • 0
  • 12.6k
  • 449.2k
Aug 12 2013 3:48 AM
Hi,
        You can do like this

private void button1_Click(object sender, EventArgs e)
{
            bool isValid = true;
            textBox1.BackColor = System.Drawing.Color.White;
            textBox2.BackColor = System.Drawing.Color.White;
            textBox3.BackColor = System.Drawing.Color.White;
            if (textBox1.Text == "")
            {
                isValid = false;
                textBox1.BackColor = System.Drawing.Color.Red;
            }
            if (textBox2.Text == "")
            {
                isValid = false;
                textBox2.BackColor = System.Drawing.Color.Red;
            }
            if (textBox3.Text == "")
            {
                isValid = false;
                textBox3.BackColor = System.Drawing.Color.Red;
            }
            if (isValid)
            {
                //do your code
            }
}

Regards,
Iftikar
0
Mahesh Babu

Mahesh Babu

  • 0
  • 6
  • 2.1k
Aug 12 2013 3:46 AM
still it is showing the only my project name i.e oppsproject only...it is not showing my added class