WinForm - TabStop 不工作

发布于 2024-09-15 16:13:15 字数 813 浏览 7 评论 0原文

我有一个带有 3 个组框的 WinForm,其中一个带有组合框,两个带有单选按钮。我将它们及其子控件全部设置为“TabStop = false”,但是当我使用 TAB 循环时,最后两个组框中当前选定的单选按钮将获得焦点。

如果没有办法改变这种行为,那么什么是抓住并转移焦点的好事件呢?我找不到“OnFocus”事件。


解决方案是设置一种方法(下面的代码)来处理表单中每个单选按钮的“Enter”事件(如果您希望如此)。

实际上,我只对第一个组框的单选按钮执行了此操作,并且它有效,第二个组框的单选按钮没有获得焦点,即使它们的“Enter”事件未被处理。这不是您所期望的行为。

private void radiobuttonXGroup1_Enter(object sender, EventArgs e)
{
   SomeOtherControl.Focus();
}

在 *.Designer.cs 文件中,您编辑每个 Enter 事件(对于每个单选按钮)以指向一个事件处理程序(上述方法)。

this.radiobutton1Group1.Enter += new System.EventHandler(this.radiobuttonXGroup1_Enter);
this.radiobutton2Group1.Enter += new System.EventHandler(this.radiobuttonXGroup1_Enter);
this.radiobutton3Group1.Enter += new System.EventHandler(this.radiobuttonXGroup1_Enter);

I have a WinForm with 3 group boxes, one with combo boxes, and two with radio buttons. I set all of them and their children controls to "TabStop = false", but when I cycle with TAB, the currently selected radio button in each of the last two group boxes gets focused.

If there's no way to change this behavior, what would be a good event to catch and move the focus away? I can't find an "OnFocus" event.


The solution is to set one method (code below) to handle the "Enter" event of every radio button in the form (if that's what you wish).

Actually, I only did it for the radio buttons of the first group box and it worked, the second group box's radio buttons don't get focus, even though their "Enter" events are not handled. This is not the behavior you would have expected.

private void radiobuttonXGroup1_Enter(object sender, EventArgs e)
{
   SomeOtherControl.Focus();
}

In the *.Designer.cs file you edit every Enter event (for each radio button) to point to one event handler (the above method).

this.radiobutton1Group1.Enter += new System.EventHandler(this.radiobuttonXGroup1_Enter);
this.radiobutton2Group1.Enter += new System.EventHandler(this.radiobuttonXGroup1_Enter);
this.radiobutton3Group1.Enter += new System.EventHandler(this.radiobuttonXGroup1_Enter);

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

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

发布评论

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

评论(2

无敌元气妹 2024-09-22 16:13:21

将 RadioButton 上的 TabStop 设置为 False 以防止按 Tab 键切换到控件,直到您真正选择单选按钮而不需要像 @msergeant 建议的任何其他覆盖。

编辑

以下代码可防止代码获取 Tab 键事件:

private void radioButton1_CheckedChanged(object sender, EventArgs e)
{
   radioButton1.TabStop = false;
}

单选按钮相对于 Tab 的行为与其他控件不同,因为它们基于设置选项卡索引或放置单选按钮在集合或组中工作组框中的按钮。

Setting the TabStop to False on a RadioButton to prevent tabbing to the control works until you actully select the radio button without any additional overrides like suggested by @msergeant.

EDIT

The following code prevents the code from getting a tab key event:

private void radioButton1_CheckedChanged(object sender, EventArgs e)
{
   radioButton1.TabStop = false;
}

Radio buttons behave differently with respect to Tab from other controls in that they work in sets or groups based on setting the tab index or placing then radio buttons in a group box.

雨后彩虹 2024-09-22 16:13:21

RadioButton.TabStop 的 MSDN 文档指出“此 API 支持 . NET Framework 基础结构,并不打算直接从您的代码中使用”。这基本上意味着“这不会像你期望的那样工作”。

也就是说,当按钮获得焦点时,Enter 事件将触发。您可以尝试使用它将焦点移动到另一个控件。

The MSDN documentation for RadioButton.TabStop states "This API supports the .NET Framework infrastructure and is not intended to be used directly from your code". Which basically means, "This isn't going to work how you expect it to".

With that said, the Enter event will fire when the button receives the focus. You can try to use that to move focus to another control.

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