输入面板打开时面板未滚动到焦点控件

发布于 2024-09-26 10:00:38 字数 1324 浏览 9 评论 0原文

正在开发 Windows Mobile 6.5 应用程序并遇到一个我认为会自动处理的问题。我在表单上有一个面板,并将其 AutoScroll 属性设置为 true。有一个文本框,显示焦点上的输入面板。为了进行测试,我将文本框放置在可见面板之外以强制滚动条。单击文本框会弹出输入面板,该面板在其 EnabledChanged 事件中调整面板大小。由于将焦点设置为可见区域之外的控件会强制面板滚动(我已经对此进行了测试并且它按预期工作),因此我希望当调整面板大小时,它会滚动到聚焦的文本框,但事实并非如此。

这是一些快速演示代码:

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

拥有 2024-10-03 10:00:38

问题在于,当调整面板大小时,它不会重新调整滚动位置以保持焦点中的控件可见。

您需要做的是调整面板的 AutoScrollPosition,以便在更改面板大小后,要保持显示的控件的底部边界可见。

对于单个控件,它看起来像这样:

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

阅读 AutoScrollPosition 以了解数学原理。

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

设置 AutoScrollPosition 有点违反直觉。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文