在构造函数中我做了:
public Form1()
{
InitializeComponent();
if ((txtHost.Text == "") || txtUploadFile.Text == "")
{
btnUpload.Enabled = false;
}
}
但我想要做的是,如果用户在两个文本框中键入文本或检查两个文本框中何时都有文本,则启用btnUpload true。
也许我需要一个活动或什么?但是我想检查一下,只有当两个复选框txtHost和txtUploadFile里面只有文本然后才启用true按钮btnUpload。
答案 0 :(得分:2)
试试这个
public Form1()
{
InitializeComponent();
txtHost.TextChanged += textBox_TextChanged;
txtUploadFile.TextChanged += textBox_TextChanged;
textBox_TextChanged(null, null);
}
private void textBox_TextChanged(Object sender, EventArgs e)
{
btnUpload.Enabled = txtHost.TextLength > 0 && txtUploadFile.TextLength > 0;
}
答案 1 :(得分:1)
连接下面的事件。这是代码posted by Phill W. on MSDN的摘录。
textBox1.TextChanged += anyTextBox_TextChanged;
textBox6.TextChanged += anyTextBox_TextChanged;
private void anyTextBox_TextChanged( object sender, TextChangedEventArgs e )
{
// ... ask the button to check its own state.
button1_CheckState();
}
private void button1_CheckState()
{
// Assume all is well to start with
bool state = true ;
foreach ( TextBox textBox in textBoxes_ )
if ( "".Equals( textBox.Text ) )
{
// If it's not, disable the button.
state = false ;
break ;
}
button1.Enabled = state ;
}
答案 2 :(得分:0)
编写一个事件处理程序并将其分配给两个文本框上的TextChanged事件。事件代码是btnUpload.Enabled = txtHost.Text.Length> 0&& txtUploadFile.Text.Lenght> 0
答案 3 :(得分:0)
使用!=运算符。 防爆。如果(text!=“”) 然后将Btnupload设置为true。