我真的是 C# 的新手,我一直在想为什么在没有事件处理程序帮助的情况下将标签文本设置为不同的值时会发生变化?
这是我的代码:
class Main_Program : Form
{
Button btnAttack = new Button();
Label[] lblEnemyInfo = new Label[4];
public Main_Program()
{
btnAttack.Text = "ATTACK";
btnAttack.Location = new Point(350, 450);
btnAttack.Width = 150;
btnAttack.Height = 50;
btnAttack.FlatStyle = FlatStyle.Popup;
btnAttack.MouseClick += new MouseEventHandler(btnAttack_MouseClick);
for (short i = 0; i < 4; i++)
lblEnemyInfo[i] = new Label();
string enemyHealth = "Health : " + enemy.Health; //I'm going set the values on label 2
lblEnemyInfo[1].Text = enemyHealth;
lblEnemyInfo[1].Font = new Font("Segoi UI", 12, FontStyle.Italic);
lblEnemyInfo[1].Location = new Point(500, 50);
for (short i = 0; i < 4; i++)
Controls.Add(lblEnemyInfo[i]);
}
private void btnAttack_MouseClick(Object sender, EventArgs e)
{
short totalDamage = totalDamageDeal(enemy.Armor, player.Attack);
string log = "You Attack the enemy, you deal " + totalDamage + " damage";
enemy.Health -= totalDamage;
string result = "Health : " + enemy.Health;
lblEnemyInfo[1].Text = result;
lblEnemyInfo[1].TextChanged += new EventHandler(lblEnemyInfo_TextChanged); //The label's text changed even without the eventhandler
}
private void lblEnemyInfo_TextChanged (object sender, EventArgs e)
{
//i don't know what statements to put here
}
}