Panel not scrolling to focused control when input panel opens

Working on a Windows Mobile 6.5 application and having an issue that I would think would be handled automatically. I have a panel on the form and have it's AutoScroll property set to true. Have a textbox that shows an inputpanel on focus. For testing, I place the textbox outside of the visible panel to force the scrollbar. Clicking in the textbox pops the inputpanel which in it's EnabledChanged event resizes the panel. Since setting the focus to a control that is outside of the visible area forces the panel to scroll (I've tested this and it works as expected), I would expect that when the panel is resized, it would scroll to the focused texbox, but it doesn't.

Here is some quick demo code:

public partial class Form3 : Form
{
    public Form3()
    {
        InitializeComponent();

        panel1.Size = this.ClientRectangle.Size;

        TextBox t = new TextBox();
        t.Size = new Size(100, 20);
        // put it out of the panel's bounds
        t.Location = new Point(10, 400);
        t.GotFocus += new EventHandler(t_GotFocus);
        t.LostFocus += new EventHandler(t_LostFocus);
        panel1.Controls.Add(t);

        t = new TextBox();
        t.Size = new Size(100, 200);
        t.Location = new Point(10,10);
        panel1.Controls.Add(t);
    }

    void t_LostFocus(object sender, EventArgs e)
    {
        inputPanel1.Enabled = false;
    }

    void t_GotFocus(object sender, EventArgs e)
    {
        inputPanel1.Enabled = true;
    }

    private void inputPanel1_EnabledChanged(object sender, EventArgs e)
    {
        if (inputPanel1.Enabled)
            panel1.Size = inputPanel1.VisibleDesktop.Size;
        else
            panel1.Size = this.ClientRectangle.Size;
    }
}

The problem is that when the panel is re-sized, it does not re-adjust the scroll position to keep the control that is in focus visible.

What you need to do is adjust the AutoScrollPosition of your panel so that the bottom bounds of the control you want to keep displayed is visible, after the panel's size is changed.

For a single control it would look something like this:

private void inputPanel1_EnabledChanged(object sender, EventArgs e)
{
     if (inputPanel1.Enabled)
     {
          panel1.Size = inputPanel1.VisibleDesktop.Size;
          panel1.AutoScrollPosition = new Point(0, t.Bounds.Bottom - panel1.Height - panel1.AutoScrollPosition.Y);
     }
     else
          panel1.Size = this.ClientRectangle.Size;
}

Read up on the AutoScrollPosition to understand how the math works.

http://msdn.microsoft.com/en-us/library/system.windows.forms.scrollablecontrol.autoscrollposition.aspx

Setting the AutoScrollPosition is a bit counter-intuitive.

链接地址: http://www.djcxy.com/p/53696.html

上一篇: WinForms临时禁用面板中的垂直滚动条

下一篇: 当输入面板打开时,面板不会滚动到聚焦控制