Click here to Skip to main content
15,886,689 members
Please Sign up or sign in to vote.
4.00/5 (1 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

C#
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.

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
Posted
Updated 23-Apr-14 21:12pm
v3

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.
 
Share this answer
 
Comments
Member 10579673 24-Apr-14 3:28am    
can you elaborate sir? by giving a code overview.it will be helpful.
Here's a quick sketch of one way you might approach this:
C#
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;
}
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.
 
Share this answer
 
v2

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900