Reputation:
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.
OR
**suppose i got 10 items in a textbox called textBox1 splitted by #.
eg:a#b#c#d#e#f#g#h#i#j
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
Upvotes: 1
Views: 432
Reputation: 216253
I would start writing a method that tries to split your string taking a value to limit the amount of splitted strings to return. The same method should delete, from the input string the parts splitted and return both the array with the splitted strings and the remainder of the original string.
string[] SplitWithLimit(ref string test, int limit)
{
int count=0;
int pos=-1;
// Try to find the position of the # at the limit count
while((pos = test.IndexOf('#', pos+1)) != -1)
{
count++;
if(count == limit)
break;
}
string toSplit;
if(pos != -1)
{
// Take the left part of the string to be splitted
// till the position of the char at limit boundary
toSplit = test.Substring(0, pos);
test = test.Substring(pos+1);
}
else
{
// If there are less # than required, take all the string
toSplit = test;
test = "";
}
// Now split only the required part
string[] parts = toSplit.Split(new char[] {'#'}, StringSplitOptions.RemoveEmptyEntries);
return parts;
}
Now it is just a matter of handling the results of this operation setting the original textbox with the remainder and the receiving textbox with the splitted array
TextBox2.Text =string.Empty;
TextBox3.Text =string.Empty;
TextBox4.Text =string.Empty;
TextBox5.Text =string.Empty;
TextBox6.Text =string.Empty;
string test = TextBox1.Text; //"a#b#c#d#e#f#g#h#j"
string[] parts = SplitWithLimit(ref test, 5);
if(parts.Length > 0)
TextBox2.Text = parts[0];
if(parts.Length > 1)
TextBox3.Text = parts[1];
if(parts.Length > 2)
TextBox4.Text = parts[2];
if(parts.Length > 3)
TextBox5.Text = parts[3];
if(parts.Length > 4)
TextBox6.Text = parts[4];
TextBox1.Text = test;
Upvotes: 1
Reputation: 5430
For deleting from Origin TextBox
use: (Just example)
TextBox1.Text = TextBox1.Text.Substring(10);
Something like that if you are sure 5 items:
private void button_Click(object sender, EventArgs e)
{
string[] values = textBox1.Text.Split('#');
if (values.Length >= 5)
{
textBox2.Text = values[0];
textBox3.Text = values[1];
textBox4.Text = values[2];
textBox5.Text = values[3];
textBox6.Text = values[4];
//change the destination
if (values.Length == 5)
textBox1.Text = textBox1.Text.Substring(9);
else if (values.Length > 5)
textBox1.Text = textBox1.Text.Substring(10);
}
}
Upvotes: 0
Reputation: 3057
Try this, previous values will be automatically deleted once user clicks the button and it gets replaced by new one
int noOfTimesClicked=0;
private void FillTextBoxes()
{
string[] values = textBox1.Text.Split('#');
textBox2.Text = values[noOfTimesClicked] != null ? values[noOfTimesClicked] : "";
textBox3.Text = values[(noOfTimesClicked) + 1] != null ? values[(noOfTimesClicked) + 1] : "";
textBox4.Text = values[(noOfTimesClicked) + 2] != null ? values[(noOfTimesClicked) + 2] : "";
textBox5.Text = values[(noOfTimesClicked) + 3] != null ? values[(noOfTimesClicked) + 3] : "";
textBox6.Text = values[(noOfTimesClicked) + 4] != null ? values[(noOfTimesClicked) + 4]:"";
noOfTimesClicked = noOfTimesClicked + 5;
}
Hope this helps you :)
Upvotes: 0