C# tabindex - 泛化(桌面应用程序)

发布于 2024-09-17 01:29:54 字数 419 浏览 1 评论 0原文

我有几个文本框。我想每次按 Enter 键时将用户指向下一个文本框。 文本框已正确设置 Tabindex。

我得到类似的信息:

 private void textBox_Description_KeyPress(object sender, KeyPressEventArgs e)
    {
        if (e.KeyChar == (char)Keys.Enter)
        {
            e.Handled = true;
            setFocusOnNextElement(sender);
        }
    } 

setFocusOnNextElement 应该是什么样子?如果我想让它变得通用。我可以解析每个控件,并找到接下来的内容,但我有一种感觉,这可以做得更好。

I have a few textboxes. I'd like to point the user each time to the next textbox, on pressing enter.
Textboxes have Tabindex set up correctly.

I got something like:

 private void textBox_Description_KeyPress(object sender, KeyPressEventArgs e)
    {
        if (e.KeyChar == (char)Keys.Enter)
        {
            e.Handled = true;
            setFocusOnNextElement(sender);
        }
    } 

How should setFocusOnNextElement look like? If i want to make it general. I could parse each control, and find what's next, but I have a feeling that this can be done nicer.

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

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

发布评论

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

评论(1

ゃ懵逼小萝莉 2024-09-24 01:29:57

我不建议像您拥有的那样构造该函数,因为它要求参数是一个对象

private static void SetFocusOnNextElement(Control control)
{
    Control target = Control.GetNextControl(control, true);

    if (target != null) target.Focus();
}

然后像这样调用它:

SetFocusOnNextElement((Control)sender);

I would not advise constructing the function just as you have it, as it would require that the parameter be an object.

private static void SetFocusOnNextElement(Control control)
{
    Control target = Control.GetNextControl(control, true);

    if (target != null) target.Focus();
}

Then invoke it like this:

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