views:

11

answers:

1

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;
    }
}
A: 

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.

jnosek
Autoscroll doesn't work in the compact framework - I had already tried it. Unless I had been doing something wrong, but the AutoScrollPosition always ended up as 0, 0. Have you done it in .netcf?
George Handlin
All Autoscroll in the .net cf does is add scroll bars if the content of the control extends beyond its bounds. I added more to my code sample above to make a bit more clear. You need to calculate the autoscroll position for the control yourself and set it on the EnabledChanged event of the input panel. I have been able to make it work in the .net cf
jnosek