如果您将一个400像素高的DataGridView放在一个300像素高的面板上,那么面板上会有一个滚动条,然后向下滚动以显示网格的下半部分,然后单击面板外部的控件,然后单击网格中的一条线,面板将向上滚动到顶部,网格中错误的行将被选中。
这不仅仅是一个DataGridView;它发生在任何比面板更高的控件上,例如Infragistics,Rich Text Box。我把它作为一个bug提交给Infragistics,但他们说这是微软的问题。
我已经尝试使用控件的所有相关事件,但面板滚动发生在事件触发之前。
有什么建议吗?
发布于 2009-01-07 18:00:44
我猜您正在将面板的AutoScroll属性设置为true。执行此操作时,切换应用程序会将滚动位置重置为零,而面板将重置其位置。
如果关闭AutoScroll并添加自己的滚动条,则可以设置滚动条的最大值和最小值以匹配面板的要求,然后在scrollbar的scroll事件中设置面板的滚动值。切换窗口时不会重置此选项。
类似于:
private void vScrollBar1_Scroll(object sender, ScrollEventArgs e)
{
panel1.VerticalScroll.Value = vScrollBar1.Value;
}
这是一个新的在我身上,我必须重新创造它。我可能不得不在我的网站上添加一篇关于它的文章:-)
发布于 2009-05-26 20:33:49
这是由于ScrollableControl类自动激发了ScrollToControl事件,并且事件处理程序滚动以显示获得焦点的控件的左上角。当可滚动容器控件只包含一个控件时,此行为没有帮助。我对这种行为感到非常沮丧,直到我找到了阻止它的方法。
停止此行为的方法是覆盖ScrollToControl事件处理程序,如下所示:
class PanelNoScrollOnFocus : Panel
{
protected override System.Drawing.Point ScrollToControl(Control activeControl)
{
return DisplayRectangle.Location;
}
}
用此面板控件替换您的面板控件。好了。
发布于 2009-12-09 19:29:24
感谢skypecake,它工作得很好:)这是你的控件的一个编辑版本,它还可以跟踪滚动条的位置:
class AutoScrollPanel : Panel
{
public AutoScrollPanel()
{
Enter += PanelNoScrollOnFocus_Enter;
Leave += PanelNoScrollOnFocus_Leave;
}
private System.Drawing.Point scrollLocation;
void PanelNoScrollOnFocus_Enter(object sender, System.EventArgs e)
{
// Set the scroll location back when the control regains focus.
HorizontalScroll.Value = scrollLocation.X;
VerticalScroll.Value = scrollLocation.Y;
}
void PanelNoScrollOnFocus_Leave(object sender, System.EventArgs e)
{
// Remember the scroll location when the control loses focus.
scrollLocation.X = HorizontalScroll.Value;
scrollLocation.Y = VerticalScroll.Value;
}
protected override System.Drawing.Point ScrollToControl(Control activeControl)
{
// When there's only 1 control in the panel and the user clicks
// on it, .NET tries to scroll to the control. This invariably
// forces the panel to scroll up. This little hack prevents that.
return DisplayRectangle.Location;
}
}
这只有在面板中只有一个控件时才有效(尽管我没有用多个控件测试过它)。
https://stackoverflow.com/questions/419774
复制相似问题