在c#中使用多个表单 [英] Using Multiple Forms in c#

查看:131
本文介绍了在c#中使用多个表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试制作一个小型项目,它为不同的状态使用多种形式(对话框),并且遇到了一些问题。
我的对话框是登录,设置和显示。
当应用程序启动时,显示登录表单

  Application.Run(new login()); 

从用户可以打开设置窗体,或者如果满足某些要求,则显示窗体。 / p>

Q1:打开设置窗体时,如何使用户登录表单不可用(我希望用户在设置窗体中填写字段,然后点击保存按钮退出,然后他可以在登录表单中执行其他任何操作)



Q2:当用户打开显示窗体时,我如何隐藏登录表单并在用户关闭显示窗体时再次显示。



为Q1:我没有ideea,我只是以为我可以和Q2一样。



for Q2:我试图将登录表单对象发送到Dispaly表单以使用ShowDialog()方法。



在登录表单我隐藏表单并显示如下显示窗体:

  this.Hide(); 
显示cat = new Display(ConString,idp,this);
cat.ShowDialog();

in显示形式我尝试在退出时关闭对话框,并显示这样的登录表单

  private void Display_FormClosed(object sender,FormClosedEventArgs e)
{
this.Close();
this.l.ShowDialog();
}

其中l var是发送到Display构造函数的Login对象,类型为。
问题是显示窗体没有关闭,如果用户再次点击显示,将显示一个新的对话框,我想要最多1个显示窗体的实例。



谢谢

解决方案

Q1& Q2:在登录表单代码内:

  using(SettingsForm frm = new SettingsForm())
{
Hide();
frm.ShowDialog(this);
Show();
}

这是当窗体控制另一个窗体时通常会看到的。 ShowDialog 将停止父表单的选择,您将看到您的对话框闪烁,系统应该有噪音来表明这一点。



ShowDialog 也会阻塞,直到表单关闭,因此它将等到您关闭表单。您可以为生成另一个表单的任何表单重复此代码,并需要等待其使用。


i'm trying to make a small project that uses multiple forms (dialogs) for different states and ran in a few problems. My dialogs are Login, Settings and Display. When application is started Login form is displayed

Application.Run(new login());

from it the user can open Settings form or, if certain requirements are met, the Display form.

Q1: how do i make Login form unavailable to user when Settings form is opened (i want the user to complete the fields in the Settings form, then click 'save' button to exit, before he can do anything else in Login form)

Q2: how do i hide the Login form when user opens the Display form and show it again when user closes the Display form.

for Q1: i have no ideea, i just thought i could do the same as in Q2.

for Q2: i tried to send the Login form object to the Dispaly form to use ShowDialog() method.

in Login form i hide the form and show the Display form like this:

this.Hide();
Display cat = new Display(ConString, idp, this);
cat.ShowDialog();

in Display form i try to close the dialog on exit and show the Login form like this

private void Display_FormClosed(object sender, FormClosedEventArgs e)
{
    this.Close();
    this.l.ShowDialog();
}

where l var is the Login object sent to Display constructor, of the type Login. the problem is that Display form is not closing and if user clicks display again a new dialog will show and i want max 1 instance of Display form.

thanks

解决方案

Q1 & Q2: when inside the login form code:

using (SettingsForm frm = new SettingsForm())
{
   Hide();
   frm.ShowDialog(this);
   Show();
}

This is what you would usually see when a form is in control of another form. ShowDialog will stop the parent form from being selectable, you will see your dialog flash and the system should make a noise to indicate this.

ShowDialog also blocks until the form closes, so it will wait until you close the form anyway. You can repeat this code for any form that spawns another form and needs to wait for it to be used.

这篇关于在c#中使用多个表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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