我有RichTextBox,这是像"控制台"。在'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中写入的内容。
在"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 ;
我应该如何得到任何文本,写在我的RichTextBox后LastChar位置?
谢谢你的帮助,最好的问候,有意
编辑:我已经把字符串放入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 |2|"
另一种方法是获取最后一个插入位置,然后将其用于文本范围。
public void ReWriteStream()
{
var myText = new TextRange(rtb.CaretPosition.GetLineStartPosition(0), rtb.CaretPosition.GetLineStartPosition(1) ?? rtb.CaretPosition.DocumentEnd).Text;
...
}