Click here to Skip to main content
14,303,528 members
Rate this:
Please Sign up or sign in to vote.
See more:
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];

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


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


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
Updated 23-Apr-14 21:12pm
Rate this:
Please Sign up or sign in to vote.

Solution 1

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.
Member 10579673 24-Apr-14 3:28am
can you elaborate sir? by giving a code will be helpful.
Rate this:
Please Sign up or sign in to vote.

Solution 2

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


    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;
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.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100