[已解决]动态创建窗体?
我有一个应用程序,在主窗体上单击菜单,动态创建了一个窗体form2,form2窗体上创建有一个按钮,单击form2窗体上的按钮关闭form2窗体。运行时第一次正常,不关闭主窗体,再次运行时出错:代码:
public Form form2 = new Form();
//单击主窗体上菜单事件
private void addNewProductionOrderToolStripMenuItem_Click(object sender, EventArgs e)
{
//添加窗体
form2.Size = new Size(400,330);
form2.BackColor = Color.SkyBlue;
form2.Text = "Add Production Order!";
form2.ControlBox = true;
//在form2窗体上添加按钮
Button cancelButton = new Button();
cancelButton.Text = "Cancel";
cancelButton.Size = new Size(100,30);
cancelButton.Location = new Point(200,237);
form2.Controls.Add(cancelButton);
cancelButton.Click+=new EventHandler(cancelButton_Click);
form2.Show();
}
//单击form2窗体上按钮事件
private void cancelButton_Click(object sender, EventArgs e)
{
form2.Close();
}
[[it] 本帖最后由 wwx024 于 2008-10-29 10:03 编辑 [/it]]