5

次のコードを使用して、null テキスト ボックスをチェックしています。null の場合は、クリップボードへのコピーをスキップして、残りのコードに進みます。

「値を NULL にすることはできません」という例外が発生する理由がわかりません。null を見て、クリップボードにコピーせずに先に進むべきではありませんか?

private void button_Click(object sender, EventArgs e)
{
    if (textBox_Results.Text != null) Clipboard.SetText(textBox_Results.Text);            

    //rest of the code goes here;
}
4

3 に答える 3

7

おそらく次のようにチェックを行う必要があります。

if (textBox_Results != null && !string.IsNullOrWhiteSpace(textBox_Results.Text))

Null参照例外が発生しないように、追加のチェックを行いtextBox_Resultsます。null

于 2013-01-16T01:57:11.087 に答える
6

.NET 4 String.IsNullOrWhitespace()を使用して .Text の Null 値をチェックする場合は、 String.IsNullOrEmpty()を使用する必要があります。

private void button_Click(object sender, EventArgs e)
    {
        if (!String.IsNullOrEmpty(textBox_Results.Text) Clipboard.SetText(textBox_Results.Text);            

        //rest of the code goes here;
    }
于 2013-01-16T01:55:22.747 に答える
1

Text が空の文字列かどうかを確認できると思います:

private void button_Click(object sender, EventArgs e)
{
    if (textBox_Results.Text != "") Clipboard.SetText(textBox_Results.Text);            

    //rest of the code goes here;
}

string.IsNullOrEmpty() メソッドを使用して確認することもできます。

于 2013-01-16T01:57:54.907 に答える