JakeG92
JakeG92

Reputation: 15

Adding concatenated strings as listbox items

I need a little help. I am making a windows form and I'm a little confused. From the code below you can see that I have 3 textboxes at equate to time.

There is also a checkbox. I need it so that if the checkbox is checked, it enables a 4th textbox and and allows me to add what is in the if statement below to a single listbox entry. In it's current state, the button click event adds 2 entries to the listbox. - Basically, I need all of this to appear on ONE LINE of the listbox.

I already have an if statement enabling the 4th textbox after checking the checkbox.

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);
    }

}

Upvotes: 0

Views: 1991

Answers (1)

Andrew Sun
Andrew Sun

Reputation: 4231

Just build your string before you add it to the ListBox.

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

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

Upvotes: 4

Related Questions