繁体   English   中英

Visual Studio 中的设计器不会将 label 文本设置为值

[英]The designer in Visual Studio will not set label text to value

我的程序不会显示生命值、经验值、等级和金币。 我有标签显示每一个的数量,如此处所示。

public Label lblHitPoints;
public Label lblExperience;
public Label lblLevel;
public Label lblGold;

然后我用这段代码声明了每一个的数量:

{
    CurrentHitPoints = 10,
    MaximumHitPoints = 10,
    Gold = 20,
    ExperiencePoints = 0,
    Level = 1,
};

lblHitPoints.Text = _player.CurrentHitPoints.ToString();
lblGold.Text = _player.Gold.ToString();
lblExperience.Text = _player.ExperiencePoints.ToString();
lblLevel.Text = _player.Level.ToString();

即使这样,这仍然不会显示在设计师身上。

我尝试了不同的格式化方式,但它们似乎不适合,我想不出其他任何东西。

如果您在Form中自己声明 label 变量而不使用设计器拖放功能,则需要通过form_name.Controls.Add(label_Name)添加这些标签并手动设置它们在表单上的位置。 这是因为当使用工具箱在设计器上添加标签时,它会自动生成所需的代码,并将其显示在表单上。

播放器 class:

public class Player
{
    public int CurrentHitPoints;
    public int MaximumHitPoints;
    public int Gold;
    public int ExperiencePoints;
    public int Level;
}

显示标签的表格:

public partial class Form1 : Form
{
    public Label lblHitPoints = new Label();
    public Label lblExperience = new Label();
    public Label lblLevel = new Label();
    public Label lblGold = new Label();

    public Form1()
    {
        InitializeComponent();

        this.Controls.Add(lblExperience);
        this.Controls.Add(lblGold);
        this.Controls.Add(lblHitPoints);
        this.Controls.Add(lblLevel);

        lblExperience.Top = 0;
        lblGold.Top = 20;
        lblHitPoints.Top = 40;
        lblLevel.Top = 60;

        Player _player = new Player{ 
            CurrentHitPoints = 10,
            MaximumHitPoints = 10,
            Gold = 20,
            ExperiencePoints = 0,
            Level = 1
         };

        lblHitPoints.Text = _player.CurrentHitPoints.ToString();
        lblGold.Text = _player.Gold.ToString();
        lblExperience.Text = _player.ExperiencePoints.ToString();
        lblLevel.Text = _player.Level.ToString();
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM