Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/backbone.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#:在richtextbox中以相反顺序显示文本/updwards_C#_Richtextbox_Reverse_Rtf - Fatal编程技术网

C#:在richtextbox中以相反顺序显示文本/updwards

C#:在richtextbox中以相反顺序显示文本/updwards,c#,richtextbox,reverse,rtf,C#,Richtextbox,Reverse,Rtf,我正在尝试使用C#NET中的richtextbox控件创建“日志显示” 有没有办法按相反顺序/向上显示文本?(最新日志和日期将显示在顶部) 非常感谢你的帮助 简短回答 public void logLine(string line) { rtxtLoginMessage.Select(0, 0); rtxtLoginMessage.SelectedText = line + "\r\n"; } 您希望将选择设置为0,然后设置S

我正在尝试使用C#NET中的richtextbox控件创建“日志显示”

有没有办法按相反顺序/向上显示文本?(最新日志和日期将显示在顶部)


非常感谢你的帮助

简短回答

  public void logLine(string line)
  {     
       rtxtLoginMessage.Select(0, 0);        
       rtxtLoginMessage.SelectedText = line + "\r\n";
  } 
您希望将选择设置为0,然后设置
SelectedText
属性

public void logLine(string line)
{
    rtxtLoginMessage.Select(0, 0);    
    rtxtLoginMessage.SelectedText = line + Environment.NewLine;
} 
长答案

我是怎么做到的

使用,搜索RichTextBox控件并找到
AppendText
方法(按照基本类型搜索
TextBoxBase
)。看一下它的功能(为了方便起见,请参见下文)

您将看到它找到结束位置,设置内部选择,然后将
SelectedText
设置为新值。要在开头插入文本,只需找到开始位置,而不是结束位置

现在,您不必在每次要为文本添加前缀时都重复这段代码,您可以创建一个

注意:我只使用了
Try/Finally
块来匹配
AppendText
的实现。如果
宽度
高度
为0,我不确定我们为什么要恢复初始选择(如果您知道原因,请留下评论,因为我有兴趣了解)

此外,使用“Prepend”作为“Append”的对立面也有一些争议,因为直接的英语定义令人困惑(在谷歌搜索一下——有好几篇关于这个主题的帖子)。然而,如果你看一看,它已经成为一个公认的用途

嗯,


丹尼斯

不错。。。我可能得用这个。一直在考虑如何进行向下滚动、顶部插入日志……感谢PrependText扩展方法,我努力找到了与
richtextbox.Text.Insert(0,Text)
public void logLine(string line)
{
    rtxtLoginMessage.Select(0, 0);    
    rtxtLoginMessage.SelectedText = line + Environment.NewLine;
} 
public void AppendText(string text)
{
    if (text.Length > 0)
    {
        int num;
        int num2;
        this.GetSelectionStartAndLength(out num, out num2);
        try
        {
            int endPosition = this.GetEndPosition();
            this.SelectInternal(endPosition, endPosition, endPosition);
            this.SelectedText = text;
        }
        finally
        {
            if ((base.Width == 0) || (base.Height == 0))
            {
                this.Select(num, num2);
            }
        }
    }
}
public static void PrependText(this TextBoxBase textBox, string text)
{
    if (text.Length > 0)
    {
        var start = textBox.SelectionStart;
        var length = textBox.SelectionLength;

        try
        {
            textBox.Select(0, 0);
            textBox.SelectedText = text;
        }
        finally
        {
            if (textBox.Width == 0 || textBox.Height == 0)
                textBox.Select(start, length);
        }
    }
}