Reputation: 21
Can you teach me how to disable a button until all textboxes are not empty?
In my design form login
, I have 2 textboxes and 2 buttons.
I want to disable the buttons until both textboxes are not empty.
Here is my code in a c#
Windows application form:
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 = "";
}
button1.Enabled = !string.IsNullOrWhiteSpace(textBox1.Text);
}
private void button2_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();
Student aa = new Student(dt.Rows[0][0].ToString());
aa.Show();
}
else
{
MessageBox.Show("Please check your username and password");
textBox1.SelectAll();
textBox2.Text = "";
}
button2.Enabled = !string.IsNullOrWhiteSpace(textBox1.Text);
}
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar))
e.Handled = true;
base.OnKeyPress(e);
}
I also tried this
button1.Enabled = !string.IsNullOrWhiteSpace(textBox1.Text);
... but when I had already put Username and Password in the textboxes, the 2 buttons were not enabled.
Upvotes: 0
Views: 8298
Reputation: 11
private void textbox1_TextChanged(object sender, EventArgs e)
{
EnableButton();
}
private void textbox2_TextChanged(object sender, EventArgs e)
{
EnableButton();
}
private void EnableButton()
{
if(textbox1.Text == "" || textbox2.Text == "")
{
button1.Enabled = false;
button2.Enabled = false;
}
else
{
button1.Enabled = true;
button2.Enabled = true;
}
}
Upvotes: 0
Reputation: 359
First set your buttons.Enabled property to false. Then add a TextChanged-Handler for both TextBoxes, where you check if both TextBoxes contain something.
The code of the handlers could look like this:
private void textBox1_TextChanged(object sender, EventArgs e)
{
setButtonVisibility();
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
setButtonVisibility();
}
private void setButtonVisibility()
{
if ((textBox1.Text != String.Empty) && (textBox2.Text != String.Empty))
{
button1.Enabled = true;
button2.Enabled = true;
}
else
{
button1.Enabled = false;
button2.Enabled = false;
}
}
Upvotes: 3
Reputation: 10297
You'll want to monitor changes to the textboxes, in their Changed event, like:
private void textbox1_Change(object sender, EventArgs e)
{
ConditionallyEnableSubmitButton();
}
private void textbox2_Change(object sender, EventArgs e)
{
ConditionallyEnableSubmitButton();
}
private void ConditionallyEnableSubmitButton()
{
button1.Enabled = (!string.IsNullOrWhiteSpace(textBox1.Text) ||
!string.IsNullOrWhiteSpace(textBox2.Text));
}
Hint at no extra charge: give any control that you are going to reference programmatically a recognizable name, such as "btnSubmit", "txtbxUsername", "txtbxPwd" etc.
You could do a lot worse than to read Steve McConnell's "Code Complete" for this and many other nudges toward good practice.
Upvotes: 1