当我从登录屏幕打开一个新表单时,它会登录用户,显示表单然后立即消失???
这是我相关部分的代码:
string checkAdmin = "select * from Logins where Name = @userName and UserType ='Admin'";
string checkUser = "select * from Logins where Name = @userName and UserType ='User'";
using (var connection = new SqlConnection(@"server=.\SQLEXPRESS; database=loginsTest;Trusted_Connection=yes"))
{
using (var checkAdminCommand = new SqlCommand(checkAdmin, connection))
{
//guessing at the column length here. Use actual column size instead of 20
checkAdminCommand.Parameters.Add("@username", SqlDbType.NVarChar, 50).Value = userNameBox.Text;
connection.Open();
if (checkAdminCommand.ExecuteScalar() != null)
{
adminScreen admnscrn = new adminScreen();
admnscrn.Show();
this.Close();
return;
}
else
{
}
}
using (var connection = new SqlConnection(@"server=.\SQLEXPRESS; database=loginsTest;Trusted_Connection=yes"))
{
using (var checkUserCommand = new SqlCommand(checkUser, connection))
{
//guessing at the column length here. Use actual column size instead of 20
checkUserCommand.Parameters.Add("@username", SqlDbType.NVarChar, 50).Value = userNameBox.Text;
connection.Open();
if (checkUserCommand.ExecuteScalar() != null)
{
userScreen usrscrn = new userScreen();
usrscrn.Show();
this.Close();
}
else
{
MessageBox.Show("Invalid details, try again");
passwordBox.Text = "";
}
}
}
因此,当我打开 usrscrn 或 admnscrn 时,它会弹起并再次直接返回?我试过研究,我用过 Application.Run(userScreen()); 等等,它只是抛出一个异常,说明第二个消息循环?
我创建了一个新屏幕,将其设置为 MDI,并输入以下代码:
private void MDI_Form_Load(object sender, EventArgs e)
{
Form1 loginscrn = new Form1();
loginscrn.Show();
}
我的 program.cs 文件包含以下代码:
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new MDI_Form());
}
这行得通吗?