简体   繁体   English

如何防止以编程方式更改RadioButton.Check从TabOrder中删除控件?

[英]How to prevent programmatically changing RadioButton.Checked from removing control from TabOrder?

I have a couple of RadioButtons on a C# Winforms project. 我在C#Winforms项目中有几个RadioButtons。 I want to provide a "clear form" button which should reset the radiobuttons. 我想提供一个“清除表单”按钮,该按钮应重置单选按钮。

Currently I just set the RadioButton.Checked values to false and it appears to be fine. 目前,我只是将RadioButton.Checked值设置为false,这似乎很好。

rdbBlockRelease.Checked = false;
rdbRegularRelease.Checked = false;

The issue is when you start to tab through the form after clearing it. 问题是当您清除表单后开始在表单中使用Tab键时。

Before you tab through the form, the tab order works as expected. 在通过表单进行制表之前,制表顺序将按预期工作。 If you check a radiobutton and then clear the form the radiobuttons appear to be removed from the taborder. 如果选中单选按钮,然后清除表格,则单选按钮似乎已从选项卡中删除。

I believe this is because of how radiobuttons and tab orders work. 我相信这是因为单选按钮和选项卡顺序如何工作。 In a radio button group, if all radio buttons are unchecked, the radio button with the lowest taborder in the group will gain focus when you tab to it. 在单选按钮组中,如果所有单选按钮都未选中,则选项卡顺序中最低的单选按钮将在您选中时获得焦点。

If you check a radio button and then tab to the group, unchecked radio buttons appear to be skipped; 如果您选中一个单选按钮,然后转到该组,则未选中的单选按钮似乎被跳过; the focus goes to the checked radio button. 焦点转到选中的单选按钮。

Keeping this in mind, I believe what is happening the form thinks the radio button group still has a checked radio button and changing the radiobutton.checked values doesn't appear to affect that. 记住这一点,我相信正在发生的事情是表格认为单选按钮组仍然具有选中的单选按钮并且更改了单选按钮。选中的值似乎不会对此产生影响。 So when the user tries to tab to the radio button group after clearing the form, the form says something like: 因此,当用户在清除表单后尝试跳至单选按钮组时,表单会显示类似以下内容:

"Oh, okay. Give focus to the selected radio button. Oh, there's no selected radio button. Give focus to the next item in tab order." “哦,好吧。将焦点放在选定的单选按钮上。哦,没有选定的单选按钮。将焦点放在选项卡顺序中的下一个项目上。”

Now comes my question. 现在是我的问题。 How do I prevent this? 我该如何预防? How do I make the user able to tab to the radio button group after clearing a form? 清除表单后,如何使用户能够跳到单选按钮组?

I'm using Visual Studio 2017. 我正在使用Visual Studio 2017。

They have written code to implement this feature, but the code has some bug: 他们已经编写了实现此功能的代码,但是代码存在一些错误:

When using tab key to move between group of radio buttons, it tries to select the first checked radio button. 当使用Tab键在单选按钮组之间移动时,它将尝试选择第一个选中的单选按钮。 But when there is no checked radio button, it neglects the group. 但是,当没有选中的单选按钮时,它将忽略该组。

There are some methods in RadioButton source code like PerformAutoUpdates and WipeTabStops which are responsible to implement the behavior of that Tab key. RadioButton源代码中有一些方法(例如PerformAutoUpdatesWipeTabStops负责实现该Tab键的行为。 Those methods check for AutoCheck and they work only in case AutoCheck is true . 这些方法检查AutoCheck并且仅在AutoChecktrue情况下才起作用。

So if you want to change the behavior, it's enough to set AutoCheck property to false and handle click event of all radio buttons yourself and revert the Checked property yourself. 因此,如果要更改行为,将AutoCheck属性设置为false并自己处理所有单选按钮的click事件并自己还原Checked属性就足够了。

Yes, I know it seems irrelevant at first glance! 是的,我知道乍一看似乎无关紧要! But that's it. 就是这样。

private void radioButton_Click(object sender, EventArgs e)
{
    var radio = (RadioButton)sender;
    radio.Checked = !radio.Checked;
}

Note: Instead of above event handler, you also can create your own radio button deriving from RadioButton and set AutoCheck to false and override OnClick and set Checked= !Checked . 注意:除了上面的事件处理程序以外,您还可以创建自RadioButton派生的单选按钮,并将AutoCheck设置为false并覆盖OnClick并设置Checked= !Checked

While Reza's answer will allow you have no default value selected for a radio button group, you should still consider just having a default value. 虽然Reza的答案将使您没有为单选按钮组选择默认值,但您仍应考虑仅具有默认值。

As Craig W. mentioned, before deleting his comment, most forms will have one of the radio buttons selected by default. 正如克雷格·W(Craig W.)所提到的,在删除他的注释之前,大多数表单默认情况下会选中其中一个单选按钮。 By doing like most, this problem can be prevented. 像大多数人一样,可以避免此问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM