简体   繁体   English

如何取消选中 C# 中的单选按钮,同时仍然只允许第一个单选按钮可选项卡?

[英]How can I uncheck radiobuttons in C# while still allowing only the first radiobutton to be tabbable?

Not sure if the title makes much sense, so here is the full context:不确定标题是否有意义,所以这里是完整的上下文:

I'm coding in C#.我在 C# 中编码。 I've made an app with several UserControls, each one with many textboxes and radiobuttons.我制作了一个包含多个用户控件的应用程序,每个控件都有许多文本框和单选按钮。 All radiobuttons are placed in a panel in a set of 2, looking like this:所有单选按钮都放置在一组 2 个面板中,如下所示:

[ <label> O <radiobutton1text> O <radiobutton2text> ]

(while the first radiobutton have TabStop = true, and the second's TabStop = false) (而第一个单选按钮的 TabStop = true,第二个的 TabStop = false)

When tabbing to such panel, only radiobutton1text is focused, and when hitting the LeftArrow key the radiobutton2text is selected.当切换到这样的面板时,只有 radiobutton1text 被聚焦,当点击 LeftArrow 键时,radiobutton2text 被选中。 That's the desired outcome.这就是想要的结果。

In order to make a UserControl load faster the second (and above) time, I'm not closing it but rather replacing it with a different UserControl each time the contents need to change.为了使 UserControl 在第二次(及以上)时间加载更快,我没有关闭它,而是在每次内容需要更改时用不同的 UserControl 替换它。 But this rises an issue: When UserControl X is open, then on top of it I open UserControl Y and then back to X, the textboxes and radiobuttons still have the contents from the first session of when I had UserControl X open for the first time.但这引发了一个问题:当 UserControl X 打开时,在它上面我打开 UserControl Y 然后返回 X,文本框和单选按钮仍然具有第一次 session 的内容,当我第一次打开 UserControl X . (I need the contents of textboxes and radiobuttons to be reset after replacing a UserControl). (我需要在替换 UserControl 后重置文本框和单选按钮的内容)。

So I made a function that loops through all controls and empties their contents.所以我做了一个 function 循环遍历所有控件并清空它们的内容。 The problem is, when I uncheck the radiobuttons (and restore their TabStop state to true) in this function, the second radiobutton is tabbable after I check either one of them and then invoke the function, whereas it wasn't before going through this function. The problem is, when I uncheck the radiobuttons (and restore their TabStop state to true) in this function, the second radiobutton is tabbable after I check either one of them and then invoke the function, whereas it wasn't before going through this function .

The function: function:

        public void BackToMain(object sender, EventArgs e)
        {
            // Go through all controls and empty each TextBox, RichTextBox, RadioButton or ComboBox.
            int parentControlsCount = Controls.Count - 1;
            for (int i = parentControlsCount; i >= 0; i--)
            {
                if (Controls[i].HasChildren == true)
                {
                    int childrenControlsCount = Controls[i].Controls.Count - 1;
                    for (int j = childrenControlsCount; j >= 0; j--)
                    {
                        var controlType = Controls[i].Controls[j].GetType().ToString();

                        switch (controlType)
                        {
                            case "System.Windows.Forms.TextBox":
                            case "System.Windows.Forms.RichTextBox":
                                Controls[i].Controls[j].Text = null;
                                break;

                            case "System.Windows.Forms.RadioButton":
                                // Restore both properties to default value
                                ((RadioButton)Controls[i].Controls[j]).Checked = false;
                                if (j == 1)
                                    ((RadioButton)Controls[i].Controls[j]).TabStop = true;
                                else if (j == 2)
                                    ((RadioButton)Controls[i].Controls[j]).TabStop = false;
                                break;

                            case "System.Windows.Forms.ComboBox":
                                ((ComboBox)Controls[i].Controls[j]).SelectedIndex = -1;
                                break;
                        }
                    }
                }
            }
        }

What am I doing wrong?我究竟做错了什么?

I ended up applying this gross hack- a function on every second radiobutton's CheckedChange:我最终在每一个单选按钮的 CheckedChange 上应用了这个严重的 hack - function:

        private void DisableTabStopOnCheckedChange(object sender, EventArgs e)
        {
            // Assume the following STR:
            // 1. In any radiobutton panel, select any radiobutton (without ever invoking BackToMain function in the first post);
            // 2. Invoke the BackToMain function;
            // 3. In the same radiobutton panel as in step #1, click the second radiobutton.
            // Normally, without this function, if the user will now cycle through the controls using the Tab key, both the first and second radiobuttons will be tabbable,
            // and that's because in the BackToMain function we reset their Checked and TabStop properies, and that's something that should be handled automatically by the control itself.
            // Doing it manually means that for the *first time* selecting the second radiobutton, the first one's TabStop state won't update, which means both radiobuttons
            // will have the TabStop state set to true, causing both to be tabbable.
            // This is a gross hack to fix this by disabling TabStop on the first radio button if the second one is checked and the first one's TabStop state
            // is true (this should happen only after BackToMain has been invoked).
            if (((RadioButton)sender).Checked)
            {
                var firstRadioButton = ((RadioButton)sender).Parent.Controls[1];
                if (((RadioButton)firstRadioButton).TabStop == true)
                {
                    ((RadioButton)firstRadioButton).TabStop = false;
                }
            }
        }

Not a pretty solution, I know.我知道,这不是一个很好的解决方案。 But it works.但它有效。

暂无
暂无

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

相关问题 如何通过简单的代码从C#的同一面板中的RadioButtons组中获取Checked RadioButton? - How Can I get by simple code the Checked RadioButton From Group of RadioButtons Located in same panel in C#? 如何在仍然允许javascript运行的同时防止回发? - How can I prevent postback while still allowing javascript to run? C#如何取消选中动态创建的单选按钮? - C# how can I uncheck dynamically created radio buttons? C#-如果在GroupBox中选择了单选按钮,请在其他GroupBox中取消选择其他单选按钮 - C# - If A Radiobutton Selected In GroupBox, Unselect Other Radiobuttons In Other GroupBoxes 如何为单选按钮分配一个 id 并在 Xamarin Forms 后面的代码中访问它,以便不检查两个单选按钮 - How can I assign an id for radiobutton and access it at code behind in Xamarin Forms in order to not check both radiobuttons 如何在lambda表达式的{}中提供默认表达式,同时仍允许将其添加到其中? - How can I provide default expression in the { } of a lambda expression, while still allowing it to be added to? 如何在C#WPF中选中/取消选中列表框中的所有复选框? - How Can I Check/UnCheck All Checkboxes in Listbox in C# WPF? 如何评估IEnumerable <T> 同时对C#中的第一个和/或最后一个元素进行特殊处理? - How can I evaluate an IEnumerable<T> while giving special treatment to its first and/or last element in C#? C#:如何避免多个选中的单选按钮 - C#: How to avoid multiple checked RadioButtons C#:如何仅从字符串中返回第一组大写字母单词? - C#: How can I only return the first set of capital letter words from a string?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM