C# 停止tablelayoutpanel的滚动
我有一个C#.NET 3.0项目,它使用包含多行控件的TableLayoutPanel。如果向下滚动,使顶部项目不再可见,然后删除一列中的控件并将其替换为新控件,TableLayoutPanel将滚动回顶部C# 停止tablelayoutpanel的滚动,c#,user-interface,controls,scroll,tablelayoutpanel,C#,User Interface,Controls,Scroll,Tablelayoutpanel,我有一个C#.NET 3.0项目,它使用包含多行控件的TableLayoutPanel。如果向下滚动,使顶部项目不再可见,然后删除一列中的控件并将其替换为新控件,TableLayoutPanel将滚动回顶部 /// the panel in question private System.Windows.Forms.TableLayoutPanel impl_; /// The user has clicked a linklabel in the panel. replace it with
/// the panel in question
private System.Windows.Forms.TableLayoutPanel impl_;
/// The user has clicked a linklabel in the panel. replace it with an edit-box
private void OnClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
LinkLabel link_label = sender as LinkLabel;
TextBox new_edit = new TextBox();
// setup the new textbox...
TableLayoutPanelCellPosition pos = impl_.GetCellPosition(link_label);
impl_.Controls.Remove(link_label);
impl_.Controls.Add(new_edit, pos.Column, pos.Row);
new_edit.Focus();
}
我需要做什么来防止滚动位置改变?您正在移除具有焦点的控件。它将尝试找到另一个焦点,如有必要,将其滚动到视图中。除了为另一个控件提供焦点之外,还有一件可能有效的事情,那就是添加文本框并在移除标签之前为其提供焦点。Hans Passant的解决方案最适合上下文。但在其他无法使用焦点的情况下,可以使用滚动返回到上一个位置:
Point scrollPosition = tlp.AutoScrollPosition;
//make changes here
tlp.AutoScrollPosition = new Point(-scrollPosition.X, -scrollPosition.Y) //according to the documentation negative coordinates are returned but positive ones need to be assigned