0

我是 C# 的初学者,我有一个家庭作业。在表单中,我有一个带有 5 个单选按钮和数字向上/向下数天的组合框。当我更改数字时。up-down,backcolor 应该改变它之前选择的颜色。我只能使用开关盒。我管理了数字。上下,标签随之变化。但是我不能插入开关盒来改变颜色。实际上我不能命名开关。

 private void Form1_Load(object sender, EventArgs e)
        {
            label1.Text = "Pazartesi";


        }

        private void numericUpDown1_ValueChanged(object sender, EventArgs e)
        {
            byte sayi = Convert.ToByte(numericUpDown1.Value);


            switch (sayi)
            {
                case 1:
                    label1.Text="Pazartesi";
                    switch ( )
                    {
                        case renkbtn_1.Checked:
                            this.BackColor = Color.Red;
                            break;
                        case renkbtn_2.Checked:
                            this.BackColor = Color.White;
                            break;
                        case renkbtn_3.Checked:
                            this.BackColor = Color.Yellow;
                            break;
                        case renkbtn_4.Checked:
                            this.BackColor = Color.Blue;
                            break;
                        case renkbtn_5.Checked:
                            this.BackColor = Color.Green;
                            break;
                    }
                    break;
                case 2:
                    label1.Text="Salı";
                    break;
                case 3:
                    label1.Text = "Çarşamba";
                    break;
                case 4:
                    label1.Text = "Perşembe";
                    break;
                case 5:
                    label1.Text = "Cuma";
                    break;
                case 6:
                    label1.Text = "Cumartesi";
                    break;
                case 7:
                    label1.Text = "Pazar";
                    break;
4

2 回答 2

2

switch 语句不是这样工作的。

里面的大括号switch ( )应该是一个产生某个值的表达式(它不是一个“名称”,但可能是一个变量)。这些cases 必须具有(编译时)常量,这些常量会根据 switch 表达式中的值进行检查。

您将不得不将其重写为一堆if语句:

  if (renkbtn_1.Checked)
      this.BackColor = Color.Red;
  else if (renkbtn_2.Checked)
      this.BackColor = Color.White;
  else if (renkbtn_3.Checked)
      this.BackColor = Color.Yellow;
  else if (renkbtn_4.Checked)
      this.BackColor = Color.Blue;
  else if (renkbtn_5.Checked)
      this.BackColor = Color.Green;
于 2012-10-31T15:48:25.773 回答
0

如果我理解了这个问题,你改变了上下值,你想将背景色设置为你选择的单选按钮的颜色。正确的?

如果是这种情况,您必须检查哪个单选按钮,然后正确设置颜色。

然后您可以遍历您的 groupbox 的控件以获取选中的收音机:

foreach (Control c in groupBox1.Controls)
            {
                if (c.GetType() == typeof(RadioButton))
                {
                    RadioButton rb = c as RadioButton;
                    if (rb.Checked)
                    {
                       //here you can either store the checked radio button in a variable to further check the color you need to set, or do the logic here.
                    }
                }
            }
}

你的整个代码可能是这样的:

private void Form1_Load(object sender, EventArgs e) { label1.Text = "Pazartesi";

    }

    private void numericUpDown1_ValueChanged(object sender, EventArgs e)
    {
        byte sayi = Convert.ToByte(numericUpDown1.Value);


        switch (sayi)
        {
            case 1:
                label1.Text="Pazartesi";
                break;
            case 2:
                label1.Text="Salı";
                break;
            case 3:
                label1.Text = "Çarşamba";
                break;
            case 4:
                label1.Text = "Perşembe";
                break;
            case 5:
                label1.Text = "Cuma";
                break;
            case 6:
                label1.Text = "Cumartesi";
                break;
            case 7:
                label1.Text = "Pazar";
                break;
        }
        RadioButton selectedRadio = GetSelectedRadioButton();

        switch(selectedRadio.Name)
        { 
           case rankbtn_1.Name:
                this.BackColor = Color.Red;
                break;
           case rankbtn_2.Name:
                this.BackColor = Color.White;
                break;

           //Repeat for the other buttons.
        }

   }

   private RadioButton GetSelectedRadioButton()
   {
                foreach (Control c in groupBox1.Controls)
            {
                if (c.GetType() == typeof(RadioButton))
                {
                    RadioButton rb = c as RadioButton;
                    if (rb.Checked)
                    {
                       return rb;
                    }
                }
            }
   }           
于 2012-10-31T15:40:04.940 回答