Hi Guys!
"can you teach me how to Disable Button Until All Textbox Is Not Empty."
in my design form Named login.
I have 2 textbox and 2 buttons
I want to disable the buttons until all textbox is not empty
i try everything i know but its not working :(
thnks for helping me
so heres my code in c# windows application form

using System.Data.SqlClient;

namespace Online_Examination_System_Finals
{
    public partial class Login : Form
    {
        public Login()
        {
            InitializeComponent();
        }
        SqlConnection con = new SqlConnection(@"Data Source=ADMIN-\MSSQLSERVERR;Initial Catalog=Admin;Integrated Security=True");
        private void button1_Click(object sender, EventArgs e)
        {

            SqlDataAdapter sda = new SqlDataAdapter("Select USN From Admin where USN ='" + textBox1.Text + "'and Password ='" + textBox2.Text + "'", con);
            DataTable dt = new DataTable();
            sda.Fill(dt);

            if (dt.Rows.Count == 1)
            {
                this.Hide();
                Admin_Panel aa = new Admin_Panel(dt.Rows[0][0].ToString());
                aa.Show();
            }


            else 
            {
                MessageBox.Show("Please check your username and password");
                textBox1.SelectAll();
                textBox2.Text = "";
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            SqlDataAdapter sda = new SqlDataAdapter("Select USN From Register where USN ='" + textBox1.Text + "'and Password ='" + textBox2.Text + "'", con);
            DataTable dt = new DataTable();
            sda.Fill(dt);
            if (dt.Rows.Count == 1)
            {
                this.Hide();
                Student aa = new Student(dt.Rows[0][0].ToString());
                aa.Show();
            }
            else
            {
                MessageBox.Show("Please check your username and password");
                textBox1.SelectAll();
                textBox2.Text = "";
            }

        }

        private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar))
                e.Handled = true;
            base.OnKeyPress(e);


        }

Recommended Answers

All 2 Replies

I'm not 100% clear on what you want. If you only care that both textboxes are non blank, can you initially set your button to disabled and then use the following code? When both textboxes have a non blank value, the button is enabled.

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            button1.Enabled = textBox1.Text.Length > 1 && textBox2.Text.Length > 1;
        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {
            button1.Enabled = textBox1.Text.Length > 1 && textBox2.Text.Length > 1;
        }
    }

Using the code you provided you could do something like

private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
    button2.Enabled = (!String.IsNullOrWhiteSpace(textBox1.Text));
}

That pretty much is saying, if there is text in the TextBox, that isn't just whitespace (like spaces), then enable the Button, but if it is blank or whitespace, disable it.

(Of course you can put your other code in there as well)

Also, you may not want to care calling the base.OnKeyPress(e). This is usually used if you override a virtual event for a component, which should be done really in a seperate class anyway. May I also ask what the purpose is of the 'if' statement in that event?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.