c# - 我的代码有什么问题?时间:2019-05-08 标签:c#winforms

标签 c# winforms

我有两种形式的登录和主形式。最初,将显示登录表单,当用户通过身份验证时,将显示主表单并关闭登录表单。

它有点工作,但我必须单击 btnLogin(登录表单中的一个按钮)两次以关闭登录表单并显示主表单。

这是我的代码。

Program.cs(登录表单)

namespace Login
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            Login fLogin = new Login();
            if (fLogin.ShowDialog() == DialogResult.OK)
            {
                Application.Run(new Main());
            }
        }
    }
}

登录表单

namespace Login
{
    public partial class Login : Form
    {
        public Login()
        {
            InitializeComponent();
        }

        private void Login_Load(object sender, EventArgs e)
        {

        }

        private void btnLogin_Click(object sender, EventArgs e)
        {
            // initially btnLogin has a DialogResult property set to None
            Authenticate();
        }

        private void Authenticate()
        {
            SqlCeConnection conn = new SqlCeConnection(@"Data source=d:/BIMS.sdf");
            conn.Open();
            SqlCeCommand cmd = new SqlCeCommand(Properties.Resources.CheckIfUserExists, conn);
            cmd.Parameters.Add("username", txtUsername.Text.Trim());
            cmd.Parameters.Add("password", txtPassword.Text.Trim());

            SqlCeDataReader dr = cmd.ExecuteReader();
            bool hasRow = dr.Read();
            if (hasRow)
            {
                btnLogin.DialogResult = DialogResult.OK;
            }
        }
    }
}

你认为我哪里做错了? 谢谢....

最佳答案

只是改变

if (hasRow)
{
  // btnLogin.DialogResult = DialogResult.OK;
     this.DialogResult = DialogResult.OK;
     this.close();
}

关于c# - 我的代码有什么问题?时间:2019-05-08 标签:c#winforms,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4766524/

相关文章:

c# - 被阻塞的 Windows 消息循环的调试思路

c# - 如何在 ASP.NET MVC 中确定 Controller 的区域

c# - Xamarin - 显示来自 base64 字符串的图像

c# - Math.Max vs inline if - 有什么区别?

c# - 使用 dataTable 从 dataGridView 中删除行

c# - 使用自定义模型 Microsoft azure 的 Azure 语音到文本单词准确度(以 % 为单位)

.net - 是否有 Control.BeginInvoke 的变体可以在句柄销毁之前/之后工作?

c# - 打字机无法将嵌套的 c# 类正确转换为 TS 类

c# - Unity中Sprite上的滚动纹理用于背景滚动效果,不起作用

c# - 内部连接多个表