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

不同groupbox中的radioButton互斥
假设有2个groupbox
每个groupbox里有2个radiobutton
怎么实现不同4个radiobutton均互斥?


------解决方案--------------------
每个radiobutton的.

CheckChanged事件里面进行判断.

比如:
C# code

using System.IO;
using System.Drawing.Imaging;
using System.Drawing;

namespace ControlBean
{
    public class GetStream : MarshalByRefObject, IGetIMG
    {
        public MemoryStream GetImageStream()
        {
            CatScreen screen = new CatScreen();
            Image img = screen.GetScreenImage();
            MemoryStream ms = new MemoryStream();
            img.Save(ms, ImageFormat.Jpeg);
            return ms;
        }
    }
}

------解决方案--------------------
探讨
引用:
4个radiobutton的Parent=this,然后BringToFront()置前.
也就是将4个radiobutton放在同一父容器里面.


互斥确实实现了
但是为什么radiobutton的位置都改变了
不在groupbox里了
而是都跑到外边了

------解决方案--------------------
不要修改布局
C# code
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication71
{
    public partial class Form1 : Form
    {
        List<RadioButton> CheckRadioButtons = new List<RadioButton>();

        public Form1()
        {
            InitializeComponent();

            radioButton1.CheckedChanged += new EventHandler(CheckedChanged);
            radioButton2.CheckedChanged += new EventHandler(CheckedChanged);
            radioButton3.CheckedChanged += new EventHandler(CheckedChanged);
            radioButton4.CheckedChanged += new EventHandler(CheckedChanged);
        }

        void CheckedChanged(object sender, EventArgs e)
        {
            RadioButton RB = (RadioButton)sender;

            if (!RB.ContainsFocus)
                return;
            
            if (!CheckRadioButtons.Contains(RB))
                CheckRadioButtons.Add(RB);

            foreach (RadioButton RB2 in CheckRadioButtons)
                if (RB2 != RB)
                    RB2.Checked = false;
        }
    }
}