我对C#是非常非常陌生的,我正在使用Winforms为我的研究做项目。下面的代码不能正常工作。当我的richComResults (richTextBox)为空时,我希望出现一个messageBox,并显示“没有什么需要清除!”,但它没有显示出来,它显示的是Yes/No对话框。
请您指出我的错误好吗?您的意见将非常感谢。谢谢。
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)
{
}
}
}发布于 2012-08-08 09:12:21
richComResults是您的RichTextBox控件,因此它可能不是null...您需要检查的是它的Text属性。它也可能不是null,但也可能是空的(请记住,空字符串与null不同)。无论如何,您都可以使用string.IsNullOrEmpty测试这两种情况:
if (string.IsNullOrEmpty(richComResults.Text))
{
MessageBox.Show("There is nothing to be cleared!");
}
else
{
...
}发布于 2012-08-08 09:27:35
另一种检查richtextbox是否为空的方法
if (richComResults.Text== "")
{
MessageBox.Show("rich text box is empty");
}
else
{
MessageBox.Show("rich text box is not empty");
}https://stackoverflow.com/questions/11856269
复制相似问题