我正在编写一个程序,它创建了用于在表单上编写文本的标签,但是在以某种方式设法销毁一个重要的类之后,我又从头开始......我似乎无法让它再打印标签了,但是根本就不出现。
public partial class Form1 : Form
{
static Form FormX = new Form();
public Form1()
{
Shown += new EventHandler(FormX_Shown);
InitializeComponent();
}
public void FormX_Shown(object sender, EventArgs e)
{
WriteTextOnScreen("Hello!");
}
public void WriteTextOnScreen(string text)
{
Label tempLabel = new Label();
tempLabel.Text = text;
tempLabel.Name = "";
tempLabel.Location = new Point(10, 10);
FormX.Controls.Add(tempLabel);
}
}
我不确定问题是什么,但此刻它变得非常烦人,因为我不够聪明,无法自行修复:-P
答案 0 :(得分:1)
您将标签添加到FormX的控件集合中,并且永远不会显示此表单 我认为你应该将标签添加到你自己的实例(这个)
public void WriteTextOnScreen(string text)
{
Label tempLabel = new Label();
tempLabel.Text = text;
tempLabel.Name = "";
tempLabel.Location = new Point(10, 10);
this.Controls.Add(tempLabel);
}
然而,这仅适用于一个标签,因为位置始终相同(10,10)。如果您多次调用此方法,最后一个标签将被绘制在上一个标签之上,您将只看到最后一个标签。