使用Microsoft Visual Studio。 我有一个带有Radiobuttons和标签的表单,如何将其中一个指定给一个对象? 我有一个类:
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)
};
错误说
字段初始值设定项不能引用字段,方法或属性 static'myprogram.Form1.RadioButton1'
答案 0 :(得分:0)
在上课前公开播放:
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)
};
}