Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/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# 停止tablelayoutpanel的滚动_C#_User Interface_Controls_Scroll_Tablelayoutpanel - Fatal编程技术网

C# 停止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

我有一个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 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