登录表单后如何打开Windows表单 [英] how to open a windows form after a login form

查看:120
本文介绍了登录表单后如何打开Windows表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何打开登录表单之后的表单应用程序.我创建了一个登录表单,该登录表单会导致在消息框中成功登录消息.
登录成功后,我可以在该表格后打开我的应用程序吗?如果登录名无效,则返回相同的登录名
我的登录表单代码是
另外我的数据库是sql server 2008,我正在使用Visual Studio
在此先感谢
救救我

how to open a application which is a form after a login form.i have made a login form which results in a message login successfull in a messagebox.
can i open my application after that form when the login is successfull. and returns to the same login form if the login is not valid
my code for login form is
also my database is sql server 2008 and i am using visual studio
thanks in advance
help me plz

private void button1_Click(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection();
            con.ConnectionString = @"Data Source=.\SQLEXPRESS;AttachDbFilename=c:\documents and settings\aquib\my documents\visual studio 2010\Projects\login\login\Database1.mdf;Integrated Security=True;User Instance=True";
            try
            {
                con.Open();
            }
            catch (Exception)
            {
                MessageBox.Show("Error with the databse connection");
            }
            string qry1 = "Select * from Log007 where Password=@Password and Username=@Username";
            SqlCommand com = new SqlCommand(qry1, con);
            com.Parameters.AddWithValue("@Username", this.textBox1.Text);
            com.Parameters.AddWithValue("@Password", this.textBox2.Text);
            SqlDataReader dr = com.ExecuteReader();
            while (dr.Read())
            {
                if (dr.HasRows == true)
                {
                    MessageBox.Show("Login Successfull", "Login Information");
                }
            }
            if (dr.HasRows == false)
            {
                MessageBox.Show("Access Denied", "Login Information");
                this.Close();

推荐答案


登录成功后尝试添加以下内容:
Hi,
Try adding this when login is successful:
if (dr.HasRows == true)
{
       MessageBox.Show("Login Successfull", "Login Information");
       this.Hide(); //This will hide your current form
       MyWinForm1 M1 = new MyWinForm1();//Create the object of your second form
       M1.Show(); //Show the second form using the object.
}


关闭文件时,请按照我在下面的评论中所述处理表单关闭事件.使用这个:


While closing the file handle the form closing event as I said in my comments below. Use this:

private void Form1_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
  // Display a MsgBox asking the user to save changes or abort.
  if(MessageBox.Show("Do you want to save changes to your text?", "My Application",
     MessageBoxButtons.YesNo) ==  DialogResult.Yes)
  {
     // Cancel the Closing event from closing the form.
     e.Cancel = true;
     Application.Exit();
     // Call method to save file...
  }
}





祝一切顺利.
--Amit





All the best.
--Amit


在Program类中使用您的代码.在此代码中,我假设Form1是您的WinForm启动文件.

use your code at Program class. In this code I assume that Form1 is your Start up WinForm.

class Program
{
   int count = 3;
   do
   {
       // show log in form here 3 times if user get mistake at typing
       if (!dr.HasRows)
           Application.Exit();
       else
           break;

   } while (count != 0);

   Application.Run(new Form1());
}


这篇关于登录表单后如何打开Windows表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆