如何将文本从文本框传输到列表框c# [英] How To Transfer text from textbox to listbox c#

查看:58
本文介绍了如何将文本从文本框传输到列表框c#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个文本框,其中的文字由#

分隔

我想通过点击按钮转移列表框中的每个拆分文本



怎么做?

I have a textbox in which text is separated by #

I want to transfer every splitted text in listbox with a button click

how to do that?

推荐答案

在按钮点击方法中添加:

Add this in your button click method:
string[] parts = yourTextBox.Text.Split('#');
yourListBox.Items.AddRange(parts);



如果您不希望列表框中有空元素,请尝试以下操作:


If you don't want that there can be empty elements in your listbox, try this:

string[] parts = yourTextBox.Text.Split(new char[] { '#' }, StringSplitOptions.RemoveEmptyEntries);
yourListBox.Items.AddRange(parts);



如果你想在每个按钮点击上添加5个项目(并且只需要列表框中的5个项目),试试这个:


If you want to add 5 items on every button click (and only want 5 items in the listbox), try this:

int i = 0;
void yourButton_Click(object sender, EventArgs e)
{
    string[] parts = yourTextBox.Text.Split(new char[] { '#' }, StringSplitOptions.RemoveEmptyEntries);
    string[] partsToAdd = parts.Skip(5 * i).Take(5).ToArray();
    yourListBox.Items.Clear();
    yourListBox.Items.AddRange(partsToAdd);
    i++;
}


这篇关于如何将文本从文本框传输到列表框c#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆