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
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
Use something such as the following:
if (String.IsNullOrEmpty(MaterialTextBox.Text))
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
}
if (MaterialTextBox.Text.length==0)
{
message
}
Adding on to what @tjg184 said, you could do something like...
if (String.IsNullOrEmpty(MaterialTextBox.Text.Trim()))
...
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