【问题标题】:Hide and Show Code doesnt work on other forms隐藏和显示代码不适用于其他表单
【发布时间】:2019-10-11 02:59:10
【问题描述】:

在表格 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会出现,form 3和form 4被隐藏。
  2. 按下表格 2 中的按钮(表格 2 将隐藏,表格 3 将显示)。
  3. 按下表格 3 中的按钮(表格 3 将隐藏,表格 4 将显示)。
  4. 按下表格 4 中的按钮(表格 4 将隐藏,表格 2 将显示,但之前写入的所有数据必须仍然存在)等等。

【问题讨论】:

    标签: c# forms show-hide


    【解决方案1】:

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

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

    【讨论】:

    • 试过了,但是“sa”有一个错误:名称“sa”在当前上下文中不存在。
    • 你创建了Form3Admin sa;类级别的属性并在 button1_Click 上分配它?
    • 抱歉,我现在有点迷路了。你能举个例子吗。我是 c# 新手,对术语感到困惑。
    • 改变这个'Form3Admin sa = new Form3Admin(position + ": " + fullname);' as 'sa = new Form3Admin(position + ":" + fullname);'在您的 button1 中单击。在 Form1 类级别中创建“Form3Admin sa;”。这样您就可以使用相同的实例来显示/隐藏。否则,您将创建多个 Form3Admin 对象。
    • 适用于表格 2,但仍不适用于其他表格。 attach 是错误图片的链接。 imgur.com/pfNAdX9 ...... 表格 1:imgur.com/VtxvcGM ....... imgur.com/X9k51Ns
    猜你喜欢
    • 2021-07-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-20
    • 2014-05-13
    • 2011-10-29
    • 2019-03-19
    相关资源
    最近更新 更多