c# Panel with autoscroll - Srollbar position reset on a control focus

前端 未结 2 1268
夕颜
夕颜 2021-02-04 04:44

This is for a windows form.

Panel has AutoScroll = True

I am adding panels dynamically to the main panel which end up exceeding the main panel display rectangle.

相关标签:
2条回答
  • 2021-02-04 05:24

    I found the answer to the problem I was having here: Answer

    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;
        }
    }
    
    0 讨论(0)
  • 2021-02-04 05:36

    Thanks, this works great except I had to adjust for the panel's padding at the bottom. Just an FYI to any others who may be seeing some offsets.

    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;
    }
    
    0 讨论(0)
提交回复
热议问题