我是C#和OOP的新手,试图创建数学事实战舰棋盘游戏,并使用自定义类创建棋盘空间。我的想法是创建一个对象并将其放入一个面板中,然后在该面板中放入:
然后将这些片段加载到2D数组中。但是,当我将表单中的块添加到面板(面板)时,看不到它们。因此,我创建了一个更简单的测试,只是一个自定义类向表单添加标签。没运气。我添加了一些代码来动态创建一个标签,该标签告诉我“ this”是否有任何子代。除非我注释掉“添加”行,否则它说的是真的。如果有人能指出我做错了什么,或者至少给我提供了一个在线解释的链接,我将非常感激。我搜寻了。我正在使用Visual Studio Express2013。Intellisense表示一切正常。谢谢
namespace testing_custom_class
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
Class1 myLabel = new Class1();
this.Controls.Add(myLabel);
myLabel.Show();
Label childrenTest = new Label();
childrenTest.Text = this.HasChildren.ToString();
this.Controls.Add(childrenTest);
}
}
}
//and from the class1.cs
...
using System.Windows.Forms;
namespace testing_custom_class
{
class Class1 : System.Windows.Forms.Control
{
public Class1()
{
Label label1 = new Label();
label1.Text = "Hello World!";
}
}
}
找到了解决方案!我需要在自定义类中将标签添加到“ this”中。我也变了
class Class1 : System.Windows.Forms.Control
到
class Class1 : System.Windows.Forms.Label
然后,我添加了以下行:
this.Controls.Add(label1);
在自定义Class1的末尾。谢谢大家!
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句