如何防止以编程方式更改 RadioButton.Checked 从 TabOrder 中删除控件? [英] How to prevent programmatically changing RadioButton.Checked from removing control from TabOrder?

查看:24
本文介绍了如何防止以编程方式更改 RadioButton.Checked 从 TabOrder 中删除控件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 C# Winforms 项目中有几个 RadioButton.我想提供一个清除表单"按钮,它应该重置单选按钮.

I have a couple of RadioButtons on a C# Winforms project. I want to provide a "clear form" button which should reset the radiobuttons.

目前我只是将 RadioButton.Checked 值设置为 false,它似乎没问题.

Currently I just set the RadioButton.Checked values to false and it appears to be fine.

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

问题是当您在清除表单后开始使用 Tab 键时.

The issue is when you start to tab through the form after clearing it.

在通过 Tab 键浏览表单之前,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.

我相信这是因为单选按钮和选项卡顺序的工作方式.在单选按钮组中,如果所有单选按钮都未选中,则当您使用 Tab 键时,组中 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.

如果您选中一个单选按钮,然后按 Tab 键切换到该组,则未选中的单选按钮似乎会被跳过;焦点转到选中的单选按钮.

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.

记住这一点,我相信发生的事情表单认为单选按钮组仍然有一个选中的单选按钮并且更改 radiobutton.checked 值似乎不会影响它.因此,当用户在清除表单后尝试使用 Tab 键切换到单选按钮组时,表单会显示如下内容:

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:

哦,好的.将焦点放在选定的单选按钮上.哦,没有选定的单选按钮.按 Tab 键顺序将焦点放在下一项."

"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."

现在我的问题来了.我如何防止这种情况?清除表单后,如何让用户能够使用 Tab 键切换到单选按钮组?

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?

我使用的是 Visual Studio 2017.

I'm using Visual Studio 2017.

推荐答案

他们已经写了代码来实现这个功能,但是代码有一些bug:

They have written code to implement this feature, but the code has some bug:

当使用 Tab 键在单选按钮组之间移动时,它会尝试选择第一个选中的单选按钮.但是当没有选中的单选按钮时,它会忽略组.

RadioButton 源代码中有一些方法,例如 PerformAutoUpdatesWipeTabStops 负责实现 Tab 键的行为.这些方法检查 AutoCheck 并且它们仅在 AutoChecktrue 的情况下工作.

There are some methods in RadioButton source code like PerformAutoUpdates and WipeTabStops which are responsible to implement the behavior of that Tab key. Those methods check for AutoCheck and they work only in case AutoCheck is true.

因此,如果您想更改行为,只需将 AutoCheck 属性设置为 false 并自行处理所有单选按钮的点击事件并恢复 Checked 自己的财产.

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.

是的,我知道乍一看似乎无关紧要!但就是这样.

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

注意:除了上面的事件处理程序,您还可以创建自己的单选按钮,派生自 RadioButton 并将 AutoCheck 设置为 false 并覆盖 OnClick 并设置 Checked= !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.Checked 从 TabOrder 中删除控件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆