简体   繁体   中英

How can i check if both textBoxes are with text inside and then to enable another button?

In the constructor i did:

public Form1()
        {
            InitializeComponent();

            if ((txtHost.Text == "") || txtUploadFile.Text == "")
            {
                btnUpload.Enabled = false;
            }
        }

But what i want to do is that if the user type text in both textboxes or to check when both textboxes have text inside then enable btnUpload true.

Maybe i need an event or something ? But i want to check that only when the two checkboxes txtHost and txtUploadFile have text inside only then enabled true button btnUpload.

Try this

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;
}

wire up the below event. Here's an extract of code 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 ; 
}

Write an event handler and assign it to TextChanged event on both text boxes. The event code would be btnUpload.Enabled = txtHost.Text.Length > 0 && txtUploadFile.Text.Lenght > 0.

Use != operator. Ex. If (text != "") Then set Btnupload to true.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM