在C#中不使用MDI的情况下从另一种形式调用一种形式 [英] Calling one form from another form without using MDI in C#

查看:78
本文介绍了在C#中不使用MDI的情况下从另一种形式调用一种形式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我在C#.net的Windows应用程序中有一个小问题.我将在下面清楚地说明该任务:
实际上,我有两种形式,一种是登录表单,另一种是家庭表单.

首先,用户应在验证后输入其凭据,然后显示第二个表单,同时必须关闭登录表单.

Hi everybody,

I have a small problem in windows application in C#.net. I will clearly explain that task in below:
Actually I have two forms i.e, one is login form and another one is home form.

At first the user should enter his credentials after verifying, the second form should be display and at the same time login form has to close.

 'In login form i have created one object for the second form by using
 form2 obj=new form2()
 obj.show()
'this is working fine but i don't want first form so for that i have used,
 form1 obj1=new form1()
 form1.close()


但是,在运行该应用程序时,将同时显示两个窗体(form1和form 2).我不想显示form1
因此,请帮助我在不使用MDI的情况下找到解决方案.

谢谢,
Sandeep


But, while running the application both forms(form1 and form 2) are displaying. I don''t want to display form1
so please help me for finding the solution with out using MDI.

Thanks,
Sandeep

推荐答案

您不想关闭Form1:如果这样做,您的应用程序将结束,并且form1和form2都将关闭.

您需要返回到课程笔记,并阅读实例:
You don''t want to close Form1: if you do, your application will end, and both form1 and form2 will close.

You need to go back to your course notes, and read up on instances:
form1 obj1=new form1()
form1.close()

这里有很多问题:
1)obj1不引用您正在显示的现有form1.当您编写new关键字时,应该提醒您它正在创建form1的新实例.现在,您有两个实例:您可以看到正在运行的一个实例,而您不能创建一个新的实例.
2)form1.Close不作为标准存在.这将是一个静态方法(不必担心名称,您稍后会找到),该方法未连接到任何实例,因此无法关闭当前(或实际上)任何形式.

您想要的是this
this是您要显式引用类的当前实例时使用的词.因此,您可以使用:

There are a number of problems here:
1) obj1 does not refer to the existing form1 you are displaying. When you wrote the new keyword it is supposed to remind you taht it is creating a new instance of form1. You now have two instances: the running one which you can see, and the new one you just created, which you can''t.
2) form1.Close doesn''t exist as standard. It would be a static method (don''t worry about the name, you will find out later) which is not connected to any instance, and so cannot close the current (or indeed any) form.

What you want is this
this is the word you use when you want to explicitly refer to the current instance of a class. So, you could use:

this.Close();

并且Form1的当前实例将关闭.不幸的是,这也会结束您的应用程序.

将代码替换为:

And teh current instance of Form1 would close. Unfortunately, this will also end your application.

Replace your code with:

this.Hide();
form2 f2 = new form2();
f2.ShowDialog();
this.Show();

Form1将出现关闭,form2将出现,并且当它关闭时,for1将重新出现.

可以吗?

And you Form1 will appear to close, form2 will appear, and when it closes, for1 will reappear.

OK?


看看这个技巧是否有用:

C#Apps中的多个后续主"表单 [ ^ ]

如果要允许用户注销,则只需以编程方式重新启动应用程序,然后将再次显示第一个表单.
See if this tip/trick doesn''t help:

Multiple Subsequent "Main" Forms in C# Apps[^]

If you want to allow the user to log off, you can simply programatically restart the app, and the first form will be displayed once again.


这篇关于在C#中不使用MDI的情况下从另一种形式调用一种形式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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