在 C# 中将 RadioButton 和标签作为参数传递

Passing RadioButton and label as Parameter in C#

使用微软 Visual Studio。 我有一个带有单选按钮和标签的表单,如何将其中一个分配给对象? 我有一个 class 比如:

class Player
{
    private string name;
    private System.Windows.Forms.RadioButton myRadioButton;
    private System.Windows.Forms.Label myLabel;
    public Player(string name, System.Windows.Forms.RadioButton MyRadioButton, System.Windows.Forms.Label MyLabel)
    {
        this.name = name;
        this.myRadioButton = MyRadioButton;
        this.myLabel = MyLabel;
    }
}

我在主窗体中这样做但是错了:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace myprogram
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            }

        Player[] players =
        {
            new player ("Mike", RadioButton1, label1),
            new player("Bob", RadioButton2, label2)
        };

错误显示

field initializer can not reference the field, method or property not static 'myprogram.Form1.RadioButton1'

将public放在class之前玩家:

public class Player
{
  private string _name;
  private System.Windows.Forms.RadioButton _myRadioButton;
  private System.Windows.Forms.Label _myLabel;
  public Player(string name, System.Windows.Forms.RadioButton myRadioButton, 
                             System.Windows.Forms.Label myLabel)
  {
    _name = name;
    _myRadioButton = myRadioButton;
    _myLabel = myLabel;
  }
}

和:

private void Form1_Load(object sender, EventArgs e)
    {


//Assuming that you have added two radio buttons and two labels to the form      
//from designer, then the default generated name for the control is     
//radiobutton1 not Radiobutton1 and so on

    Player[] players =
    {
        new Player ("Mike", radioButton1, label1),
        new Player("Bob", radioButton2, label2)
    };
}