之后如何在textbox.text上声明字符串相等

时间:2012-10-27 10:57:50

标签: c#

这里的错误在哪里:

    static int count = 0;
    string s;
    private void SetClock_Click(object sender, EventArgs e)
    {
        txtSend.Text = s;
        count++;

        label5.Text = count.ToString("X2");

        DateTime time = DateTime.Now;

        s = "4D-" + "1A-" + "2B-" + "3C-" +
        (label5.Text.ToString());

    }

在第一次点击我得到一个点击值01,但没有得到txtsend.text ...在第二次点击我得到值02但在txtsend.text我得到4D-1A-2B-3C-01

2 个答案:

答案 0 :(得分:3)

每次更改字符串变量时,都会重新创建。

第一次点击后,textboxs指向null,之后变量s已更改且指向new value,但textbox仍然指向null;

答案 1 :(得分:1)

将您的代码更改为

static int count = 0;
string s;
private void SetClock_Click(object sender, EventArgs e)
{
    count++;
    label5.Text = count.ToString("X2");
    DateTime time = DateTime.Now;
    s = "4D-" + "1A-" + "2B-" + "3C-" +(label5.Text);
    txtSend.Text = s; 

    //Set your s before assign to txtSend.Text

    //or you can do without s like
    //txtSend.Text = "4D-" + "1A-" + "2B-" + "3C-" +(label5.Text);
}