[英]How to disable the submit button until a textbox is not empty in c#?
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. 在设计表单
login
,我有2个文本框和2个按钮。 I want to disable the buttons until both textboxes are not empty. 我要禁用按钮,直到两个文本框都不为空。
Here is my code in a c#
Windows application form: 这是我在
c#
Windows应用程序形式中的代码:
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. ...但是当我已经在文本框中输入了用户名和密码时,这两个按钮没有启用。
First set your buttons.Enabled property to false. 首先将您的button.Enabled属性设置为false。 Then add a TextChanged-Handler for both TextBoxes, where you check if both TextBoxes contain something.
然后为两个TextBox添加一个TextChanged-Handler,在其中检查两个TextBox是否都包含某些内容。
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;
}
}
You'll want to monitor changes to the textboxes, in their Changed event, like: 您需要监视文本框的Changed事件中的更改,例如:
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. 无需额外提示:给任何控件以编程方式引用可识别的名称,例如“ btnSubmit”,“ txtbxUsername”,“ txtbxPwd”等。
You could do a lot worse than to read Steve McConnell's "Code Complete" for this and many other nudges toward good practice. 您可能比读史蒂夫·麦康奈尔(Steve McConnell)的“代码完成”(Code Complete)还要糟糕得多,因为这和其他许多推动良好实践的方法。
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;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.