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



不确定标题是否有意义,所以这里是完整的上下文:

我正在用C#进行编码。我制作了一个带有多个UserControls的应用程序,每个UserControls都有许多文本框和单选按钮。所有的单选按钮都放在一个面板上,一组2个,看起来像这样:

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

(第一个单选按钮的TabStop=true,第二个单选按钮为TabStop=false(

当选项卡切换到这样的面板时,只有单选按钮1文本被聚焦,而当按下左箭头键时,单选按钮2文本被选中。这就是我们想要的结果。

为了使UserControl在第二次(及以上(更快地加载,我不会关闭它,而是在每次内容需要更改时用不同的UserControl替换它。但这引发了一个问题:当UserControl X打开时,我在它上面打开UserControl Y,然后回到X,文本框和单选按钮仍然包含我第一次打开UserControl X时的第一个会话的内容。(我需要在替换UserControl后重置文本框和单选按钮的内容(。

所以我做了一个函数,循环所有控件并清空它们的内容。问题是,当我取消选中该函数中的单选按钮(并将其TabStop状态恢复为true(时,在我选中其中一个单选按钮并调用该函数之后,第二个单选按钮是可选项卡的,而在执行该函数之前是不可选项卡的。

功能:

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:上的函数

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

我知道这不是一个很好的解决方案。但它是有效的。

相关内容

  • 没有找到相关文章

最新更新