成功登录后关闭登录表单 [英] closing login form after successful login

查看:59
本文介绍了成功登录后关闭登录表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个小型C#应用程序.我是C#的新手.

我有2种表格form1&该form1中的form2是登录表单,在提供所有凭据后将其重定向到form2.但是form1(登录表单)仍然保留在它后面.成功登录后,我有2个已关闭.
请帮帮我.
预先感谢.

I am creating a small C# application. I''m new in C#.

I have 2 forms form1 & form2 in that form1 is login form , after giving all credentials it redirect to form2. But form1 (login form)remain behind it. I have 2 closed it on successful login.
Please help me.
Thanks in advance.

推荐答案

好吧,这是我第一次遮篷,但我早就在寻找这种遮篷,所以我会尽力帮助您

要显示第二种形式,你会做的
Okay this is my first time awnsering to a post but i was looking for this awnser earlyer so i will try to help you

To show the 2nd form you would do
form2 f2 = new form2();
f2.Show();



隐藏一个简单的东西



To hide for one simple do

form2 f2 = new Form2();
f2.Show();
this.hide();




但是这样做的缺点是,如果您关闭for2,则窗体1仍将被隐藏,
因此,如果您想让Form 2在您关闭form2时关闭整个应用程序,请执行以下操作





But the disadvantage to that is that, if you Close for2, Form 1 will still be hidden,
so if you want to have Form 2 Close you whole application when u close form2 do this


private void btnLogin_Click(object sender, EventArgs e)
        {

                Form2 frm2 = new Form2();
                frm2.FormClosed += new FormClosedEventHandler(frm2_FormClosed);
                frm2.Show();
                this.Hide();
        }
        private void frm2_FormClosed(object sender, FormClosedEventArgs e)
        {
            this.Close();
        }
        }



我将其添加到我的登录按钮中,因此,如果他们成功登录,它将隐藏Form1和Show Form2,然后当他们想要关闭应用程序时,他们只是关闭Form2

希望对您有帮助,我花了很长时间弄清了前一天的夜晚



如果您需要其他帮助,请告诉我



i added that to my login button, so if they Succsesfuly login, it will hide Form1 and Show Form2, then when they want to close the application They just close Form2

i hope that made sense top you, i spent a long time figureing that out the other night



If you need anymore help let me know


如果Form1是您的启动表单(即,在启动应用程序时它会自动出现),那么您将无法关闭它并将Form2显示为关闭表单将终止该应用程序并关闭所有表格.

要么:
1)登录时隐藏Form1,然后显示Form2.然后,您可以在Form2关闭时关闭Form1(和应用程序),也可以重新显示Form1以进行新的登录.
2)修改Program.cs中的Main方法,以从Form1中获取结果(成功登录或关闭应用程序),然后根据需要显示Form2或关闭.
3)将Form2设为启动表单,并在构造函数中显示您的登录表单.
If Form1 is your startup form (i.e. it comes up automatically when you start the application) then you cannot just close it and show Form2 as closing the form will terminate the application and close all forms.

Either:
1) Hide Form1 when you log in, and show Form2. You can then close either close Form1 (and the app) when Form2 closes, or re-display Form1 for a fresh log in.
2) Modify the Main method in Program.cs to take a result from Form1 (successful login or close application) and then display Form2 or close down as appropriate.
3) Make Form2 your startup form, and display your login form as part of the constructor.


您好专家对此发表评论

您也可以不隐藏表单而做到

为此,您必须像这样在启动时使用Program.cs中的Main()

Hello experts comment on this

you can do it without hiding form also

For that you have to use Main() in Program.cs as start up like this

public static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            UI.FormUserLogin objForm = new UI.FormUserLogin();
            if (objForm.ShowDialog() == DialogResult.OK)
            {
                objForm.Dispose();
                Application.Run(new UI.FormMain(objForm.UserInfo));
            }
        }




在这里,无论用户是否通过身份验证,您的登录表单都必须返回.

如果您有任何了解的问题,请告诉我.

:)




here your Login form must return whether user is authenticate or not.

please let me know if you have any problem to understand it.

:)


这篇关于成功登录后关闭登录表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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