面板单选按钮选择1并更改其他面板单选按钮? C#

时间:2016-06-18 15:46:33

标签: c# .net

我想制作交通应用程序,因为我必须在充当信号的单选按钮之间切换,实际上我有4个面板,每个面板有3个单选按钮红色,绿色,橙色

问题:当我在任何面板中选择红色或绿色时,其他单选按钮必须根据要求将1设置为绿色或红色

请记住,他们将同时只有1个绿色和所有其他红色

我试图在检查更改事件中实现代码,但如果我在每个按钮上应用代码,这将变为递归,因此创建问题我也尝试过但不适合解决方案

每个单选按钮上有1个事件

   private void R_lane3Red_CheckedChanged(object sender, EventArgs e)
    {
      //  var checkedButton2 = panel2.Controls.OfType<RadioButton>()
      //                            .FirstOrDefault(r => r.Checked);
      //  var checkedButton1 = panel1.Controls.OfType<RadioButton>()
      //                            .FirstOrDefault(r => r.Checked);
      //  var checkedButton3 = panell3.Controls.OfType<RadioButton>()
      //                            .FirstOrDefault(r => r.Checked);
      //  var checkedButton4 = panell4.Controls.OfType<RadioButton>()
      //                            .FirstOrDefault(r => r.Checked);

      //if  (checkedButton2.Name.Contains("Green")){
      //    R_lane1Red.Checked = true;
      //    R_lane4Red.Checked = true;
      //    R_lane3Red.Checked = true;

      //}

      //if (checkedButton1.Name.Contains("Green"))
      //{
      //    R_lane2Red.Checked = true;
      //    R_lane4Red.Checked = true;
      //    R_lane3Red.Checked = true;

      //}
      //if (checkedButton3.Name.Contains("Green"))
      //{
      //    R_lane2Red.Checked = true;
      //    R_lane4Red.Checked = true;
      //    R_lane1Red.Checked = true;

      //}
      //if (checkedButton4.Name.Contains("Green"))
      //{
      //    R_lane2Red.Checked = true;
      //    R_lane3Red.Checked = true;
      //    R_lane1Red.Checked = true;

      //}

下面是面板和单选按钮的图像

image of UI

1 个答案:

答案 0 :(得分:0)

我建议您创建交通灯作为控件。然后,您可以将规则构建到交通灯控制中,以了解在设置特定灯光时它应该如何表现。以下内容可能对您有用:

// The event that fires when a light changed, sends notification to the main form.
public delegate void LightChangedHandler(object sender, Lights light);

// Simple enum to pass the state of the TrafficLight around.
public enum Lights
{
    Green,
    Orange,
    Red
}

public partial class TrafficLights : UserControl
{
    public event LightChangedHandler LightChanged;

    protected virtual void OnChanged(Lights light) 
    {
        if (LightChanged != null)
        {
            LightChanged(this, light);
        }
    }

    private bool _changing = false;

    public TrafficLights()
    {
        InitializeComponent();
    }

    // The business rules for the Traffic Light states.
    public void SetLight(Lights light)
    {
        if (_changing)
        {
            return;
        }

        _changing = true;

        switch (light)
        {
            case Lights.Green:
                radioGreen.Checked = true;
                radioOrange.Checked = false;
                radioRed.Checked = false;
                break;

            case Lights.Orange:
                radioOrange.Checked = true;
                radioGreen.Checked = false;
                radioRed.Checked = false;
                break;

            case Lights.Red:
                radioRed.Checked = true;
                radioGreen.Checked = false;
                radioOrange.Checked = false;
                break;
        }

        OnChanged(light);

        _changing = false;
    }

    private void radioGreen_CheckedChanged(object sender, EventArgs e)
    {
        SetLight(Lights.Green);
    }

    private void radioOrange_CheckedChanged(object sender, EventArgs e)
    {
        SetLight(Lights.Orange);
    }

    private void radioRed_CheckedChanged(object sender, EventArgs e)
    {
        SetLight(Lights.Red);
    }
}

然后,您可以根据需要为主表单添加任意数量的TrafficLights。当您的任何交通信号灯发生变化时,它会向主窗体发出一个事件。然后,您可以处理此事件并应用交通灯组应如何表现的业务规则。像这样:

    private readonly List<TrafficLights> _trafficLights = new List<TrafficLights>();

    private bool _changing = false;

    public Form1()
    {
        InitializeComponent();

        // Add each TrafficLight control on the Form to a list so we can spin through them later and set their state.
        _trafficLights.Add(trafficLights1);
        _trafficLights.Add(trafficLights2);
        _trafficLights.Add(trafficLights3);
        _trafficLights.Add(trafficLights4);

        // Add the handler for the LightChanged event of each TrafficLight.
        this.trafficLights1.LightChanged += TrafficLightsOnLightChanged;
        this.trafficLights2.LightChanged += TrafficLightsOnLightChanged;
        this.trafficLights3.LightChanged += TrafficLightsOnLightChanged;
        this.trafficLights4.LightChanged += TrafficLightsOnLightChanged;
    }

    private void TrafficLightsOnLightChanged(object sender, Lights light)
    {
        // Only allow the first LightChanged event to fire off to avoid the recursive events.
        if (_changing)
        {
            return;
        }

        _changing = true;

        TrafficLights currentLight = sender as TrafficLights;

        // Set the Light state of all other TrafficLights.
        foreach (TrafficLights trafficLight in _trafficLights
            .Where(trafficLight => trafficLight != currentLight))
        {
            LightChanged(trafficLight, light);
        }

        _changing = false;
    }

    // The business rules for setting the other TrafficLight states.
    private static void LightChanged(TrafficLights trafficLight, Lights light)
    {
        // You will need to adjust the following to suit your specific business rules.
        switch (light)
        {
            case Lights.Green:
                trafficLight.SetLight(Lights.Red);
                break;

            case Lights.Orange:
                trafficLight.SetLight(Lights.Orange);
                break;

            case Lights.Red:
                trafficLight.SetLight(Lights.Green);
                break;
        }
    }

添加bool _changing变量可以让您以递归方式停止事件。触发第一个事件时,将_changing设置为true。这将阻止进一步的事件被触发,直到你完成处理第一个事件后将其设置为假。