在 C# 中使用不同的父控件对 Windows 窗体单选按钮进行分组 [英] Grouping Windows Forms Radiobuttons with different parent controls in C#

查看:33
本文介绍了在 C# 中使用不同的父控件对 Windows 窗体单选按钮进行分组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 Windows 窗体应用程序,其中有许多单选按钮.这些 RadioButtons 放置在 FlowLayoutPanel 中,它会自动为我安排它们.直接添加到 FlowLayoutPanel 的所有 RadioButtons 都被分组,这意味着我只能选择其中一个.但是,其中一些 RadioButtons 与 TextBox 配对,因此我可以在那里提供一些参数.但是为了妥善安排这一切,我在 FlowLayoutPanel 中添加了一个 Panel 控件,这样我就可以自己控制 RadioButton 和 TextBox 相对于彼此的对齐方式.

I've got a Windows Forms application in which I have a number of RadioButtons. These RadioButtons are placed within a FlowLayoutPanel which automatically arranges them for me. All RadioButtons that are directly added to the FlowLayoutPanel are grouped, meaning I can select only one of them. However, some of these RadioButtons are paired up with a TextBox so I can supply some argument there. But to have all this arranged properly, I add a Panel control to the FlowLayoutPanel so I can control the alignment of the RadioButton and TextBox relatively to each other myself.

这些 RadioButton 现在有自己的面板作为父控件,因此不再与其他 RadioButton 一起包含在单选组中.我读到 System.Web.UI 命名空间中的 RadioButtons 具有 GroupName 属性,但不幸的是,它们的 System.Windows.Forms 对应项缺少此属性.还有其他方法可以对这些单选按钮进行分组吗?我必须自己处理 onClick 事件吗?

These RadioButtons now have their own respective Panels as parent controls and thus are no longer included in the radio group with the other RadioButtons. I read that the the RadioButtons that are in the System.Web.UI namespace have a GroupName property, but unfortunately their System.Windows.Forms counterparts lack this property. Is there some other way I can group these radio buttons are am I going to have to handle onClick events myself?

谢谢,杰瑞

推荐答案

恐怕你得手动处理这个了……其实还不错,你可以把所有的 RadioButton 存储在一个列表中,并为所有这些使用单个事件处理程序:

I'm afraid you'll have to handle this manually... It's not so bad actually, you can probably just store all the RadioButton in a list, and use a single event handler for all of them:

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

这篇关于在 C# 中使用不同的父控件对 Windows 窗体单选按钮进行分组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆