The_Bear
The_Bear

Reputation: 173

How to Split Textbox Text to Listbox C#

I have a textbox, a listbox and a button on a winforms. I want the user to input some text into the textbox and when I click the button it outputs to the listbox, but I want the text seperated by a comma. For example if I enter Monday, Tuesday, Wednesday into the textbox, I want it to display in the listbox as:

Monday,

Tuesday,

Wednesday

Could anyone help?

I have managed to add the text from the textbox to the listbox but can't work out how to split the text by comma.I know that the Split method is used but unsure how to implement it

Thanks

 private void btnSplit_Click(object sender, EventArgs e)
    {
        listboxListItems.Items.Add(txtboxUserInput.Text);
    }

Upvotes: 0

Views: 343

Answers (1)

Caius Jard
Caius Jard

Reputation: 74605

how to split the text by comma.

You don't actually want to -

From your spec that seems to demand the comma be in the list box too, and your statement that you will enter "Monday, Tuesday, Wednesday" in the textbox:

listboxListItems.Items.AddRange(
  txtboxUserInput.Text.Split()
);

Split() will split on the spaces.. AddRange takes the array Split returns

Upvotes: 1

Related Questions