日期:2014-05-18  浏览次数:21033 次

如何实现一个RadioGroupBox控件
在用户界面中,经常需要这样的逻辑——当选择某个RadioButton时启用一组控件(如TextBox,ComboBox等),当这个RadioButton未被选中时,相应的一组控件被禁用。为求方便,需要UserControl。它具有如下特性:
1、具有一个单选钮
2、能作为一个容器
3、具有GroupBox一样的边框和标题栏
4、具有Checked属性,当未选定单选钮时,其中包含的各控件的Enabled属性为False;而当本UserControl时启用其子控件。
  但其包含的每个子控件的Enabled状态可能不相同,要求能保持子控件的启用/禁用状态。
5、本RadioGroupBox的选定状态可以跟同级的其它RadioGroupBox和RadioButton协调,以便实现单选功能。
如下图所示:


------解决方案--------------------
RadioButton 的选中事件啊,再设置需要的控件的Enable属性的true或者false
------解决方案--------------------
C# code

partial class RadioGroupBox
    {
        /// <summary> 
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary> 
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Component Designer generated code

        /// <summary> 
        /// Required method for Designer support - do not modify 
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.groupBox1 = new System.Windows.Forms.GroupBox();
            this.radioButton1 = new System.Windows.Forms.RadioButton();
            this.groupBox1.SuspendLayout();
            this.SuspendLayout();
            // 
            // groupBox1
            // 
            this.groupBox1.Controls.Add(this.radioButton1);
            this.groupBox1.Dock = System.Windows.Forms.DockStyle.Fill;
            this.groupBox1.Location = new System.Drawing.Point(0, 0);
            this.groupBox1.Name = "groupBox1";
            this.groupBox1.Size = new System.Drawing.Size(220, 127);
            this.groupBox1.TabIndex = 0;
            this.groupBox1.TabStop = false;
            // 
            // radioButton1
            // 
            this.radioButton1.AutoSize = true;
            this.radioButton1.Location = new System.Drawing.Point(0, 1);
            this.radioButton1.Name = "radioButton1";
            this.radioButton1.Size = new System.Drawing.Size(95, 16);
            this.radioButton1.TabIndex = 0;
            this.radioButton1.TabStop = true;
            this.radioButton1.Text = "radioButton1";
            this.radioButton1.UseVisualStyleBackColor = true;
            this.radioButton1.CheckedChanged += new System.EventHandler(this.radioButton1_CheckedChanged);
            // 
            // RadioGroupBox
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.Controls.Add(this.groupBox1);
            this.Name = "RadioGroupBox";
            this.Size = new System.Drawing.Size(220, 127);
            this.groupBox1.ResumeLayout(false);
            this.groupBox1.PerformLayout();
            this.ResumeLayout(false);

        }

        #endregion

        private System.Windows.Forms.GroupBox groupBox1;
        private System.Windows.Forms.RadioButton radioButton1;



private string radioButtonText;

        public string RadioButtonText
        {
            get { return radioButtonText; }
            set { 
                radioButton1.Text = value;
            }
        }

        private void radioButton1_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButton1.Checked)
            {
                groupBox1.Enabled = true;
            }