滚动到 C# TextBox 的底部
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1228675/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Scroll to bottom of C# TextBox
提问by SnAzBaZ
I have a TextBox on a C# Forms Application. I populate the TextBox with information on the Load event of the form. I then call the following:
我在 C# 表单应用程序上有一个 TextBox。我使用有关窗体的 Load 事件的信息填充 TextBox。然后我调用以下内容:
this.txtLogEntries.SelectionStart = txtLogEntries.Text.Length;
this.txtLogEntries.ScrollToCaret();
However the TextBox does not scroll to the bottom ?
但是 TextBox 没有滚动到底部?
This only applies to the Load event though. I also update this TextBox from other parts of the application once it's running, and as soon as one of these events update's the TextBox, it is scrolled to the bottom.
但这仅适用于 Load 事件。一旦它运行,我也会从应用程序的其他部分更新这个 TextBox,一旦这些事件之一更新了 TextBox,它就会滚动到底部。
So, how can I get it to scroll to the bottom when pre populating the TextBox in the Form Load event?
那么,在 Form Load 事件中预填充 TextBox 时,如何让它滚动到底部?
采纳答案by bernhof
Try putting the code in the Form's Shown event:
尝试将代码放在 Form 的 Shown 事件中:
private void myForm_Shown(object sender, EventArgs e)
{
txtLogEntries.SelectionStart = txtLogEntries.Text.Length;
txtLogEntries.ScrollToCaret();
}
回答by Joey
While the Load
event (occurs beforethe Form is shown) is processed, there is no form to display yet, and thus no visual state has been established. Scrolling a non-visible control therefore very likely doesn't do anything because—hey, there is nothing to scroll as a scrolling viewport is just a view on the control but not part of its state.
在处理Load
事件(在显示表单之前发生)时,还没有要显示的表单,因此还没有建立视觉状态。因此滚动一个不可见的控件很可能不会做任何事情,因为——嘿,没有什么可滚动的,因为滚动视口只是控件上的一个视图,而不是其状态的一部分。
You may have more success with moving the scrolling part into the Shown
event (occurs afterthe form is first shown) of the form
将滚动部分移动到表单的Shown
事件(在表单首次显示之后发生)可能会取得更大的成功