How to get the TextRange of text written by user in RichTextBox

I have a RichTextBox that looks like a "console". In 'cmd' I am showing data from SSH connection and I also allow writing something to '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 the last lines, I am using the TextPointer "LastChar" to get the position of the last added content, because I want to get what the user will write to the RichTextBox.

In the "Enter" event, I use a ReWriteStream to get the last text and send it over 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 myText variable is empty.

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

      

How should I get any text written in my RichTextBox after the LastChar position?

Thanks for any help, Best wishes, Wiktor

edit: I put a line in the richtextbox to parse the carriage 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 the RichTextBox cmd: "You have mail. - bash -3,2 $ | 1 | ls -l | 2 | "

+3


source to share


1 answer


Another method is to grab the last insertion position and 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;    
    ...

}

      

+1


source







All Articles