[英]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.