gpt4 book ai didi

c# - 如何在单击交叉按钮时使 bool 值为 false?

转载 作者:太空宇宙 更新时间:2023-11-03 14:47:13 27 4
gpt4 key购买 nike

我正在构建一个登录表单,其中我有一个 bool 值,当用户从登录页面单击登录按钮时该值变为真。现在我希望如果用户单击交叉按钮关闭 MDI 表单,则 bool 值应该变为false 并且用户可以从登录表单再次登录。我该怎么做?在我的例子中,当我点击交叉按钮时,我无法做到这一点,它显示错误消息“您已经登录”,但如果表单关闭,它应该允许我再次登录。请帮助我摆脱困境。

Here is the code for the login form----

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace Login
{
public partial class LogIin : Form
{
public bool IsLoggedIn { get; set; }


public LogIin()
{
InitializeComponent();
}

private void LogIn_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(@"Data Source=IBMPC\SQLEXPRESS;Initial Catalog=UserData;Integrated Security=True");
SqlCommand cmd = new SqlCommand("select * from User_Credential where UserName = @UserName and Passwords = @Passwords", con);

cmd.Parameters.AddWithValue("@UserName", UsTxt.Text);
cmd.Parameters.AddWithValue("@Passwords", PassTxt.Text);
SqlDataAdapter sda = new SqlDataAdapter(cmd);


DataTable dt = new DataTable();
sda.Fill(dt);
con.Open();
int i = cmd.ExecuteNonQuery();
con.Close();

{

if (!IsLoggedIn)
{

if (dt.Rows.Count > 0)
{

Welcome_Form wf = new Welcome_Form();

wf.Show();
IsLoggedIn = true;
}
else
{
MessageBox.Show("Please enter Correct Username and Password");
}
}
else
{
MessageBox.Show("You are already logged in", "Error");
}

}
}


private void Exit_Click(object sender, EventArgs e)
{

this.Close();
}
}
}

最佳答案

如果关闭窗口,则必须将值设置为 false。

 private void Exit_Click(object sender, EventArgs e)
{
IsLoggedIn = false;
this.Close();
}

关于c# - 如何在单击交叉按钮时使 bool 值为 false?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53588424/

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