0

很愚蠢的问题,但仍然很烦人。

问题是我有两个组框,其中的标题有单选按钮,覆盖了组框标题。

就像是

(x) I want pizza
*Pizza stuff*

( ) I want Hamburger
*Hamburger stuff*

由于它们现在位于不同的组框中,因此它们都可以被选中。有没有办法设置/强制单选按钮在同一个“组”中?就像在 HTML 中设置 name="WhatToEat" value="Pizza"第一个值,然后 name="WhatToEat" value="Hamburger"

或者我可以将组框的标题设置为像单选按钮或其他东西吗?

当然,我可以在 grop 框之外使用单选按钮,但我认为将标题作为单选按钮最有意义,并且看起来更好。

4

5 回答 5

3

如果您在表单上有所有单选按钮。您可以使用 RadioButton 变量来标记当前检查的内容。每次用户检查 RadioButton 时,如果它不是当前选中的 RadioButton,则取消选中当前选中的 RadioButton,并将当前选中的 RadioButton 设置为该 RadioButton。

这是我的代码:

public Form1(){
  InitializeComponents();
  currentChecked = radioButton1;
}
//Suppose the initially checked radio is radioButton1
RadioButton currentChecked;
//This is the CheckedChanged event handler used for all the radiobuttons
private void radioButtonChecked(object sender, EventArgs e)
{
        RadioButton r = (RadioButton)sender;
        if (r != currentChecked)
        {
            currentChecked.Checked = false;
            currentChecked = r;
        }
}

我的代码在不使用任何循环的情况下要简单得多。它需要额外currentChecked的费用,但并不多。

希望能帮助到你!

于 2013-06-03T14:42:04.210 回答
1

不,它必须在一组..

但是对于这种情况,您可以在 checked_change 事件中进行控制

Private Sub RadioButton2_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton2.CheckedChanged
    RadioButton1.Checked = Not RadioButton2.Checked
End Sub

Private Sub RadioButton1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton1.CheckedChanged
    RadioButton2.Checked = Not RadioButton1.Checked
End Sub
于 2013-06-03T14:13:53.697 回答
1

只需在运行时将它们移动到表单。使用 PointToScreen() 和 PointToClient() 将它们保持在您在设计时放置它们的相同位置。因此,您可以将“RadioButton1”、“RadioButton2”和“RadioButton3”替换为您的标题 RadioButtons:

Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    Dim RadioTitles() As RadioButton = {RadioButton1, RadioButton2, RadioButton3}
    For Each rb As RadioButton In RadioTitles
        Dim pt As Point = Me.PointToClient(rb.PointToScreen(New Point(0, 0)))
        Me.Controls.Add(rb)
        rb.Location = pt
        rb.BringToFront()
    Next
End Sub

*您可以在每个 RadioButton 的 Tag() 属性中放入一个应该是“标题”的值,然后搜索这些值,而不是将它们硬编码到数组中。或者,也许您可​​以以某种方式命名它们。

编辑:当像这样检查它们时,您可以使“标题”单选按钮启用/禁用其关联的 GroupBox: RadioButton GroupBox 标题

Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    Dim RadioTitles() As RadioButton = {RadioButton1, RadioButton2, RadioButton3}
    For Each rb As RadioButton In RadioTitles
        rb.Parent.Enabled = False
        rb.Tag = rb.Parent
        AddHandler rb.CheckedChanged, AddressOf TitleRadioButtons_CheckedChanged

        Dim pt As Point = Me.PointToClient(rb.PointToScreen(New Point(0, 0)))
        Me.Controls.Add(rb)
        rb.Location = pt
        rb.BringToFront()
    Next
End Sub

Private Sub TitleRadioButtons_CheckedChanged(sender As Object, e As System.EventArgs)
    Dim rb As RadioButton = DirectCast(sender, RadioButton)
    If Not IsNothing(rb.Tag) AndAlso TypeOf rb.Tag Is Control Then
        Dim ctl As Control = DirectCast(rb.Tag, Control)
        ctl.Enabled = rb.Checked
    End If
End Sub
于 2013-06-03T14:51:36.370 回答
0

不幸的是,这不是单选按钮的工作方式。

我相信您知道,单选按钮从容器中获取它们的分组。 据我所知,如果您希望能够完成您的要求,您可能需要编写自定义解决方案。例如,您可以在每个单选按钮上放置一个事件处理程序来触发相同的事件以取消选中其他框,例如

radioButton1.CheckedChanged += anyRadioButton_CheckedChanged;
radioButton2.CheckedChanged += anyRadioButton_CheckedChanged;
radioButton3.CheckedChanged += anyRadioButton_CheckedChanged;

...

private void anyRadioButton_CheckedChanged(object sender, EventArgs e)
{
    foreach (var control in this.Controls)
    {
        if(control is GroupBox)
        {
            foreach (var childControl in ((GroupBox)control).Controls)
            {
                if (childControl is RadioButton && childControl != sender)
                {
                    ((RadioButton)childControl).Checked = false;
                }
            }
        }
    }
}
于 2013-06-03T14:14:31.857 回答
0

您可以将文本设置GroupBox为空字符串并RadioButton覆盖它。诀窍是把它放在表单上,​​但移动它看起来像是GroupBox. 但这仅在您的组框是静态且不会移动的情况下就足够了。否则最好使用@matzone 提出的解决方案。但即使在这种情况下,您也可以将所有单选按钮放在代码中的集合中,并对所有单选按钮使用唯一的事件处理程序。就像是

private List<RadioButton> radioButtons;

public YourFormConstructor()
{
    InitializeComponent();
    radioButtons.Add(radio1);
    radioButtons.Add(radio2);
    radioButtons.Add(radio3);
    foreach (var radio in radioButtons)
        radio.CheckedChanged += RadioCheckedChanged;
}

private void CheckedChanged(object sender, EventArgs e)
{
    var thisRadio = sender as RadioButton;
    if (!thisRadio.Checked)
        return;
    foreach (var radio in radioButtons)
        if (radio != thisRadio)
            radio.Checked = false;
}
于 2013-06-03T14:16:32.777 回答