0

我有三个单选按钮。他们更改事件背后的代码如下:

    private void uxRajRadioButton_CheckedChanged(object sender, EventArgs e) 
    {
        if (uxRajRadioButton.Checked == true)
        {
            uxPersonBettingLabel.Text = "Raj";
            GuySelected = 0;
            uxBetNumericUpDown.Maximum =  Guys[GuySelected].Cash;
        }
    }
    private void uxPaulRadioButton_CheckedChanged(object sender, EventArgs e) 
    {
        if (uxPaulRadioButton.Checked == true) 
        {
            uxPersonBettingLabel.Text = "Paul";
            GuySelected = 1;
            uxBetNumericUpDown.Maximum = Guys[GuySelected].Cash;
        }
    }
    private void uxMikeRadioButton_CheckedChanged(object sender, EventArgs e) 
    {
        if (uxMikeRadioButton.Checked == true)
        {
            uxPersonBettingLabel.Text = "Mike";
            GuySelected = 2;
            uxBetNumericUpDown.Maximum = Guys[GuySelected].Cash;
        }
    }

只有三个单选按钮这是可以接受的,但如果我说 7 个单选按钮,每个按钮在其更改事件后面有 20 行代码,那么这将是很多(不必要的?)代码。我试图缩短上述内容并最终得到以下内容。它是否正确?

     private void uxRajRadioButton_CheckedChanged(object sender, EventArgs e) 
    { 
         radioButtonCode(this.uxRajRadioButton, 0);
     }
    private void uxPaulRadioButton_CheckedChanged(object sender, EventArgs e) 
    {
        radioButtonCode(this.uxPaulRadioButton,1);
    }
    private void uxMikeRadioButton_CheckedChanged(object sender, EventArgs e) 
    {
        radioButtonCode(this.uxMikeRadioButton, 2);
    }

    int GuySelected=0;
    public void radioButtonCode(RadioButton myRadio, int mybettorIndex)
    {
        if (myRadio.Checked == true)
        {
            GuySelected = mybettorIndex;
            uxPersonBettingLabel.Text = Guys[GuySelected].Name;
            uxBetNumericUpDown.Maximum = Guys[GuySelected].Cash;
        }
    }
4

5 回答 5

1

您的第二个代码示例将共性提取到一个函数中,看起来就像我进行此重构的方式。

UpdateUserInfo除了名字(可能会稍微好一点)之外,它几乎和你能做的一样好。

于 2012-08-19T18:47:47.140 回答
1

有一个更好的办法。声明一个单选按钮数组并在运行时将它们绑定到上述事件。此绑定代码不会在设计器页面内。这将导致单个ArrayRadio_checkedChange事件。在这种方法中,您可以使用 sender 属性找出正确的单选按钮的索引并采取相应的措施。

于 2012-08-19T18:51:10.943 回答
1

您能否为所有单选按钮设置相同的事件处理程序?就像是

private void uxRadioButton_CheckedChanged(object sender, EventArgs e) 
{ 
     radioButtonCode((RadioButton)sender);
}

public void radioButtonCode(RadioButton myRadio)
{
    if (myRadio.Checked == true)
    {
        int guySelected = getGuySelectedIndex(myRadio);
        uxPersonBettingLabel.Text = Guys[guySelected].Name;
        uxBetNumericUpDown.Maximum = Guys[guySelected].Cash;
    }
}

public int getGuySelectedIndex(RadioButton myRadio)
{
    int index = 0;
    if (myRadio == this.uxRajRadioButton) index = 0;
    else if (myRadio == this.uxPaulRadioButton) index = 1;
    else if (myRadio == this.uxMikeRadioButton) index = 2;
    return index;
}
于 2012-08-19T18:58:35.073 回答
0

您可以使用

uxRajRadioButton.CheckedChanged += new EventHandler(rb_CheckedChanged);
uxPaulRadioButton.CheckedChanged += new EventHandler(rb_CheckedChanged);
...

uxRajRadioButton.Tag =new KeyValuePair<string,int>("Raj",0);
uxPaulRadioButton.Tag =new KeyValuePair<string,int>("Paul",1);
....

private void rb_CheckedChanged(object sender, EventArgs e) 
{
    if(!(sender is RadioButton))
         return;
       RadioButton myRadio= sender as RadioButton;
      if (myRadio.Checked == true)
      {
        myRadio.Text = (myRadio.Tag as KeyValuePair<string,int>).Key;
        GuySelected = (myRadio.Tag as KeyValuePair<string,int>).Value;
        uxBetNumericUpDown.Maximum =  Guys[GuySelected].Cash;
      }
}
于 2012-08-19T18:54:56.260 回答
0

我通常做的是将索引放在标签属性中。这样您就可以将此事件绑定到每个 RadioButton

public void uxRadioButton_CheckedChanged(object sender, EventArgs e)
{
    RadioButton myRadio = (RadioButton) sender;
    if (myRadio.Checked)
    {
        GuySelected = (int)myRadio.Tag;
        uxPersonBettingLabel.Text = Guys[GuySelected].Name;
        uxBetNumericUpDown.Maximum = Guys[GuySelected].Cash;
    }
}
于 2012-08-19T18:55:48.503 回答