如何关闭这个表单

本文关键字:表单 何关闭 | 更新日期: 2023-09-27 18:17:36

我有三个类:Form1, LoginForm和program。

程序保存我的main方法,然后运行loginform,如果满足登录表单中的条件,则运行form1。

我想让它在显示form1之前隐藏loginform。

我不能使用loginfo .hide();

代码:

namespace RepSalesNetAnalysis
{
public partial class LoginForm : Form
{
    public  bool letsGO = false;
    public LoginForm()
    {
        InitializeComponent();
    }
    private static DataTable LookupUser(string Username)
    {
        const string connStr = "Server=10asaf;" +
                            "Database=dfafa;" +
                            "uid=bufaf;" +
                            "pwd=dridfsdf;" +
                            "Connect Timdf0;";
        //"Data Source=apex2006sql;Initial Catalog=Leather;Integrated Security=True;";
        const string query = "Select password From dbo.UserTable (NOLOCK) Where UserName = @UserName";
        DataTable result = new DataTable();
        using (SqlConnection conn = new SqlConnection(connStr))
        {
            conn.Open();
            using (SqlCommand cmd = new SqlCommand(query, conn))
            {
                cmd.Parameters.Add("@UserName", SqlDbType.VarChar).Value = Username;
                using (SqlDataReader dr = cmd.ExecuteReader())
                {
                    result.Load(dr);
                }
            }
        }
        return result;
    }
    private void buttonLogin_Click(object sender, EventArgs e)
    {
        if (string.IsNullOrEmpty(textUser.Text))
        {
            //Focus box before showing a message
            textUser.Focus();
            MessageBox.Show("Enter your username", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
            //Focus again afterwards, sometimes people double click message boxes and select another control accidentally
            textUser.Focus();
            return;
        }
        else if (string.IsNullOrEmpty(textPass.Text))
        {
            textPass.Focus();
            MessageBox.Show("Enter your password", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
            textPass.Focus();
            return;
        }
        //OK they enter a user and pass, lets see if they can authenticate
        using (DataTable dt = LookupUser(textUser.Text))
        {
            if (dt.Rows.Count == 0)
            {
                textUser.Focus();
                MessageBox.Show("Invalid username.", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
                textUser.Focus();
                return;
            }
            else
            {
                string dbPassword = Convert.ToString(dt.Rows[0]["Password"]);
                string appPassword = Convert.ToString(textPass.Text); //we store the password as encrypted in the DB
                //MessageBox.Show
                Console.WriteLine(string.Compare(dbPassword, appPassword));
                if (string.Compare(dbPassword, appPassword) == 0)
                {
                     DialogResult = DialogResult.OK;
                     this.Close();
                }
                else
                {
                    //You may want to use the same error message so they can't tell which field they got wrong
                    textPass.Focus();
                    MessageBox.Show("Invalid Password", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
                    textPass.Focus();
                    return;
                }
            }
        }
    }
}

}

我错过了什么吗?这是我的主类;

namespace RepSalesNetAnalysis
{
static class Program
{
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        LoginForm fLogin = new LoginForm();
        if (fLogin.ShowDialog() == DialogResult.OK)
        {
            Application.Run(new Form1());
        }
        else
        {
            Application.Exit();
        }
    }
}
}

如何关闭这个表单

Steven,这完全是错误的。

还有其他的方法来做你需要正确的Program类的Main方法创建一个登录表单,只有当登录成功时,你实例化并显示主应用程序表单。

查看这个问题/答案的详细信息和示例:我如何关闭登录表单并显示主表单而不关闭应用程序?

你实际上需要这种方法:

static void Main()
{
    LoginForm fLogin = new LoginForm();
    if (fLogin.ShowDialog() == DialogResult.OK)
    {
        Application.Run(new MainForm());
    }
    else
    {
        Application.Exit();
    }
}

您可以在LoginForm中设置DialogResultOKCancel。这将关闭LoginForm并将对话结果返回给Main方法。然后在Main方法中检查结果,如下所示。

[STAThread]
static void Main()
{
    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);
    LoginForm form = new LoginForm();
    if (form.ShowDialog() == DialogResult.OK)
        Application.Run(new Form1());
}

希望对您有所帮助

static class Program
{
    private static bool canLogin;
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        using (LoginForm loginForm = new LoginForm()){
           // show as dialog
           // perform logic to check if successful
           canLogin = SomeStaticClass.VerifyCredentials(loginForm.Credentials);
           // grab any properties you may want here
        }
        //then run the application
        if(canLogin){
           Application.Run(new Form1());
        }
    }
}
}