我想做一个简单的winform登陆程序,我建立了两个form 分别是:Form1,login;其中Form1是主窗口,
我希望程序运行的时候只显示login窗口,登陆后再才将login关点,Form1窗口打开,请问我应该怎么做,谢谢
我现在是这样做的
login f1=new login();
f1.Show();
f1.TopMost=true; Form1 f2=new Form1();
f2.Hide();但是Form1主窗口怎么不能隐藏掉呢,郁闷,在线等

解决方案 »

  1.   

    private void button1_Click(object sender, System.EventArgs e)
    {
    #region 登录
    #region 建立对象
    this.sqlDataAdapter1 = new System.Data.SqlClient.SqlDataAdapter();
    this.sqlDataAdapter2 = new System.Data.SqlClient.SqlDataAdapter();
    this.sqlDeleteCommand2 = new System.Data.SqlClient.SqlCommand();
    this.sqlInsertCommand2 = new System.Data.SqlClient.SqlCommand();
    this.sqlSelectCommand2 = new System.Data.SqlClient.SqlCommand();
    this.sqlUpdateCommand2 = new System.Data.SqlClient.SqlCommand();
    this.sqlSelectCommand1 = new System.Data.SqlClient.SqlCommand();
    this.sqlInsertCommand1 = new System.Data.SqlClient.SqlCommand();
    this.sqlUpdateCommand1 = new System.Data.SqlClient.SqlCommand();
    this.sqlDeleteCommand1 = new System.Data.SqlClient.SqlCommand();
    #endregion try
    {
    this.Cursor=Cursors.Default; #region update,insert,delete
    this.sqlDataAdapter1.DeleteCommand = this.sqlDeleteCommand1;
    this.sqlDataAdapter1.InsertCommand = this.sqlInsertCommand1; this.sqlDataAdapter1.TableMappings.AddRange(new System.Data.Common.DataTableMapping[] {
      new System.Data.Common.DataTableMapping("Table", "getuser", new System.Data.Common.DataColumnMapping[] {
     new System.Data.Common.DataColumnMapping("cbmdh", "cbmdh"),
     new System.Data.Common.DataColumnMapping("cbmmc", "cbmmc"),
     new System.Data.Common.DataColumnMapping("cpassword", "cpassword")})});
    this.sqlDataAdapter1.UpdateCommand = this.sqlUpdateCommand1;
    // 
    // sqlInsertCommand1
    // 
    this.sqlInsertCommand1.CommandText = "INSERT INTO getuser(cbmdh, cbmmc, cpassword) VALUES (@cbmdh, @cbmmc, @cpassword);" +
    " SELECT cbmdh, cbmmc, cpassword FROM getuser WHERE (cbmdh = @cbmdh)";
    this.sqlInsertCommand1.Connection = this.sqlConnection1;
    this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@cbmdh", System.Data.SqlDbType.VarChar, 5, "cbmdh"));
    this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@cbmmc", System.Data.SqlDbType.VarChar, 10, "cbmmc"));
    this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@cpassword", System.Data.SqlDbType.VarChar, 10, "cpassword"));
    // 
    // sqlUpdateCommand1
    // 
    this.sqlUpdateCommand1.CommandText = "UPDATE getuser SET cbmdh = @cbmdh, cbmmc = @cbmmc, cpassword = @cpassword WHERE (" +
    "cbmdh = @Original_cbmdh) AND (cbmmc = @Original_cbmmc) AND (cpassword = @Origina" +
    "l_cpassword); SELECT cbmdh, cbmmc, cpassword FROM getuser WHERE (cbmdh = @cbmdh)" +
    "";
    this.sqlUpdateCommand1.Connection = this.sqlConnection1;
    this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@cbmdh", System.Data.SqlDbType.VarChar, 5, "cbmdh"));
    this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@cbmmc", System.Data.SqlDbType.VarChar, 10, "cbmmc"));
    this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@cpassword", System.Data.SqlDbType.VarChar, 10, "cpassword"));
    this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_cbmdh", System.Data.SqlDbType.VarChar, 5, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "cbmdh", System.Data.DataRowVersion.Original, null));
    this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_cbmmc", System.Data.SqlDbType.VarChar, 10, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "cbmmc", System.Data.DataRowVersion.Original, null));
    this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_cpassword", System.Data.SqlDbType.VarChar, 10, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "cpassword", System.Data.DataRowVersion.Original, null));
    // 
    // sqlDeleteCommand1
    // 
    this.sqlDeleteCommand1.CommandText = "DELETE FROM getuser WHERE (cbmdh = @Original_cbmdh) AND (cbmmc = @Original_cbmmc)" +
    " AND (cpassword = @Original_cpassword)";
    this.sqlDeleteCommand1.Connection = this.sqlConnection1;
    this.sqlDeleteCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_cbmdh", System.Data.SqlDbType.VarChar, 5, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "cbmdh", System.Data.DataRowVersion.Original, null));
    this.sqlDeleteCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_cbmmc", System.Data.SqlDbType.VarChar, 10, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "cbmmc", System.Data.DataRowVersion.Original, null));
    this.sqlDeleteCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_cpassword", System.Data.SqlDbType.VarChar, 10, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "cpassword", System.Data.DataRowVersion.Original, null));
    #endregion

    this.sqlDataAdapter1.SelectCommand = this.sqlSelectCommand1;
    this.sqlSelectCommand1.CommandText = "SELECT cbmdh, cbmmc, cpassword FROM getuser WHERE (cbmmc = @cbmmc)";
    this.sqlSelectCommand1.Connection = this.sqlConnection1;
    this.sqlSelectCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@cbmmc", System.Data.SqlDbType.VarChar, 10, "cbmmc"));
    this.sqlSelectCommand1.Parameters["@cbmmc"].Value=this.flatTextBox1.Text.ToString();
    this.sqlDataAdapter1.Fill(this.dataSet11);
    if(this.flatTextBox1.Text!="")
    {
    if(this.dataSet11.Tables["getuser"].Rows.Count>0)
    {
    try
    {
    #region update,insert,delete
    this.sqlDataAdapter2.TableMappings.AddRange(new System.Data.Common.DataTableMapping[] {
      new System.Data.Common.DataTableMapping("Table", "getuser", new System.Data.Common.DataColumnMapping[] {
     new System.Data.Common.DataColumnMapping("cbmdh", "cbmdh"),
     new System.Data.Common.DataColumnMapping("cbmmc", "cbmmc"),
     new System.Data.Common.DataColumnMapping("cpassword", "cpassword")})});
    this.sqlDataAdapter2.DeleteCommand = this.sqlDeleteCommand2;
    this.sqlDataAdapter2.InsertCommand = this.sqlInsertCommand2;
    this.sqlDataAdapter2.UpdateCommand = this.sqlUpdateCommand2;
      

  2.   


    // 
    // sqlDeleteCommand2
    // 
    this.sqlDeleteCommand2.CommandText = "DELETE FROM getuser WHERE (cbmdh = @Original_cbmdh) AND (cbmmc = @Original_cbmmc)" +
    " AND (cpassword = @Original_cpassword)";
    this.sqlDeleteCommand2.Connection = this.sqlConnection1;
    this.sqlDeleteCommand2.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_cbmdh", System.Data.SqlDbType.VarChar, 5, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "cbmdh", System.Data.DataRowVersion.Original, null));
    this.sqlDeleteCommand2.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_cbmmc", System.Data.SqlDbType.VarChar, 10, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "cbmmc", System.Data.DataRowVersion.Original, null));
    this.sqlDeleteCommand2.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_cpassword", System.Data.SqlDbType.VarChar, 10, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "cpassword", System.Data.DataRowVersion.Original, null));
    // 
    // sqlInsertCommand2
    // 
    this.sqlInsertCommand2.CommandText = "INSERT INTO getuser(cbmdh, cbmmc, cpassword) VALUES (@cbmdh, @cbmmc, @cpassword);" +
    " SELECT cbmdh, cbmmc, cpassword FROM getuser WHERE (cbmdh = @cbmdh)";
    this.sqlInsertCommand2.Connection = this.sqlConnection1;
    this.sqlInsertCommand2.Parameters.Add(new System.Data.SqlClient.SqlParameter("@cbmdh", System.Data.SqlDbType.VarChar, 5, "cbmdh"));
    this.sqlInsertCommand2.Parameters.Add(new System.Data.SqlClient.SqlParameter("@cbmmc", System.Data.SqlDbType.VarChar, 10, "cbmmc"));
    this.sqlInsertCommand2.Parameters.Add(new System.Data.SqlClient.SqlParameter("@cpassword", System.Data.SqlDbType.VarChar, 10, "cpassword"));

    // 
    // sqlUpdateCommand2
    // 
    this.sqlUpdateCommand2.CommandText = "UPDATE getuser SET cbmdh = @cbmdh, cbmmc = @cbmmc, cpassword = @cpassword WHERE (" +
    "cbmdh = @Original_cbmdh) AND (cbmmc = @Original_cbmmc) AND (cpassword = @Origina" +
    "l_cpassword); SELECT cbmdh, cbmmc, cpassword FROM getuser WHERE (cbmdh = @cbmdh)" +
    "";
    this.sqlUpdateCommand2.Connection = this.sqlConnection1;
    this.sqlUpdateCommand2.Parameters.Add(new System.Data.SqlClient.SqlParameter("@cbmdh", System.Data.SqlDbType.VarChar, 5, "cbmdh"));
    this.sqlUpdateCommand2.Parameters.Add(new System.Data.SqlClient.SqlParameter("@cbmmc", System.Data.SqlDbType.VarChar, 10, "cbmmc"));
    this.sqlUpdateCommand2.Parameters.Add(new System.Data.SqlClient.SqlParameter("@cpassword", System.Data.SqlDbType.VarChar, 10, "cpassword"));
    this.sqlUpdateCommand2.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_cbmdh", System.Data.SqlDbType.VarChar, 5, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "cbmdh", System.Data.DataRowVersion.Original, null));
    this.sqlUpdateCommand2.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_cbmmc", System.Data.SqlDbType.VarChar, 10, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "cbmmc", System.Data.DataRowVersion.Original, null));
    this.sqlUpdateCommand2.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_cpassword", System.Data.SqlDbType.VarChar, 10, System.Data.ParameterDirection.Input, false, ((System.Byte)(0)), ((System.Byte)(0)), "cpassword", System.Data.DataRowVersion.Original, null));
    #endregion this.dataSet11.Tables["getuser"].Rows.Clear();
    this.sqlDataAdapter2.SelectCommand = this.sqlSelectCommand2;
    this.sqlSelectCommand2.CommandText = "SELECT cbmdh, cbmmc, cpassword FROM getuser WHERE (cbmmc = @cbmmc) AND (cpassword = @cpassword)";
    this.sqlSelectCommand2.Connection = this.sqlConnection1;
    this.sqlSelectCommand2.Parameters.Add(new System.Data.SqlClient.SqlParameter("@cbmmc", System.Data.SqlDbType.VarChar, 10, "cbmmc"));
    this.sqlSelectCommand2.Parameters.Add(new System.Data.SqlClient.SqlParameter("@cpassword", System.Data.SqlDbType.VarChar, 10, "cpassword"));
    this.sqlSelectCommand2.Parameters["@cbmmc"].Value=this.flatTextBox1.Text.ToString();
    this.sqlSelectCommand2.Parameters["@cpassword"].Value=this.flatTextBox2.Text.ToString();
    this.sqlDataAdapter2.Fill(this.dataSet11);

    if(this.dataSet11.Tables["getuser"].Rows.Count==1)
    {
    Mainform mf=new Mainform();
    mf.Show(); //这里就是你要的
    this.Visible=false;//这里就是你要的 this.Dispose(false);//这里就是你要的 }
    else
    {
    MessageBox.Show("密码错误,请重新填写!");
    }

    }
    catch(Exception ex)
    {
    MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
    }

    }
    else
    {
    MessageBox.Show("该用户不存在,请重新登录!");
    this.flatTextBox3.Focus();
    this.flatTextBox3.Cursor=Cursors.Default;
    }
    }
    else
    {
    MessageBox.Show("用户名不能为空!");
    }
    }
    catch(Exception ex)
    {
    MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
    }
    finally
    {
    this.dataSet11.Dispose();
    this.sqlDataAdapter1.Dispose();
    this.sqlDataAdapter2.Dispose();
    }
    #endregion
    }
      

  3.   

    f2.Hide();
    改成this.Hide();试试
      

  4.   

    最好不要Form1 f2=new Form1();
      

  5.   

    以上程序已经调试通过,我自己写的,用来登陆的。mf.Show(); 
    this.Visible=false;
    this.Dispose(false);这样显示的窗体显示,this隐藏后在Dispose,就可以了.
      

  6.   

    ==================
    我是楼主
    ==================
    private void button1_Click(object sender, System.EventArgs e)
    {
    if(username.Text=="hexinyu" && userpwd.Text=="")
    {
    Form1 mf=new Form1();
    mf.Show();
    this.Visible=false;
    this.Dispose(false);
    }
    else
    {
    MessageBox.Show("用户名或者密码错!");
    }
    }
    我这样进入那个窗口后又回来了,因为
    在主窗口
    private void Form1_Load(object sender, System.EventArgs e)
    {
    Login lf=new Login();
    lf.Show();
    this.Visible=false;
    this.Dispose(false);
    }我应该怎么做??
      

  7.   

    你不觉得这样做很累吗?
    在Form1的Load里调Login
    又在Login里回到Form1
    你只用把Login设成起动窗体不简单多了吗?那样的话你只用在Login的Button里this.Hide();
    再把Form1实例化并Show出来不就可以了
      

  8.   

    Form1:
    static void Main() 
    {
    login newForm=new login();
    newForm.ShowDialog();
    Application.Run(new Form1());
    }
    Login:
    private void button1_Click(object sender, System.EventArgs e)
    {
    if(username.Text=="hexinyu" && userpwd.Text=="")
    {
    this.Close();
    }
    else
    {
    MessageBox.Show("用户名或者密码错!");
    }
    }
      

  9.   

    HomePage页:
    private void button1_Click(object sender, System.EventArgs e)
    {
    this.Hide();
    test t = new test();
        t.Show();

    }
    test页:
    private void button1_Click(object sender, System.EventArgs e)
    {
    HomePage h = new HomePage();
    h.Show();
    this.Close();
    }
    我这样就可以啊,你试试
      

  10.   

    把login窗体设成启动窗体,然后用我的程序即可,入口函数也要放在login窗体。
      

  11.   

    login页面被隐藏后最后关闭程序时如何关闭login页面呢
      

  12.   

    找了几天都没好的方法,以前都是做webForm程序,现在开始写WinForm还确实不适应
      

  13.   

    搞一个 判断的字段aaa   bool的
    如果 aaa=truethis.Close();//关闭窗体
      

  14.   

    wsh236 正解
    static void Main() 
    {
    login newForm=new login();
    newForm.ShowDialog();
    //这里修改下!            if(login_flag == "ok")  做个静态变量来标示是否成功登陆
                                   {
    Application.Run(new Form1());
                               }
    }//在newForm 里面登陆成功后把 login-flag 改成ok  然后 this.close() 就可以了注意把main()放到一个单独的类中去!
      

  15.   

    Form1:
    static void Main() 
    { Application.Run(new FrmLogin());
    }
    Login
    private void btnOK_Click(object sender, System.EventArgs e)
    {   if(username.Text=="yourName" && userpwd.Text=="")
    {
    this.Close();
                                         Form1 form1=new Form1();
    form1.Show();
    }
    else
    {
    MessageBox.Show("用户名或者密码错!");
    }
    }
      

  16.   

    登陆程序不過是來驗証用戶的一個簡單過程。
    運行完了就Close掉﹐只保留用戶信息就OK了。用個類的靜態變量保存就搞定﹐不要那么麻煩.
      

  17.   

    FormLogin formLogin = new FormLogin();
                    formLogin.ConnectionString = m_connStr;
                    bool login = false;
                    while (!login)
                    {
                        formLogin.ShowDialog();
                        if ((formLogin.DialogResult == DialogResult.OK))
                        {
                            login = true;
                            this.WindowState = FormWindowState.Maximized;
                            m_loginUser = formLogin.LoginUser;                    }
                        else if (formLogin.DialogResult == DialogResult.Cancel)
                        {
                            login = true;
                            this.Close();
                        }
                    }我是用while循环来实现的.