简体   繁体   English

如何将文本框文本拆分为列表框 C#

[英]How to Split Textbox Text to Listbox C#

I have a textbox, a listbox and a button on a winforms.我在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我已设法将文本框中的文本添加到列表框,但无法弄清楚如何用逗号分割文本。我知道使用了 Split 方法但不确定如何实现它

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:根据您的规范,似乎也要求逗号在列表框中,以及您将在文本框中输入"Monday, Tuesday, Wednesday"的声明:

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

Split() will split on the spaces.. AddRange takes the array Split returns Split() 将在空格上拆分.. AddRange 采用数组拆分返回

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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