如何在RichTextBox

时间:2015-06-08 12:20:35

标签: c# wpf richtextbox textrange

我有RichTextBox,就像“console”。在'cmd'中我显示来自SSH连接的数据,我还允许在'cmd'RichTextBox中写入内容。

public void ConnectTTY()
    {
        NewTerminal = new TTY(HostBox.Text, "", LoginBox.Text, PasswordBox.Text);

        Thread.Sleep(1000);

        ReadStreamAsync(NewTerminal.reader, cmd);

        // cmd is my RichTextBox . At first I am filling it with data from server
        // and in next step I am allowing user to write some command 
        cmd.Focus();


        cmd.CaretPosition = cmd.Document.ContentEnd;
        cmd.ScrollToEnd();

        LastChar =    cmd.Document.ContentEnd ;
    }

在最后几行中,我使用TextPointer“LastChar”获取最后添加内容的位置,因为我想知道用户将在RichTextBox中写入什么内容。

On“Enter”事件我使用ReWriteStream获取最后一个文本并通过SSH发送。

 public void ReWriteStream()
    {

        string myText = new TextRange(LastChar, cmd.Document.ContentEnd).Text ;


        MessageBox.Show("Tekst: " + myText.ToString()  );

        if ( myText.Length != 0 )
        {
            WriteStreamAsync(myText, NewTerminal.writer, NewTerminal.stream);
            ReadStreamAsync(NewTerminal.reader, cmd);

            //WriteStream(myText, NewTerminal.writer, NewTerminal.stream);
            //Thread.Sleep(1000);
            //ReadStream(NewTerminal.reader, cmd);


        }
        cmd.Focus();

    }

但每次,我的变量myText都是空的。

string myText = new TextRange(LastChar, cmd.Document.ContentEnd).Text ;

如何获取在LastChar位置后写入RichTextBox的任何文本?

感谢您的帮助, 最好的祝福, Wiktor的

编辑:我已将字符串放入richtextbox来分析插入位置:

public void ConnectTTY()
    {
        NewTerminal = new TTY(HostBox.Text, "", LoginBox.Text, PasswordBox.Text);

        Thread.Sleep(1000);

        ReadStreamAsync(NewTerminal.reader, cmd);



        cmd.Focus();


        cmd.CaretPosition = cmd.Document.ContentEnd;
        cmd.ScrollToEnd();

        cmd.CaretPosition.InsertTextInRun("|1|");

        // LastChar = cmd.CaretPosition.DocumentEnd;

    }

在这里:

 public void ReWriteStream()
    {
        string caret;
        string myText;



        LastChar = cmd.CaretPosition.DocumentEnd.GetInsertionPosition(LogicalDirection.Backward);

        cmd.CaretPosition.InsertTextInRun("|2|");

        myText = new TextRange(LastChar, cmd.CaretPosition.DocumentEnd).Text;
       // myText = new TextRange(LastChar, cmd.Document.ContentEnd).Text;

        MessageBox.Show("Text : " +  myText.ToString()  );

        if ( myText.Length != 0 )
        {
            WriteStreamAsync(myText, NewTerminal.writer, NewTerminal.stream);
            ReadStreamAsync(NewTerminal.reader, cmd);

            //WriteStream(myText, NewTerminal.writer, NewTerminal.stream);
            //Thread.Sleep(1000);
            //ReadStream(NewTerminal.reader, cmd);


        }
        cmd.Focus();

    }

我在cmd RichTextBox中得到了它应该是什么: “ 你有邮件。 -bash-3.2 $ | 1 | ls -l <​​strong> | 2 | “

1 个答案:

答案 0 :(得分:1)

另一种方法是抓取最后一个插入位置,然后将其用于文本范围。

public void ReWriteStream()
{
    var myText = new TextRange(rtb.CaretPosition.GetLineStartPosition(0), rtb.CaretPosition.GetLineStartPosition(1) ?? rtb.CaretPosition.DocumentEnd).Text;    
    ...

}