textBox项目复制到不同的textBoxes [英] textBox items copy to different textBoxes

查看:62
本文介绍了textBox项目复制到不同的textBoxes的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的一个项目中执行任务



i希望将文本框中的项目转移到不同的文本框中。我可以通过此实现>


I want to perform a task in one of my project

i want to transfer items in a textBox into different textBoxes.that i can achieve by this

string[] values = textBox1.Text.Split('#');
textBox2.Text = values[0];
textBox3.Text = values[1];
textBox4.Text = values[2];
textBox5.Text = values[3];
textBox6.Text = values[4];







我想要的是在5个不同的文本框中复制5个项目。必须从原始文本框中删除这5个项目。







**假设我有50个文本框中名为textBox1的项目由#分割。



例如:a#b#c#d#e#f#g#h#i#j#... .etc



现在点击按钮点击按钮1



发生以下事件



a in textBox2 / b in textBox3 / c in textBox4 / d in textBox5 / e in textBox6



- 当我点击button1再次



i希望textBox1中剩下的5个项目必须是这样的



f in textBox2 / g textBox3 / h in textBox4 / i in textBox5 / j in textBox6



现在再次点击按钮


$ b文本框3中的textBox2 / m中的$ bk /文本框4中的textBox4 /中的文本框4中的$ b / b
每次点击它必须选择5个项目的下一个插槽



提前致谢




what i want is when 5 items are copied in 5 different textboxes. those 5 items must be deleted from the origin textbox.

OR

**suppose i got 50 items in a textbox called textBox1 splitted by #.

eg:a#b#c#d#e#f#g#h#i#j#....etc

Now with a button click named button1

following event occurs

a in textBox2/b in textBox3/c in textBox4/d in textBox5/e in textBox6

-Now when i click the button1 again

i want the 5 items which are left in textBox1 must be like this

f in textBox2/g in textBox3/h in textBox4/i in textBox5/j in textBox6

now with again button click

k in textBox2/l in textBox3/m in textBox4/n in textBox5/o in textBox6

and with every click it must pick next slot of 5 items

Thanks in advance

推荐答案

迭代使用 String.IndexOf [ ^ ]获取(最大)<$ c的位置$ c> 5 输入字符串中的分隔符,将此位置存储在数组中,并使用后者更新输出文本框。然后,使用最后一个位置,使用输入字符串的已处理部分。
Iteratively use String.IndexOf[^] to get the positions of (maximum) 5 delimiters in the input string, store such position in an array and use the latter for updating the output textboxes. Then, using the last position, consume the processed part of the input string.


这是一个快速草图,说明您可能采用的一种方法:
Here's a quick sketch of one way you might approach this:
private const int chunkSize = 5;

private string source = "a#b#c#d#e#f#g#h#i#j#k#l#m#n#o#p#q#r#s#t#u#v#w#x#y#z#";

private string sourceCopy;

private List<string> sourceList;

private List<TextBox> textBoxes;

private void Form1_Load(object sender, EventArgs e)
{
    sourceCopy = source;

    textBox1.Text = sourceCopy;

    textBoxes = new List<TextBox>{textBox2, textBox3, textBox4, textBox5, textBox6};

    sourceList = source.Split(new char[] {'#'}, StringSplitOptions.RemoveEmptyEntries).ToList();
}

private void button1_Click(object sender, EventArgs e)
{
    if (sourceList.Count == 0)
    {
        MessageBox.Show("running on empty: refuel ?");

        foreach(TextBox tb in textBoxes) tb.Clear();

        return;
    }

    int nToTake = (sourceList.Count < chunkSize) ? sourceList.Count : chunkSize;

    for (int i = 0; i < chunkSize; i++)
    {
        textBoxes[i].Text = (i < nToTake) ? sourceList[i] : "";
    }

    sourceList.RemoveRange(0, nToTake);
    sourceCopy = sourceCopy.Remove(0, nToTake*2);
    textBox1.Text = sourceCopy;
}

转换使用'拆分为列表<字符串>生成的字符串[]允许在List上使用方便的'RemoveRange(index,count)运算符。将用于单个字符显示的五个TextBox放在List中可以轻松迭代所有这些,避免重复编码。



注意源字符串的副本是根据您可能希望再次使用原件的想法使用。

Converting the string[] produced by using 'Split to a List<string> allows using the handy 'RemoveRange(index, count) operator on the List. Putting the five TextBoxes used for single character display in a List makes iterating over all of them easy, and avoids repetitive coding.

Note that a copy of the source string was used based on the idea you might wish to use the original again.


这篇关于textBox项目复制到不同的textBoxes的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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