c带有自动滚动的面板-控制焦点上的滚动条位置重置



这是用于windows窗体的。

面板具有AutoScroll=True

我正在向主面板动态添加面板,这些面板最终会超过主面板显示矩形。然后将标签、组合框和文本框添加到添加的面板中。

如果我选择一个组合框或文本框,它会将主面板滚动条的位置重置为0,并且组合框的下拉菜单也会放在屏幕X、Y上,如果滚动条没有重置,它应该在那里。

我正在考虑在选择控件时保存滚动位置。经过测试,滚动位置似乎还没有重置,所以我可以在这里捕捉到它。然后在面板的某个事件上恢复滚动位置,我希望如此我正试图弄清楚我将使用什么事件来恢复滚动位置我还希望在执行此操作时,下拉菜单将放置在正确的x,y处。

更好的解决方案是在面板控件的基础上创建一个自定义控件,并可能覆盖事件?这样,我就不需要每次使用滚动面板时都保存滚动位置,从而打乱我的项目。

我在这里找到了问题的答案:回答

public class CustomPanel : System.Windows.Forms.Panel
{
    protected override System.Drawing.Point ScrollToControl(System.Windows.Forms.Control activeControl)
    {
        // Returning the current location prevents the panel from
        // scrolling to the active control when the panel loses and regains focus
        return this.DisplayRectangle.Location;
    }
}

谢谢,这很好,除了我必须调整底部的面板填充。仅供其他可能看到一些抵消的人参考。

protected override System.Drawing.Point ScrollToControl(System.Windows.Forms.Control activeControl)
{
    Point retPt = DisplayRectangle.Location;
    retPt.Offset(new Point(-1*Padding.Left, -1*Padding.Bottom));
    return retPt;
}

最新更新