C# 从其他类操纵窗体控件属性?
我试图通过我的一个类操纵主窗体中控件的属性 基本上,我正在尝试更新标签,我已将其设置为public,如下所示:C# 从其他类操纵窗体控件属性?,c#,C#,我试图通过我的一个类操纵主窗体中控件的属性 基本上,我正在尝试更新标签,我已将其设置为public,如下所示: public Label DicerollLabel; 我指的是我班上的主要形式,如下所示: private Form1 _mainForm; _mainForm.DicerollLabel.Text = "Hello World!"; 当我尝试访问标签并将其设置为如下值时: private Form1 _mainForm; _mainForm.DicerollLabel.
public Label DicerollLabel;
我指的是我班上的主要形式,如下所示:
private Form1 _mainForm;
_mainForm.DicerollLabel.Text = "Hello World!";
当我尝试访问标签并将其设置为如下值时:
private Form1 _mainForm;
_mainForm.DicerollLabel.Text = "Hello World!";
我得到以下错误:
发生“System.NullReferenceException”类型的未处理异常。
我的两个文件的全部代码如下:
主要形式:
namespace BettingGame
{
public partial class Form1 : Form
{
private Player _playerOne;
private Player _playerTwo;
private DiceRoller _diceRoller;
private decimal _prizePool;
private Random _random;
public int ProgressBar
{
get {return progressBar1.Value;}
set { progressBar1.Value = value; }
}
public Label DicerollLabel;
public Form1()
{
InitializeComponent();
_playerOne = new Player() {PlayerName = "x", PlayerFunds = 100};
_playerTwo = new Player() {PlayerName = "x", PlayerFunds = 100};
_diceRoller = new DiceRoller();
_random = new Random();
playerOneFundsLabel.Text = "Funds: " + _playerOne.PlayerFunds.ToString(CultureInfo.CurrentCulture) + "$";
playerTwoFundsLabel.Text = "Funds: " + _playerTwo.PlayerFunds.ToString(CultureInfo.CurrentCulture) + "$";
PrizeAmountLabel.Text = "";
diceRollLabel.Text = "";
}
private void button1_Click(object sender, EventArgs e)
{
_playerOne.PlayerBetAmount = (decimal) playerOneBet.Value;
_playerTwo.PlayerBetAmount = (decimal) playerTwoBet.Value;
if (!(_playerOne.PlayerBetAmount <= 0) || !(_playerTwo.PlayerBetAmount <= 0)
&& (_playerOne.PlayerBetAmount > 0) && (_playerTwo.PlayerBetAmount > 0))
{
_prizePool = _playerOne.PlayerBet() + _playerTwo.PlayerBet();
PrizeAmountLabel.Text = _prizePool.ToString(CultureInfo.CurrentCulture);
FormUpdate();
}
else
{
MessageBox.Show("Invalid bets! Bet amount must be greater than 0!");
}
}
private void FormUpdate()
{
playerOneFundsLabel.Text = "Funds: " + _playerOne.PlayerFunds.ToString(CultureInfo.CurrentCulture) + "$";
playerTwoFundsLabel.Text = "Funds: " + _playerTwo.PlayerFunds.ToString(CultureInfo.CurrentCulture) + "$";
}
private void button2_Click(object sender, EventArgs e)
{
//for (int i = 0; i < 45; i++)
//{
// int value = _random.Next(1, 50);
// diceRollLabel.Text = value.ToString();
// diceRollLabel.Update();
// Thread.Sleep(50);
//}
_diceRoller.RollDice();
}
}
}
我做错了什么?请注意,我只是在我的编程第二周,所以我仍然在学习 更改您的
类以定义以下构造函数:
public DiceRoller(Form1 host) {
_mainForm = host;
}
然后在Form1中创建DiceRoller的实例:
private DiceRoller _diceRoller = new DiceRoller(this);
从长远来看,这是一个非常糟糕的设计。这绝对是我希望从一个新的程序员那里得到的代码——所以别难过。你做得很好
在将来,试着考虑可重用性。通过使掷骰子依赖于Form1(以及Form1中的特定控件),您将在以后面临两个挑战:
1.未经修改,您不能在另一个项目中(甚至可能在同一个项目中)使用Dicellorer。
2.如果您更改DiceRoller依赖的任何控件,您也必须更改DiceRoller
我会让你考虑如何避免这些问题。我相信,如果您需要帮助,您将发布另一个问题。:) 您需要创建对Form1的引用。这不是命名为Form1,但在Form1中,您可以使用关键字“This”引用它,并可以将其传递到Dicellorer类的构造函数中
class DiceRoller
{
private Random _random = new Random();
private Form1 _mainForm;
public DiceRoller(Form1 f)
{
_mainForm = f;
}
public void RollDice()
{
_mainForm.DicerollLabel.Text = "Hello World!";
}
}
然后在Form1的构造函数中调用:
public Form1()
{
InitializeComponent();
_playerOne = new Player() {PlayerName = "x", PlayerFunds = 100};
_playerTwo = new Player() {PlayerName = "x", PlayerFunds = 100};
_diceRoller = new DiceRoller(this);
....
这些是使代码工作所需的最小更改。但是,您可能会考虑其他更改,例如在标签中传递而不是表单。在第二类中, <代码>我试图创建一个构造函数并将Form1传递给类,但没有成功。既然我只想引用主表单,我该怎么做呢?我不想创建它的新实例。为什么要切换到VB?是VB吗?我不熟悉那种语言:p