隐藏和显示代码不适用于其他形式

问题描述 投票:0回答:1

形式1

public void button1_Click(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection(@"Data Source=.\ALLENSQL;Initial Catalog=TITOMSLogin;Integrated Security=True");
            SqlDataAdapter sdf = new SqlDataAdapter("select usertype, fullname, position from login where username= '" + txtuser.Text + "' and password='" + txtpass.Text + "'", con);
            DataTable dt = new DataTable();
            sdf.Fill(dt);
            string fullname = dt.Rows[0]["FullName"].ToString();
            string position = dt.Rows[0]["Position"].ToString();
            if (dt.Rows.Count == 1)
             {
                 this.Hide();
                 if (dt.Rows[0]["Usertype"].ToString() == "Admin")
                 {
                    Form2Admin ss = new Form2Admin(position + ": " + fullname);
                    Form3Admin sa = new Form3Admin(position + ": " + fullname);
                    Form5 sw = new Form5(position + ": " + fullname);
                    ss.Show();
                    sa.Hide();
                    sw.Hide();
                 }

现在此代码无法以其他形式使用

        private void button2_Click(object sender, EventArgs e)
        {
            this.Hide();
            Form3Admin.Show();
        }

这是我要实现的方案:

  1. 在按下登录键后,将显示form2,隐藏3,而4隐藏。
  2. 按下表格2中的按钮(表格2将隐藏,表格3将显示。
  3. 按下表格3中的按钮(表格3将隐藏,表格4将显示。
  4. 按下表单4中的按钮(表单4将隐藏,表单2将显示,但之前写入的所有数据必须仍然存在),依此类推。

形式为1 public void button1_Click(对象发送者,EventArgs e){SqlConnection con =新的SqlConnection(@“ Data Source =。\ ALLENSQL; Initial Catalog = TITOMSLogin; Integrated Security = ...

c# forms show-hide
1个回答
0
投票

您必须使用相同的实际Form3Admin实例来显示它。将Form3Admin sa实例保留为类变量,并在要显示它时使用该实例。

© www.soinside.com 2019 - 2024. All rights reserved.