Check if TextBox is empty and return MessageBox?

后端 未结 8 1267
谎友^
谎友^ 2020-12-23 22:19

I made this statement to check if TextBox is empty, but the MessageBox always shows up wether the TextBox is empty or not.

    private void NextButton_Click         


        
相关标签:
8条回答
  • 2020-12-23 22:35

    Try doing the following

    if (String.IsNullOrEmpty(MaterialTextBox.Text) || String.IsNullOrWhiteSpace(MaterialTextBox.Text))
        {
            //do job
        }
        else
        {
            MessageBox.Show("Please enter correct path");
        }
    

    Hope it helps

    0 讨论(0)
  • 2020-12-23 22:37

    Use something such as the following:

    if (String.IsNullOrEmpty(MaterialTextBox.Text)) 
    
    0 讨论(0)
  • 2020-12-23 22:37

    Becasue is a TextBox already initialized would be better to control if there is something in there outside the empty string (which is no null or empty string I am afraid). What I did is just check is there is something different than "", if so do the thing:

     if (TextBox.Text != "") //Something different than ""?
            {
                //Do your stuff
            }
     else
            {
                //Do NOT do your stuff
            }
    
    0 讨论(0)
  • 2020-12-23 22:39
    if (MaterialTextBox.Text.length==0)
    {
    message
    
    }
    
    0 讨论(0)
  • 2020-12-23 22:41

    Adding on to what @tjg184 said, you could do something like...

    if (String.IsNullOrEmpty(MaterialTextBox.Text.Trim())) 
    

    ...

    0 讨论(0)
  • 2020-12-23 22:44

    Try this condition instead:

    if (string.IsNullOrWhiteSpace(MaterialTextBox.Text)) {
        // Message box
    }
    

    This will take care of some strings that only contain whitespace characters and you won't have to deal with string equality which can sometimes be tricky

    0 讨论(0)
提交回复
热议问题