我是c#的新手。 有登录项目的登录系统。 表格有问题。
我有2个表单,form1和form2。 Form1是我的主要表单,我的登录表单。 Form2是注册表。
这就是应用程序的运行方式:在form1显示之前,首先要满足一个条件。 如果满足条件,将出现消息框,然后将显示form2。 但是,当条件不满足时,将显示form1。
所以问题是,虽然满足条件,但form1仍然显示。 Form2出现,但form1也出现。
private void Form1_Load(object sender, EventArgs e) { if (condition) { MessageBox.Show("Message"); this.Hide(); Form2 frm = new Form2(); frm.Show(); } }I'm new to c#. Got a log-in system with registration project. Having trouble with the forms.
I have 2 forms, form1 and form2. Form1 is my main form, my log-in form. Form2 is the registration form.
So this is how the application runs: Before form1 shows, there is a condition first to be met. If the condition is met, message box will appear then form2 will show. However, when the condition is not met form1 will show.
So the problem is, form1 keeps showing although the condition is met. Form2 appears but form1 appears too.
private void Form1_Load(object sender, EventArgs e) { if (condition) { MessageBox.Show("Message"); this.Hide(); Form2 frm = new Form2(); frm.Show(); } }最满意答案
你不应该在Form1 Form_Load中开始这样做。 从逻辑上讲,首先测试你的病情是最有意义的,如果你不打算使用它,就不要实例化Form1 。
在Program.cs ,如果你没有改变它,你应该有以下代码行:
Application.Run(new Form1());您应该使用某些代码替换该行以测试您的条件并确定要启动的表单:
if (condition) { MessageBox.Show("Message"); Application.Run(new Form2()); } else { Application.Run(new Form1()); }You shouldn't do this sort of thing in Form_Load of Form1 to begin with. Logically, it makes the most sense to test your condition first and then never instantiate Form1 if you're not going to use it.
Inside Program.cs, if you haven't changed it, you should have the following line of code:
Application.Run(new Form1());You should replace that line with some code to test your condition and determine which form to start up with:
if (condition) { MessageBox.Show("Message"); Application.Run(new Form2()); } else { Application.Run(new Form1()); }更多推荐
发布评论