我的代码有什么问题?c# winforms

3

我有两个表单,登录表单和主表单。初始情况下,登录表单将被显示,当用户通过身份验证后,主表单将被显示,并关闭登录表单。

它基本上可以工作,但我必须点击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;
            }
        }
    }
}

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

1
你进行过一些调试吗? - decyclone
是的先生...看起来我的授权代码获取了用户。 - yonan2236
我认为这是因为btnLogin的DialogResult属性被设置为none。只有在用户通过身份验证后,它才会被设置为“OK”。因此,用户必须点击两次按钮才能关闭对话框...我该怎么办? - yonan2236
如果 (fLogin.ShowDialog() == DialogResult.OK) { Application.Run(new Main()); } 在这种情况下,在 Application.Run(new Main()); 之后使用 fLogin.Dispose(); - Ankush Roy
5个回答

3

只要修改

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

剩下的问题是什么? - Navaneethan
1
不要调用Close()方法,设置窗体的DialogResult属性就足够了。 - Hans Passant

1

试试这个:

namespace Login

{

static class Program

{ /// /// The main entry point for the application. ///

   [STAThread]
   static void Main()
   {
       Application.EnableVisualStyles();
       Application.SetCompatibleTextRenderingDefault(false);
       Application.Run(new Login());
   }

}

}

>

命名空间登录 { public partial class 登录:表单 { public 登录() { 初始化组件(); } } }
    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)
        {
            Main formmain = new Main();
            formmain.Show();
            this.Dispose(); // U can also use this.Close();
        }
    }
}

}


@Ankush,@yonan:在“登录”和“主要”表单交互的方式中存在三个问题。 1)逻辑上似乎不正确,“登录”表单根本不应该生成“主要”表单;在我看来,这违反了单一职责原则。在“登录”表单关闭并返回后,应该从“程序”类中显示“主要”表单。 2)“主要”表单应该是模态的(ShowDialog),而不是非模态的(Show)。否则,程序将过早终止。 3)“登录”表单不应该自行处理。相反,在“Program.Main”中,将“new Login()”包装在“using”块中。 - stakx - no longer contributing

1
在编写按钮事件处理程序时,当您想要关闭窗体(设置所需的DialogResult后),应该只调用Form.Close()。
据我所知,这不会自动发生。
我从未以其他方式完成过它,而且这种方法对我总是有效的。

0

很可能是您的Authenticate()出了问题。您能否删除该代码并简单地使用

private void Authenticate()
{
   btnLogin.DialogResult = DialogResult.OK;
}

这将告诉您问题是身份验证代码还是GUI的问题。

如果是您的身份验证代码有问题,可能需要更多操作才能访问数据库(检查并查看是否已连接等)。


0

如前一篇帖子所述,可能是Authenticate方法出了问题... 也许访问数据库需要更长的时间... 下面的代码将帮助我们知道Authenticate是否已经完成处理...

private void btnLogin_Click(object sender, EventArgs e)
{
      btnLogin.Enabled = false; 

      // initially btnLogin has a DialogResult property set to None
      Authenticate();
      // better place call to Authenticate in try catch blocks
      // to prevent btnLogin in a disabled state forever if Authenticate fails with
      // an exception ...
      // also if an exception occurs show that in a message box

      btnLogin.Enabled = true;
}

在主程序中,一旦用户已经通过身份验证,关闭登录窗口。
 Login fLogin = new Login();
            if (fLogin.ShowDialog() == DialogResult.OK)
            {
                fLogin.Close();
                Application.Run(new Main());
            }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接