检查NULL文本框的文本 [英] Checking Textbox Text for NULL
问题描述
很抱歉的要点问题,但我一直在努力了一个多小时摸不着头脑了。我使用下面的代码来检查空文本框,如果为空,跳过复制到剪贴板,并移动到代码的其余部分。
我不明白为什么我收到值不能为空异常。难道不应该看空,继续前进,而不复制到剪贴板?
私人无效button_Click(对象发件人,EventArgs的发送)
{
如果(textBox_Results.Text!= NULL)Clipboard.SetText(textBox_Results.Text);
//代码其余放在这里;
}
您可能应该做你的检查这样的
如果(textBox_Results = NULL&放大器;!&安培;!string.IsNullOrWhiteSpace(textBox_Results.Text))
只是一个额外的检查,所以如果textBox_Results是有史以来空你没有得到一个空引用异常。
Sorry for the nub question, but I have been trying to figure this out for over an hour. I am using the following code to check for a null text-box, and if it is null, skip the copy to clipboard and move on to the rest of the code.
I don't understand why I am getting "Value cannot be NULL" exception. Shouldn't it see the null and move on without copying to the clipboard?
private void button_Click(object sender, EventArgs e)
{
if (textBox_Results.Text != null) Clipboard.SetText(textBox_Results.Text);
//rest of the code goes here;
}
You should probably be doing your check like this.
if (textBox_Results != null && !string.IsNullOrWhiteSpace(textBox_Results.Text))
Just an extra check so if textBox_Results is ever null you don't get a Null Reference exception.
这篇关于检查NULL文本框的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!