如果输入的信息正确,则关闭第一份表格 [英] Closing the First Form if the entered information is correct

查看:63
本文介绍了如果输入的信息正确,则关闭第一份表格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,

在一个程序中,我有2种形式.第一个用于从用户获取用户名"和密码".如果输入的信息正确,则显示第二个表格.现在,我想关闭第一个表单,或者至少隐藏它不被看到,因此我使用了下面的代码,但是它没有关闭或隐藏第一个表单.您能告诉我如何使它工作吗?

非常感谢

Hello,

In a program I have 2 forms. First one is used to get "Username" and "Password" from the user. If the entered information is correct, the second form is shown. Now I want to close the first form or at least hide it not to be seen so I used the code below but it did not close or hide the first form. Could you please let me know how could I make it work?

Thank you a lot

private void button1_Click(object sender, EventArgs e)
        {
            string InputUserName = "Ali";
            string InputPassword = "4862597k";

            string strUserName = textBox1.Text;
            string strPassword = maskedTextBox1.Text;

            if (strPassword == InputPassword && strUserName == InputUserName )
            {
                Form2 f2 = new Form2();
                f2.Show();
                Form1 f1 = new Form1();
                f1.Hide();  // f1.Close();
            }
            else
            {
                MessageBox.Show("Wrong Password or Username, Try again.",
                                "Alert", MessageBoxButtons.OK, 
                                MessageBoxIcon.Warning);

                maskedTextBox1.Text = "";
            }
        }

推荐答案

好吧,当您这样做时:
Well, when you do this:
Form2 f2 = new Form2();
f2.Show();
Form1 f1 = new Form1();
f1.Hide();  // f1.Close();


您正在创建两个新表单,分别显示一个表单和另一个表单.您是否要隐藏已经显示的表单,而不是隐藏刚刚创建的新表单?我认为您的意思是this.Hide()而不是创建新的Form1.

另外,请参见 [


you''re creating two new forms, showing one and hiding the other. Don''t you want to hide the form that''s already shown instead of hiding a new one that you just created? I think you meant this.Hide() instead of making a new Form1.

Also, see this[^] answer that I posted to a very similar question. It describes in vague terms a better solution for creating and showing a login form.


您应该将其替换为您的代码



如果(strPassword == InputPassword&&strUserName == InputUserName
{
Form2 f2 =新的Form2();
f2.Show();



this.Hide(); ///////


}

只需尝试一下.
you should replace this with ur code



if (strPassword == InputPassword && strUserName == InputUserName
{
Form2 f2 = new Form2();
f2.Show();



this.Hide(); ///////


}

just try it .


对于您采用的方法,我可能建议您在要远程"关闭的表单上添加一个名为CloseForm()的公共方法.如果需要,这将允许您执行其他关闭项,这些逻辑应该在该类上,而不是在该类上.

当您创建Form1的实例以显示用户名/密码对话框时,请保留对其的引用.使用引用来调用CloseForm()方法(而不是像上面那样创建一个新方法).

干杯.
For the approach you''re taking, I would likely suggest adding a public method called CloseForm() on the form you want to close "remotely". This will allow you to do other shutdown items if required down the road, logic that should be on that class and not this one.

When you create an instance of Form1 to show the username/password dialog, keep a reference to it. Use the reference to invoke the CloseForm() method (rather than creating a new one as you do above).

Cheers.


这篇关于如果输入的信息正确,则关闭第一份表格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆