0

这里的错误在哪里:

    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

4

2 回答 2

3

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

在第一次单击时,textbox并且s指向null,之后该变量s被更改并且它指向new value,但textbox仍然指向null;

于 2012-10-27T11:05:19.667 回答
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);
}
于 2012-10-27T11:04:17.957 回答