How to get TextRange of text written by user in RichTextBox

926 views Asked by At

I have RichTextBox which is like "console". In 'cmd' I displaying data from SSH connection and I also allowing to write something into '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 ;
    }

In last lines I using TextPointer "LastChar" to get position of last added content, because I want to get what user will write in RichTextBox.

On "Enter" event I using ReWriteStream to get last text and send it through 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();

    }

But every time, my variable myText is empty.

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

How i should get any text, written in my RichTextBox after LastChar position?

Thank You for any help, Best Regards, Wiktor

edit: I have put string into richtextbox to analyze caret position :

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;

    }

and here :

 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();

    }

And I get exactly what it should be in cmd RichTextBox : " You have mail. -bash-3.2$ |1| ls -l |2| "

1

There are 1 answers

7
Dylan Corriveau On BEST ANSWER

Another method is to grab the last insertion position, then use that for the text range.

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

}