C# checking white space in a textbox

后端 未结 9 860
孤独总比滥情好
孤独总比滥情好 2021-01-03 06:45

How can I check in C# that is there a white space only in a textbox and perform some operation after that?

9条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-03 07:08

    Check the Text property of the textbox using string.IsNullOrWhiteSpace.

    if (string.IsNullOrWhiteSpace(myTextBox.Text) && myTextBox.Text.Length > 0)
    {
      // do stuff
    }
    

    Since IsNullOrWiteSpace will return true if the textbox is empty (or the property is null), adding the Length check ensures that there is something within the text box. The combination of the tests ensures a true if there is only whitespace in the textbox.

提交回复
热议问题