选择选项卡后如何将焦点集中到文本框?

发布于 2024-08-20 22:50:48 字数 465 浏览 8 评论 0 原文

我想在选择选项卡后将焦点集中到文本框,但无论我尝试什么,它都不起作用。我在这里看过类似的问题,但他们没有给我我需要的结果。这是我尝试过的。

    private void tabBDERip_Click(object sender, EventArgs e)
    {
        textBoxPassword.Focus();
    }

有人

    private void tabAll_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (tabAll.SelectedTab == tabBDERip)
        {
            textBoxPassword.Focus();
        }
    }

可以告诉我我做错了什么吗?

谢谢

I'd like give focus to a textBox after a Tab has been selected but no matter what I try it doesn't work. I've looked at similar questions here but they don't get me the results I need. Here is what Ive tried.

    private void tabBDERip_Click(object sender, EventArgs e)
    {
        textBoxPassword.Focus();
    }

and

    private void tabAll_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (tabAll.SelectedTab == tabBDERip)
        {
            textBoxPassword.Focus();
        }
    }

Can someone please tell me what I'm doing wrong?

Thanks

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

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

发布评论

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

评论(1

眼泪都笑了 2024-08-27 22:50:48

首先,当用户在 TabPage 内部单击而不是在标题上时,TabPage 控件的 Click 事件就会触发,因此您的 SelectedIndexChanged 事件是您想要使用的事件。

我刚刚测试了与您的代码非常相似的代码:

private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
{
    if (tabControl1.SelectedTab == tabPage2)
    {
        textBox4.Focus();
    }
}

并且运行良好。

密码文本框是否未启用或类似的情况?

如果您尝试在不同的控件上调用 Focus() ,这也不起作用吗?

如果您在 SelectedIndexChanged 代码中设置断点,它会被击中吗?

更新:有趣。如果断点没有被命中(在 if 之前),我会仔细检查您的事件处理程序是否已正确附加。在您的 Designer.cs 中查找类似以下内容的内容:

this.tabControl1.SelectedIndexChanged += new System.EventHandler(this.tabControl1_SelectedIndexChanged);

更新: 我将我的工作示例放在 http://www.ccswe.com/temp/SO_TextBoxFocus.zip 也许查看它会帮助您找出问题所在。

更新:将事件处理程序附加到表单上的控件的更简单方法:

1:选择要附加事件处理程序的 Control,然后单击 属性 窗口中的>事件 图标(闪电)。

替代文本 http://www.ccswe.com/temp/Attach_EventHandler_1.png

2:找到您要附加的事件,然后双击右侧的事件。

替代文本 http://www.ccswe.com/temp/Attach_EventHandler_2.png

3:系统将自动为您生成代码存根,并且该事件将附加在设计器中。

替代文本 http://www.ccswe.com/temp/Attach_EventHandler_3.png

如果您再次查看属性窗口,您现在将看到生成的方法的名称。

First thing the Click event of the TabPage control fires when the user clicks inside the TabPage not on the header so your SelectedIndexChanged event is the one you want to use.

I just tested code very similiar to yours:

private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
{
    if (tabControl1.SelectedTab == tabPage2)
    {
        textBox4.Focus();
    }
}

And it worked fine.

Is the password textbox not enabled or something like that?

If you try to call Focus() on a different control does that also not work?

If you set a breakpoint inside the SelectedIndexChanged code does it get hit?

Update: Interesting. If the breakpoint isn't getting hit (before the if) I would double check that your eventhandler is properly attached. Look in your designer.cs for something like:

this.tabControl1.SelectedIndexChanged += new System.EventHandler(this.tabControl1_SelectedIndexChanged);

Update: I put my working example at http://www.ccswe.com/temp/SO_TextBoxFocus.zip maybe looking at it will help you figure out where the issue is.

Update: The easier way to attach an event handler to a control on your form:

1: Select the Control to want to attach an event handler to and then click the Events icon (lightning bolt) in the Properties window.

alt text http://www.ccswe.com/temp/Attach_EventHandler_1.png

2: Find the event you want to attach to and double click to the right.

alt text http://www.ccswe.com/temp/Attach_EventHandler_2.png

3: A code stub will be automatically generated for you and the event will be attached in the designer.

alt text http://www.ccswe.com/temp/Attach_EventHandler_3.png

If you look at the properties window again you'll now see the name of the method that was generated.

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