I have 2 forms set up. In the first form I have the following code.
frm_BL addBranch = new frm_BL();
do
{
addBranch.ShowDialog();
if (addBranch.txtAmount.Text == "")
{
break;
}
} while (true);
In the main form. And just this in the second form.
private void btnAccept_Click(object sender, EventArgs e)
{
this.Close();
}
However I found that if I change the code of the main form to:
if (addBranch.txtAmount.Text == null) //changed to null
The second form keeps popping up. But if it stays at
if (addBranch.txtAmount.Text == "")
It closes the form. Can someone explain why that is?
The best way to do this is:
The
txtAmount.Textproperty is astringcontaining the content of the textbox. If the textbox is empty then it’s a zero-length string.Checking for equality with
nullis saying “If the textbox doesn’t have a string …”, which will always be false. The correct condition to check is “If the textbox’s string is empty …”.Using the
IsNullOrEmptymethod checks for both conditions. In this case the string should never be null, but it doesn’t hurt to check.Note that
""is an empty string (equivalent toString.Empty), whereasnullsays the string doesn’t exist.