簡體   English   中英

如何取消選中 C# 中的單選按鈕,同時仍然只允許第一個單選按鈕可選項卡?

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

不確定標題是否有意義,所以這里是完整的上下文:

我在 C# 中編碼。 我制作了一個包含多個用戶控件的應用程序,每個控件都有許多文本框和單選按鈕。 所有單選按鈕都放置在一組 2 個面板中,如下所示:

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

(而第一個單選按鈕的 TabStop = true,第二個的 TabStop = false)

當切換到這樣的面板時,只有 radiobutton1text 被聚焦,當點擊 LeftArrow 鍵時,radiobutton2text 被選中。 這就是想要的結果。

為了使 UserControl 在第二次(及以上)時間加載更快,我沒有關閉它,而是在每次內容需要更改時用不同的 UserControl 替換它。 但這引發了一個問題:當 UserControl X 打開時,在它上面我打開 UserControl Y 然后返回 X,文本框和單選按鈕仍然具有第一次 session 的內容,當我第一次打開 UserControl X . (我需要在替換 UserControl 后重置文本框和單選按鈕的內容)。

所以我做了一個 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 .

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;
                        }
                    }
                }
            }
        }

我究竟做錯了什么?

我最終在每一個單選按鈕的 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;
                }
            }
        }

我知道,這不是一個很好的解決方案。 但它有效。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM