0

I am very very new to C# and I am in the process of doing my project for my studies using Winforms. The below code is not doing its work. When my richComResults (richTextBox) is empty, I want a messageBox to appear and says "There is nothing to be cleared!", but it doesn't say it, it shows the Yes/No dialog.

Please could you be kind enough to point out my mistakes? Your input would be very much appreciated. Thank you.

private void btnComClearAll_Click(object sender, EventArgs e)
    {
        if (richComResults == null)
        {
            MessageBox.Show("There is nothing to be cleared!");
        }
        if (richComResults != null)
        {
            DialogResult dialogResult = MessageBox.Show("Are you sure you want to clear the results?", "Warning", MessageBoxButtons.YesNo);
            if (dialogResult == DialogResult.Yes)
            {
                richComResults.Clear();
            }
            else if (dialogResult == DialogResult.No)
            {
            }
        }
    }
4

2 に答える 2

4

richComResults is your RichTextBox control, so it's probably not null... What you need to check is its Text property. It will probably not be null either, but it might be empty (keep in mind that an empty string is not the same as null). You can use string.IsNullOrEmpty to test both cases anyway:

    if (string.IsNullOrEmpty(richComResults.Text))
    {
        MessageBox.Show("There is nothing to be cleared!");
    }
    else
    {
        ...
    }
于 2012-08-08T01:12:20.693 に答える
0

Another way to check whether richtextbox is empty or not

if (richComResults.Text== "")
            {
                MessageBox.Show("rich text box is empty");

            }
            else
            {
                MessageBox.Show("rich text box is not empty");
            }
于 2012-08-08T01:27:35.293 に答える