在多行文本框中选择文本,在C#中单击按钮时将其移动到另一个多行文本框

时间:2012-06-30 17:56:02

标签: c#

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

布伦特

3 个答案:

答案 0 :(得分:1)

好吧,假设您正在定义一条"线"作为由带有换行符的其他类似字符串分隔的完整字符串,而不仅仅是在文本字段中单个水平平面中可见的字符串,其中自动换行属性设置为true .....

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。此外,这是从袖口写的,所以你可能需要调整一两个字符来编译。无保证,LOL。

答案 1 :(得分: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;
    }
}

答案 2 :(得分:1)

尝试这样的事情:

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

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