winform关闭窗口时清除窗口内控件里的数据
如图两个窗口,菜品和Dish,点击菜品窗口的新建,弹出Dish窗口效果如附件2显示,在菜品窗口点击对应的菜信息再点击编辑,弹出的Dish窗口效果如附件三显示,遇到一个问题是点击编辑后,弹出窗口,关闭后再点击新建的话显示的样式还是跟上次点击编辑时弹出的窗口一样。这应该怎么解决啊,我尝试用的方法是点击Dish窗口的关闭按钮时将所有控件的内容清空,不过没有效果
菜品窗口代码
public string str;
public string[] alt;
public string id;
//点击新建按钮
private void toolStripButton1_Click(object sender, EventArgs e)
{
Dish dish = new Dish();
Sanck.con.Open();
using (SqlCommand cmd = new SqlCommand("select Max(编号) from 菜单表", Sanck.con))
{
if (Convert.ToString(cmd.ExecuteScalar()) == "")
{
dish.fm = this;
this.str = "D1001";
dish.Show();
}
else
{
string strID = Convert.ToString(cmd.ExecuteScalar());
dish.fm = this;
//自动生成添加纪录的编号
this.str = strID.Substring(0, 1) + Convert.ToString(Convert.ToUInt32(strID.Substring(1)) + 1);
dish.Show();
}
dish.button1.Enabled = true;
dish.button1.Visible = true;
dish.fm = null;
}
Sanck.con.Close();
}
//点击编辑按钮
private void toolStripButton2_Click(object sender, EventArgs e)
{
Dish dish = new Dish();
using (SqlCommand cmd = new SqlCommand("select * from 菜单表 where 编号='" + id + "'", Sanck.con))
{
Sanck.con.Open();
SqlDataReader dr = cmd.ExecuteReader();
if (dr.HasRows)
{
dish.fm = this;
dr.Read();
this.str = dr[0].ToString();
this.alt = new string[5] { dr[1].ToString(), dr[2].ToString(), dr[3].ToString(), dr[4].ToString(), dr[5].ToString() };
}
dr.Close();
Sanck.con.Close();
}
dish.button2.Enabled = true;
dish.button2.Visible = true;
dish.ShowDialog();
}
Dish窗口代码
public 菜品 fm = null;
private void Dish_Load(object sender, EventArgs e)
{
this.textBox1.Text = fm.str;
showAlt();
}
private void showAlt()
{
if (fm != null)
{
this.textBox1.Text = fm.str;
this.textBox2.Text = fm.alt[0].ToString();
this.textBox3.Text = fm.alt[2].ToString();
this.textBox4.Text = fm.alt[4].ToString();
= fm.alt[1].ToString();
= fm.alt[3].ToString();
}
}
private void Clear()
{
foreach (Control ctr in this.Controls)
{
if (ctr is TextBox)
{
ctr.Text = "";
}
else if (ctr is ComboBox)
{
ctr.Text = "";
}
}
}
private void Dish_FormClosing(object sender, FormClosedEventArgs e)
{
Clear();
this.Hide();
}