2010-08-24 116 views
1

我有一个C#.NET 3.0项目,使用TableLayoutPanel包含几行控件。如果向下滚动以使顶层项目不再可见,则删除一列中的控件并将其替换为新控件,TableLayoutPanel将滚动回顶端。停止从滚动tablelayoutnel

/// 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(); 
} 

为了防止滚动位置发生变化,我需要做些什么?

回答

2

您正在移除具有焦点的控件。它会尝试找到另一个焦点,如果需要滚动查看。从另一个控制焦点开始,有一点可能会起作用,那就是添加文本框并在之前为焦点删除标签。

0

Hans Passant的解决方案对于上下文是最好的。但在其他情况下,如果您无法专注工作,则可以使用AutoScollPosition回滚到上一个位置:

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