| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 642 人关注过本帖
标题:winform关闭窗口时清除窗口内控件里的数据
只看楼主 加入收藏
Sephirose
Rank: 1
来 自:四川-乐山
等 级:新手上路
帖 子:51
专家分:0
注 册:2013-3-26
结帖率:55.56%
收藏
已结贴  问题点数:10 回复次数:1 
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();
        }
搜索更多相关主题的帖子: 新建 public 编辑 
2013-04-20 18:06
yhlvht
Rank: 13Rank: 13Rank: 13Rank: 13
等 级:贵宾
威 望:36
帖 子:707
专家分:4405
注 册:2011-9-30
收藏
得分:10 
方法有很多种,例如,Dish类里面加个全局变量public string state = "";
菜品类里面,新建的时候dish.state = "add";
编辑的时候,dish.state = "edit";

Dish_Load事件里面
this.textBox1.Text = fm.str;
if(dish.state == "edit")
{
    showAlt();
}
2013-04-20 19:04
快速回复:winform关闭窗口时清除窗口内控件里的数据
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.025650 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved