[英]Parent form and Child form in Loop C#
I am trying to create a login for my tool and have sorted that but when they press login(Child form) the program loops and asks the user to login again.我正在尝试为我的工具创建一个登录名并对其进行了排序,但是当他们按下登录名(子表单)时,程序会循环并要求用户再次登录。 I notice when I press the 'Exit' button it then loads the Parent form fine but I dont want that.我注意到当我按下“退出”按钮时,它会很好地加载父表单,但我不想要那样。 I want my users to press login and then go right to the Parent form.我希望我的用户按登录,然后直接转到父表单。 To open the child form before the parent form inside of Form1_Load I have this:要在 Form1_Load 中的父表单之前打开子表单,我有这个:
Login Log = new Login();
Log.ShowDialog();
Inside the 'Login' button on the child form I have this:在子表单上的“登录”按钮内,我有这个:
this.Hide();
Form1 Main = new Form1();
Main.Show();
The best way to handle this would be to handle the login BEFORE your form starts (eg in the Program.cs but after the application initialization).处理此问题的最佳方法是在表单启动之前处理登录(例如在 Program.cs 中,但在应用程序初始化之后)。 Insert a variable into your login form (Eg a bool that shows if they have passed your authorization test) and check after the form has closed to see what the status of authorization is.在您的登录表单中插入一个变量(例如,一个显示他们是否通过您的授权测试的布尔值)并在表单关闭后检查授权状态。 If the flag is set to false then you can do a return or Application.Exit() and the user will never see the main form.如果该标志设置为 false,那么您可以执行 return 或 Application.Exit() 并且用户将永远不会看到主窗体。
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
SomeLoginForm Login = new SomeLoginForm();
Login.ShowDialog();
if (!Login.HasPassedAuthorization)
{
MessageBox.Show("Sorry you failed to pass the test! I'm kicking you out now!");
Application.Exit(); // or do a "return;"
}
Application.Run(new Form1());
}
}
static class SomeLoginForm()
{
internal bool HasPassedAuthorization;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.