在运行时向表单添加 C# 标签 [英] Adding C# labels to a form at Runtime

查看:37
本文介绍了在运行时向表单添加 C# 标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试用 C# 制作一个简单的基于文本的游戏.我想如何实现这一点是通过向表单添加标签(而不是使用命令提示符).我在将它们添加到屏幕时遇到了一些问题.Visual Studio 给出了一个未指明的错误(只是说我有一个未处理的异常):

<块引用>

未将对象引用设置为对象的实例

当我尝试使用数组用这些标签填充屏幕时.代码:

private void Main_Game_Load(object sender, EventArgs e){标签 [] 敌人 = 新标签 [20];标签 [] 弹丸 = 新标签 [5];Font font = new Font(new System.Drawing.FontFamily("Microsoft Sans Serif"), 12);随机 rand = new Random();Point point = new Point(rand.Next(500), rand.Next(500));for (int i = 0; i 

我想知道问题可能藏在哪里?我已经用谷歌搜索了它,我的代码似乎应该可以工作(除了现在点不会在尝试填充时随机化).

解决方案

这行代码创建了一个空数组(即每个元素都没有引用)来存储最多 20 个标签:

Label[] Enemies = new Label[20];

您必须显式地在数组中创建每个标签:

for (int i = 0; i 

来自 单维数组(C# 编程指南):

SomeType[] array4 = new SomeType[10];

<块引用>

此语句的结果取决于 SomeType 是否为值类型或引用类型.如果是值类型,则语句创建一个包含 10 个元素的数组,每个元素的类型为 SomeType.如果SomeType 是一个引用类型,该语句创建了一个 10 的数组元素,每个元素都初始化为空引用.

I'm trying to make a simple text-based game in C#. How I want to achieve this is by adding labels to a form (instead of using the command prompt). I'm having some trouble adding them to the screen. Visual studio is giving an unspecified error (Only saying I have an unhandled exception):

Object reference not set to an instance of an object

when I try to use an array to populate the screen with these labels. The code:

private void Main_Game_Load(object sender, EventArgs e)
{
    Label[] Enemies = new Label[20];
    Label[] Projectile = new Label[5];
    Font font = new Font(new System.Drawing.FontFamily("Microsoft Sans Serif"), 12);
    Random rand = new Random();
    Point point = new Point(rand.Next(500), rand.Next(500));

    for (int i = 0; i < Enemies.Length; i++)
    {
        Enemies[i].Text = "E";
        Enemies[i].Font = font;
        Enemies[i].BackColor = ColorTranslator.FromHtml("#000000");
        Enemies[i].Location = point;
        Enemies[i].Size = new Size(12, 12);
        Enemies[i].Name = "Enemy"+i.ToString();
        this.Controls.Add(Enemies[i]);
    }
}

I am wondering where the issue might be hiding at? I've googled it and my code seems like it should work (aside from right now point doesn't randomize on attempt to populate).

解决方案

This line of code creates an empty array (i.e. each element referencing to nothing) to store up to 20 labels:

Label[] Enemies = new Label[20];

You must create each label in the array explicitly:

for (int i = 0; i < Enemies.Length; i++)
{
    //creates a new label and stores a reference to it into i element of the array 
    Enemies[i] = new Label(); 
    //...
}

From Single-Dimensional Arrays (C# Programming Guide):

SomeType[] array4 = new SomeType[10];

The result of this statement depends on whether SomeType is a value type or a reference type. If it is a value type, the statement creates an array of 10 elements, each of which has the type SomeType. If SomeType is a reference type, the statement creates an array of 10 elements, each of which is initialized to a null reference.

这篇关于在运行时向表单添加 C# 标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆