选择选项卡后如何将焦点集中到文本框?
我想在选择选项卡后将焦点集中到文本框,但无论我尝试什么,它都不起作用。我在这里看过类似的问题,但他们没有给我我需要的结果。这是我尝试过的。
private void tabBDERip_Click(object sender, EventArgs e)
{
textBoxPassword.Focus();
}
有人
private void tabAll_SelectedIndexChanged(object sender, EventArgs e)
{
if (tabAll.SelectedTab == tabBDERip)
{
textBoxPassword.Focus();
}
}
可以告诉我我做错了什么吗?
谢谢
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,当用户在
TabPage
内部单击而不是在标题上时,TabPage
控件的Click
事件就会触发,因此您的SelectedIndexChanged
事件是您想要使用的事件。我刚刚测试了与您的代码非常相似的代码:
并且运行良好。
密码文本框是否未启用或类似的情况?
如果您尝试在不同的控件上调用
Focus()
,这也不起作用吗?如果您在
SelectedIndexChanged
代码中设置断点,它会被击中吗?更新:有趣。如果断点没有被命中(在
if
之前),我会仔细检查您的事件处理程序是否已正确附加。在您的 Designer.cs 中查找类似以下内容的内容:更新: 我将我的工作示例放在 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 theTabPage
control fires when the user clicks inside theTabPage
not on the header so yourSelectedIndexChanged
event is the one you want to use.I just tested code very similiar to yours:
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: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 theEvents
icon (lightning bolt) in theProperties
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.