我有一个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();
}我需要做什么才能防止滚动位置发生变化?
发布于 2010-08-25 06:50:48
您正在移除具有焦点的控件。它将尝试找到另一个给予焦点,如有必要将其滚动到视图中。除了给另一个控件提供焦点之外,有一件事可能会起作用,那就是添加TextBox,并在移除标签之前给它提供焦点。
发布于 2016-04-06 15:54:34
汉斯·帕桑特的解决方案是最适合这种情况的。但在其他无法使用焦点的情况下,可以使用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 assignedhttps://stackoverflow.com/questions/3561262
复制相似问题