简体   繁体   English

如何在c#中的另一个表单中打开一个新窗口表单

[英]How to open a new window form within another form in c#

i have developed a windows form application using c#. 我用c#开发了一个Windows窗体应用程序。

it has 2 forms like login form and main form. 它有2种形式,如登录表单和主表单。 When i enter the correct login credentials it should close(not hide) the login form and display the main form. 当我输入正确的登录凭据时,它应该关闭(不隐藏)登录表单并显示主表单。

i used the following code 我使用以下代码

MainForm main=new MainForm();
this.hide();//close login form
main.show();//display main form

but when I close the main form using the cross mark(right upper corner) in the mdi container , the main form closes but the application is still running in the task manager. 但是当我使用mdi容器中十字标记(右上角)关闭主窗体时,主窗体关闭但应用程序仍在任务管理器中运行。

If I use the following code instead of the previous code, application will close before main form display. 如果我使用以下代码而不是以前的代码,应用程序将在主窗体显示之前关闭。

this.close()//close login form
main.show();//display main form

do i have to hide mdi container from the main form or is there any way to achieve this? 我是否必须从主窗体中隐藏mdi容器,或者有任何方法可以实现此目的吗? please help. 请帮忙。

Try like this: 试试这样:

this.Hide();
Main.ShowDialog();
this.Close();

First, you hide the login form. 首先,隐藏登录表单。 Next, you show the Main form dialog, but prevent the caller of "ShowDialog()" from continuing until the dialog is closed. 接下来,显示“主窗体”对话框,但阻止“ShowDialog()”的调用者继续,直到关闭对话框。 Last, once the dialog is closed, you close the login form, ending the application. 最后,关闭对话框后,关闭登录表单,结束应用程序。

The application is still running because you still have one form that is still alive but hidden. 应用程序仍在运行,因为您仍然有一个仍处于活动但隐藏的表单。

You can subscribe the Close event in MainForm and manually exit the application through Application.Exit() . 您可以在MainForm中订阅Close事件,并通过Application.Exit()手动退出应用程序

Another option is to make sure there is only one window alive: open MainForm in the handler of the LoginForm.Close event as described here: Windows Forms: Change application mainwindow at runtime 另一种选择是确保只有一个窗口存活:在LoginForm.Close事件的处理程序中打开MainForm ,如下所述: Windows窗体:在运行时更改应用程序主窗口

MyForm1 f = new MyForm1();
f.Close += OnOpenOverviewWin();
Application.ShutdownMode = ShutdownMode.OnLastWindowClose;
Application.Run(f);

void OnOpenOverviewWin()
{
  if (loginok)
  {
    MyOverViewForm f = new MyOverViewForm ();
    f.Show();
  }
}

You need to show main form before closing login form. 您需要在关闭登录表单之前显示主表单。 try this: 试试这个:

main.show();//display main form
this.close()//close login form

What I always do is : 我一直在做的是:

MainForm main=new MainForm();
Visible = false;
main.Show();

and when in my main form I set the form_closed event handler to Application.Exit(); 在我的主窗体中,我将form_closed事件处理程序设置为Application.Exit(); like this: 像这样:

private void main_FormClosed(object sender, FormClosedEventArgs e)
{
    Application.Exit();
}

so when the user click close the main all application stops 所以当用户点击关闭主要的所有应用程序停止

I think the accepted answer is incorrect to some extent. 我认为接受的答案在某种程度上是不正确的。 This is the actual answer the OP was looking for. 这是OP正在寻找的实际答案。 Sorry it took a year to get answered. 对不起,需要一年的时间才能得到解答。 Inside of your Main() method in your project's Program.cs file, copy and paste the following at the bottom: 在项目的Program.cs文件中的Main()方法内,复制并粘贴以下内容:

        Logon logonForm = new Logon();
        if(logonForm.ShowDialog() == DialogResult.OK)
        {
            Application.Run(new Portal());             
        }

Inside of your logonForm when the user is authenticated or has a successful login event set the form's DialogResult = DialogResult.OK Here is an example of this: 当用户通过身份验证或登录成功成功时,在logonForm内部设置表单的DialogResult = DialogResult.OK以下是此示例:

    private void logonButton_Click(object sender, EventArgs e)
    {
        string username = usernameTextBox.Text;
        string password = passwordTextBox.Text;

        if(logon(username, password))
        {
            MessageBox.Show("Logged On Successfully!", "Success",
                            MessageBoxButtons.OK, MessageBoxIcon.Information);
            this.DialogResult = System.Windows.Forms.DialogResult.OK;
        }
        else
        {
            MessageBox.Show(getFailureReason(), "Failure",
                            MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM