3

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| "

Wiktor
  • 754
  • 1
  • 7
  • 24

1 Answers1

1

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;    
    ...

}
Dylan Corriveau
  • 2,561
  • 4
  • 29
  • 36
  • Thank You for Your answer, unfortunately it doesn't helped. – Wiktor Jun 08 '15 at 13:30
  • If I use something like this : ` string myText = new TextRange(rtb.Document.ContentStart, rtb.Document.ContentEnd).Text; ` I can get whole text also that text inserted by user. – Wiktor Jun 08 '15 at 13:32
  • Thank You again for taking time with my problem but again this doesn't help. – Wiktor Jun 08 '15 at 14:22
  • I of course tried: LastChar = rtb.CaretPosition.DocumentEnd.GetInsertionPosition(LogicalDirection.Backward); and also LastChar = rtb.Document.ContentEnd.GetInsertionPosition(LogicalDirection.Backward); – Wiktor Jun 08 '15 at 14:24
  • Please take a look on my first post, I insert string "|1|" and "|2|" . Text inserted by user "ls -l" is exactly where it should by. I have no idea why I can't get it in myText variable. – Wiktor Jun 08 '15 at 14:34
  • see the changed answer – Dylan Corriveau Jun 08 '15 at 14:54
  • Thank You, it actually help. I need to understand why ;) I will try to make some changes and look what will happen. Best Regards ! – Wiktor Jun 08 '15 at 15:44