For
.Net Framework 3.0 and lower, you can use
String.IsNullOrEmpty(string)
function which returns
true
if the string argument is
Null
or is empty. But this function does not check if the string parsed contains only spaces(WhiteSpace), so you you also have to call the
Trim()
function of string.You can create a validate function to check your controls before remitting it...
public bool ValidateControls()
{
if (String.IsNullOrEmpty(myTextBox.Text))
{
MessageBox.Show("The content of the Textbox is not valid.");
return false;
}
return true;
}
However, if you're using
.Net Framework 3.5 and higher, you should use
String.IsNullOrWhiteSpace(string)
function which returns
true
if the string argument is empty,
Null
or contains only spaces. So, you would not have to Trim the string again, and the above code could be rewritten as
public bool ValidateControls()
{
if (String.IsNullOrWhiteSpace(myTextBox.Text))
{
MessageBox.Show("The content of the Textbox is not valid.");
return false;
}
return true;
}