【问题标题】:Stop a tablelayoutpanel from scrolling阻止表格布局面板滚动
【发布时间】:2010-08-24 21:40:26
【问题描述】:

我有一个 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();
}

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

【问题讨论】:

    标签: c# user-interface controls scroll tablelayoutpanel


    【解决方案1】:

    您正在删除具有焦点的控件。它会尝试找到另一个焦点,如有必要,将其滚动到视图中。除了为另一个控件提供焦点之外,可能可行的一件事是添加 TextBox 并在 删除标签之前为其提供焦点。

    【讨论】:

      【解决方案2】:

      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
      

      【讨论】:

        猜你喜欢
        • 2011-06-15
        • 1970-01-01
        • 2015-03-21
        • 1970-01-01
        • 2010-09-30
        • 1970-01-01
        • 2013-11-26
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多