请帮助关闭表格 [英] pls help how to close a form

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

问题描述

嘿,我有一个登录表单,所以当我点击登录以便它转到另一个表单,我的问题是,我希望一旦另一个表单打开就关闭登录表单。香港专业教育学院试过close();但是它关闭了两个窗口,以及如何获得第二个winform全屏,提前tnx

hey i have a login form so when i click login so that it goes to another form thats working my problem is that i want to have the login form close as soon as the other form opens. ive tried close(); but then it closes both windows,and how to get the 2nd winform fullscreen , tnx in advance

推荐答案

基本上至少有三种可能性

- login是启动表单,而不是另一个表单。从项目设置更改

- 登录表单拥有另一个表单,因此在登录关闭时关闭另一个表单。检查你是否在代码中定义了所有者。

- 在登录关闭期间执行关闭另一个表单的代码



To最大化表单使用 WindowState [ ^ ]属性
Basically there are at least three possibilities
- login is the startup form, not the other one. Change from project settings
- the login form owns the other for and because of this the other form is closed when login is closed. Check if you have defined owner somehwere in the code.
- during the login close you execute such code that closes the other form

To maximize the form use WindowState[^] property


如果您的登录表单是项目的启动表单,那么当它关闭时,您的应用程序也会这样做 - 所以它打开的任何其他表单也将关闭。

处理此问题的最佳方法可能非常简单 - 当您的用户正确输入登录详细信息时:

If your login form is the startup form from your project, then when it closes, your application does as well - so any other forms it opens will also be closed.
Probably the best way to handle this is pretty simple - when your user correctly enters the login details:
MainForm mf = new MainForm();
Hide();
mf.ShowDialog();
Close();

这样,代码将停止,直到邮件表单关闭,但登录表单将不再可见。登录表单(和您的应用程序)将在您的主表单关闭时关闭。



好的,现在尝试该代码,请参阅第二种形式的cos我有一个切换用户按钮,重新打开登录表单





这也很容易处理:

您可以从主表单设置一个返回值,并在登录表单中通过DialogResult或主表单中的NewUserbool进行检查。

例如:

This way, the code will stop until the mail form is closed, but the login form will no longer be visible. The login form (and your app) will close when your main form does.

"okay uhm il try that code now, see cos in the 2nd form ill be having a switch user button which re opens the login form"


That's also easy to handle:
You can set a return value from the main form and inspect that in the login form, either via the DialogResult or via a "NewUser" bool in your main form.
For example:

MainForm mf = new MainForm();
Hide();
mf.ShowDialog();
if (mf.NewUser)
   {
   Show();
   }
else
   {
   Close();
   }



使用DialogResult是相同的:


Using the DialogResult is the same:

MainForm mf = new MainForm();
Hide();
if (mf.ShowDialog() == DialogResult.OK)
   {
   Show();
   }
else
   {
   Close();
   }



您设置切换用户按钮以使DialogResult为OK,它将关闭表单并返回OK到您的登录表单。



现在它没有重新打开登录表格-_- ugh ima noob





好​​的 - 回去一步。

这是你的SwitchUser按钮处理程序中的代码,是吗?

最简单的方法是手动开始。

如果你想再次显示登录表单,那么设置主窗体DialogResult:


And you set the "Switch User" button to have the DialogResult of OK and it'll close the form and return OK to your login form.

"now it doesnt re open the login form -_- ugh ima noob"


OK - go back a step.
This is the code in your "SwitchUser" button handler, yes?
The simplest way to do this is to do it manually to start with.
If you want to show the login form again, then set the main form DialogResult:

private void SwitchUserButton_Click(object sender, EventArgs e)
    {
    if (MessageBox.Show("Are You Sure To Exit ?", "Exit", MessageBoxButtons.YesNo) == DialogResult.Yes)
        {
        DialogResult = DialogResult.OK;
        }
    }

如果按是,主表格将关闭,它将返回OK。

如果你按否,它不会关闭或返回任何东西。

如果您以任何其他方式关闭主窗体,它将返回取消

(您可以按钮做它通过设置属性自动进行,但如果用户说否则必须取消它

The main form will close if you press "yes" and it will return OK.
If you press no, it won't close or return anything.
If you close the main form in any other way, it will return "Cancel"
(You can make the button do it automatically by setting the properties, but then you have to "cancel" it if the user says "No")


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

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