C#中经常用到RichTextBox控件,用于显示数据,那么如果在写入数据后,让RichTextBox始终得到焦点或让RichTextBox始终显示最下方的最新数据呢?
以前一般用到的方法是使用 Focus 先获取焦点,然后 Select 方法,滚动到最下方,这样效率不免低下,而且多写代码:
richTextBox1.Focus() ;
richTextBox1.Select(richTextBox1.Text.Length, 0);
参考这样的自动滚动到最下方的事件函数:
private void richTextBox1_TextChanged(object sender, EventArgs e)
{//滚动到最下方
if (richTextBox1.Lines.Length > 8000)
{
int n = 3000;
int start = richTextBox1.GetFirstCharIndexFromLine(0);//第一行第一个字符的索引
int end = richTextBox1.GetFirstCharIndexFromLine(n);//第n行第一个字符的索引
richTextBox1.Select(start, end);//选中N行
richTextBox1.SelectedText = "";//设置前N行的内容为空
}
richTextBox1.Focus() ;
richTextBox1.Select(richTextBox1.Text.Length, 0);
}
实际上有更简单的方法,用 RichTextBox.AppendText 方法追加数据后,只要再设置 RichTextBox 的 HideSelection 属性为 false 即可。
RichTextBox.HideSelection 属性是继承自 TextBoxBase:
“Gets or sets a value indicating whether the selected text in the text box control remains highlighted when the control loses focus.”
当RichTextBox.HideSelection值为
true, the selected text does not appear highlighted when the text box control loses focus;
false, the selected text remains highlighted when the text box control loses focus.
The default is true.
意思就是,当RichTextBox.HideSelection为flase时,无论 RichTextBox 是否获取焦点,RichTextBox控件都将被重点强调并显示,相当于始终有焦点的效果,这样就实现了 RichTextBox.AppendText 追加数据后,自动滚动到最下方的效果了。
敢达决战官方正版 安卓版v6.7.9
下载敢达决战 安卓版v6.7.9
下载像素火影骨架佐助 (Perseverance Fire Shadow)手机版v1.16
下载要塞英雄 安卓版v33.20.0-39082670-Android
下载梦想城镇vivo最新版本 安卓版v12.0.1
梦想城镇vivo版是这款卡通风模拟经营类手游的渠道服版本,玩
怦然心动的瞬间 安卓版v1.0
怦然心动的瞬间是一款真人向的恋爱互动游戏,在游戏中玩家将扮演
曼尼汉堡店游戏 安卓版v1.0.3
曼尼汉堡店是一款非常好玩的精品恐怖类型冒险游戏,在这款游戏中
现代总统模拟器去广告版 安卓版v1.0.46
现代总统模拟器是一款休闲养成类游戏,可能对于不少的玩家来说都
现代总统模拟器付费完整版 安卓版v1.0.46
现代总统模拟器高级版在商店是需要付费的,相对于普通版本,高级