我想在我的一个项目中执行任务
我想将textBox中的项目转移到不同的textBoxes中。我可以通过此
实现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个项目。
OR
**假设我在一个名为textBox1的文本框中有10个项目被#。
分割例如:A·B·C·d·E·F#G#ħ#I#Ĵ
现在点击按钮点击button1
发生以下事件
a textBox2 / b in textBox3 / c in textBox4 / d in textBox5 / e in textBox6
- 当我再次点击按钮1时
我希望textBox1中剩下的5个项目必须像这样
in textBox2 / g in textBox3 / h in textBox4 / i in textBox5 / j in textBox6
答案 0 :(得分:1)
我会开始编写一种方法,尝试拆分字符串,使用一个值来限制要返回的拆分字符串的数量。同样的方法应该从输入字符串中删除已拆分的部分,并返回带有拆分字符串的数组和原始字符串的其余部分。
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;
}
现在只需要处理此操作的结果,将原始文本框设置为余数,接收文本框设置为分割数组
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;
答案 1 :(得分:0)
从Origin TextBox
中删除使用:(仅举例)
TextBox1.Text = TextBox1.Text.Substring(10);
如果您确定5件物品,那就是这样:
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);
}
}
答案 2 :(得分:0)
尝试此操作后,用户点击按钮后会自动删除以前的值,并将其替换为新的
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;
}
希望这可以帮助你:)