0

我有一个我还没有遇到的问题,希望你们中的一些人可以帮助我。我正在尝试在多行文本框中选择一行,第一行、第二行或最后一行,然后在 C# 中单击按钮将其移动到另一个多行文本框。我不确定如何一次只选择一行,然后将其添加到另一个多行文本框中。如果有人有一些见解,那就太好了!谢谢!

布伦特

4

3 回答 3

1

尝试这样的事情:

private void button1_Click(object sender, EventArgs e)
{
    if (textBox1.Lines.Length > 0)
    {
        textBox2.Text += textBox1.Lines[textBox1.GetLineFromCharIndex(textBox1.SelectionStart)];
    }
}

它正在做的是使用GetLineFromCharIndex和 SelectionStart 插入符号位置作为字符索引来将 Line 从TextBox.Lines数组中拉出

于 2012-06-30T18:52:52.590 回答
1

好吧,假设您将“行”定义为由其他类似字符串用换行符分隔的完整字符串,而不仅仅是在具有自动换行属性的文本字段中的单个水平面上可见的字符串为真.....

public void Button1_Click(object sender, ClickEventArgs e)
{
     //get the values of both boxes
     string value1 = TextBox1.Text.Trim();
     string value2 = TextBox2.Text.Trim();

     //split the value from the source box on its new line characters
     string[] parts = value1.split(Environment.NewLine);
     string last_line = parts[parts.length -1];

     //add the last row from the source box to the destination box
     value2 += (Environment.NewLine + last_line);

     //set the last_line in the source to an empty string
     parts[parts.Length -1] = String.Empty;

     //put the new values back in their text boxes
     TextBox1.Text = String.Join(Environment.NewLine, parts).Trim();
     TextBox2.Text = value2;
}

如果您正在处理可见的线条和文字包装,那是一个完整的“其他球类游戏”,答案取决于您是在谈论 ASP 还是 Win App。此外,这是临时写的,因此您可能需要调整一两个字符才能编译。没有保证,大声笑。

于 2012-06-30T18:18:32.307 回答
1

像这样的东西会起作用:

public void Button1_Click(object sender, ClickEventArgs e)
{    
   string text = TextBox1.Text;

    // spliting text on the basis on newline.
    string[] myArray = text.Split(new char[] { '\n' });

    foreach (string s in myArray)
    {
       //Line by line copy
       TextBox2.Text += s;
    }
}
于 2012-06-30T18:18:45.240 回答