【发布时间】:2012-12-23 04:26:39
【问题描述】:
using System;
using System.Drawing;
using System.Windows.Forms;
namespace Test
{
class mainWindow : Form
{
public mainWindow()
{
Label firstLabel = new label();
firstLabel.Text = "Hello";
this.Controls.Add(firstLabel);
Button firstButton = new Button();
firstButton.Text = "Click me";
firstButton.Click += firstButton_Click;
firstbutton.Left = 100;
this.Controls.Add(firstButton);
}
void firstButton_Click(object sender, EventArgs e)
{
firstlabel.Text = "Goodbye";
}
}
class XxX
{
static void Main()
{
mainWindow form = new mainWindow();
Application.Run(form);
}
}
}
【问题讨论】:
-
欢迎来到 StackOverflow!请在以后使用
{ }按钮格式化您的代码。 -
您的代码无法编译。即使您修复了某些变量的大小写,您也需要将
firstlabel声明为您的类的成员。 -
由于您是在运行时动态创建控件对象,因此请确保您在表单上指定控件的坐标