gpt4 book ai didi

c# - 如何关闭 this.form

转载 作者:太空宇宙 更新时间:2023-11-03 22:05:49 25 4
gpt4 key购买 nike

我有 3 个类:Form1、LoginForm 和程序。

程序包含我的主要方法,然后运行 ​​loginform,如果满足登录表单中的条件,则运行 form1。

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

因为我不能使用 loginform.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;
}
}
}

}
}

我错过了什么吗?这是我的主要类(class);

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 方法来创建登录表单,并且仅当登录成功时才实例化并显示主申请表。

查看此问题/答案以获取详细信息和示例:How can I close a login form and show the main form without my application closing?

您实际上需要这种方法:

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

关于c# - 如何关闭 this.form,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8490315/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com