将连接的字符串添加为列表框项

时间:2013-08-17 01:50:57

标签: c# winforms

我需要一些帮助。我正在制作一个窗体,我有点困惑。从下面的代码中你可以看到我有3个文本框等于时间。

还有一个复选框。我需要它,以便如果选中该复选框,它将启用第4个文本框,并允许我将下面的if语句中的内容添加到单个列表框条目中。在其当前状态下,按钮单击事件将2个条目添加到列表框中。 - 基本上,我需要所有这些出现在列表框的一行上。

在选中复选框后,我已经有一个启用第4个文本框的if语句。

private void button1_Click(object sender, EventArgs e)
{
    listBox1.Items.Add(textBox1.Text + "hrs, " + textBox2.Text + "min, " + textBox3.Text + "sec.");
    textBox1.Clear();
    textBox2.Clear();
    textBox3.Clear();
    if (checkBox1.Checked)
    {
        listBox1.Items.Add("Novelty: " + textBox4.Text);
    }

}

1 个答案:

答案 0 :(得分:4)

在将字符串添加到ListBox之前,只需构建它。

string text = textBox1.Text + "hrs, " + 
              textBox2.Text + "min, " + 
              textBox3.Text + "sec.";

if (checkBox1.Checked) text += " Novelty: " + textBox4.Text;
listBox1.Items.Add(text);