简体   繁体   中英

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM