English 中文(简体)
C#中带有不同母子控制的视窗广播台
原标题:Grouping Windows Forms Radiobuttons with different parent controls in C#

I ve got aWindows Forms application in which I have a number of RadioButtons. 这些电台设在FlowLayoutPanel,自动为我安排。 所有直接加入流放线的Button电台都进行了分类,这意味着我只能选择其中之一。 然而,其中一些广播公司与文本Box配对,因此我可以在那里提供一些论点。 但是,为了妥善安排所有这些安排,我对流放通道增加了一个小组的控制,以便我能够控制Button电台和文本Box电台的相互配合。

这些Buttons电台现在有自己的小组控制,因此不再与其他Buttons电台一起纳入无线电组。 我读到的是,位于 αspace有团体财产,但不幸的是,这些财产有:>> >> > > / 。 我能否以某种其他方式把这些无线电纽子集中起来,我是否必须亲自处理Click事件?

Thanks, Jerry

最佳回答

恐怕你不得不人工处理这一疾病...... 事实上,这并不是非常糟糕的,你可能只是把布特顿的所有电台都列入一个名单,并且为他们使用一个单一的活动手:

private List<RadioButton> _radioButtonGroup = new List<RadioButton>();
private void radioButton_CheckedChanged(object sender, EventArgs e)
{
    RadioButton rb = (RadioButton)sender;
    if (rb.Checked)
    {
        foreach(RadioButton other in _radioButtonGroup)
        {
            if (other == rb)
            {
                continue;
            }
            other.Checked = false;
        }
    }
}
问题回答

我同意“JonH”——使用标签是这样做的最佳方式(im)。

  private void FormLoad(object sender, EventArgs e)
  {
     radioCsv.Tag = DataTargetTypes.CsvFile;
     radioTabbed.Tag = DataTargetTypes.TxtFile;
     radioSas.Tag = DataTargetTypes.SasFile;
  }

  private void RadioButtonCheckedChanged(object sender, EventArgs e)
  {
     var radio = (RadioButton) sender;
     this.DataDestinationType = (DataTargetTypes)radio.Tag;
  }

这里比第一个答案稍有改进:设立一个无线电组级,负责组群功能,并为标准的关键导航(顶/下级钥匙)提供额外支持,并进行制表工作。

为了使用该节目,简单地宣布一个无线电集团成员以你的形式,并(在开始编辑后)按适当顺序通过所有无线电台。

public class RadioGroup
{
    List<RadioButton> _radioButtons;

    public RadioGroup(params RadioButton[] radioButtons)
    {
        _radioButtons = new List<RadioButton>(radioButtons);
        foreach (RadioButton radioButton in _radioButtons)
        {
            radioButton.TabStop = false;
            radioButton.KeyUp += new KeyEventHandler(radioButton_KeyUp);
            radioButton.CheckedChanged += new EventHandler(radioButton_CheckedChanged);
        }
        _radioButtons[0].TabStop = true;
    }

    void radioButton_KeyUp(object sender, KeyEventArgs e)
    {
        e.Handled = true;
        RadioButton radioButton = (RadioButton)sender;
        int index = _radioButtons.IndexOf(radioButton);

        if (e.KeyCode == Keys.Down)
        {
            index++;
            if (index >= _radioButtons.Count)
            {
                index = 0;
            }
            e.Handled = true;
        }
        else if (e.KeyCode == Keys.Up)
        {
            index--;
            if (index < 0)
            {
                index = _radioButtons.Count - 1;
            }
            e.Handled = true;
        }

        radioButton = _radioButtons[index];
        radioButton.Focus();
        radioButton.Select();
    }

    void radioButton_CheckedChanged(object sender, EventArgs e)
    {
        RadioButton currentRadioButton = (RadioButton)sender;

        if (currentRadioButton.Checked)
        {
            foreach (RadioButton radioButton in _radioButtons)
            {
                if (!radioButton.Equals(currentRadioButton))
                {
                    radioButton.Checked = false;
                }
            }
        }
    }
}

有一个洞察点:由于Button电台已经处理上下级钥匙,因此,上下层/下级钥匙与现有的广播Button电台的班级工作相当顺利。 一种容易的办法,将其固定在Button电台下级,并转而处理上下级钥匙:

public class RadioButtonEx : RadioButton
{
    protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
    {
        if (keyData == Keys.Up || keyData == Keys.Down)
        {
            return true;
        }

        return base.ProcessCmdKey(ref msg, keyData);
    }
}

@Jerry, I m 不太熟悉Windows表格,但我会枪声。 如果有一个名为Tag的财产,你可向每个广播电台打上独特的标签。





相关问题
Bring window to foreground after Mutex fails

I was wondering if someone can tell me what would be the best way to bring my application to the foreground if a mutex was not able to be created for a new instance. E.g.: Application X is running ...

How to start WinForm app minimized to tray?

I ve successfully created an app that minimizes to the tray using a NotifyIcon. When the form is manually closed it is successfully hidden from the desktop, taskbar, and alt-tab. The problem occurs ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

Handle DataTable.DataRow cell change event

I have a DataTable that has several DataColumns and DataRow. Now i would like to handle an event when cell of this DataRow is changed. How to do this in c#?

Apparent Memory Leak in DataGridView

How do you force a DataGridView to release its reference to a bound DataSet? We have a rather large dataset being displayed in a DataGridView and noticed that resources were not being freed after the ...

ALT Key Shortcuts Hidden

I am using VS2008 and creating forms. By default, the underscore of the character in a textbox when using an ampersand is not shown when I run the application. ex. "&Goto Here" is not ...

WPF-XAML window in Winforms Application

I have a Winforms application coded in VS C# 2008 and want to insert a WPF window into the window pane of Winforms application. Could you explain me how this is done.