Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
405 views
in Technique[技术] by (71.8m points)

winforms - 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);
    }
question from:https://stackoverflow.com/questions/66054922/how-to-split-textbox-text-to-listbox-c-sharp

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...