显示之前隐藏的相同表单 [英] show same form which was hidden before

查看:27
本文介绍了显示之前隐藏的相同表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的项目中,有两种形式frmLoginfrmMain.从 frmLogin 成功登录后,我通过执行以下操作向用户显示 frmMain 表单:

In my project, there are two forms frmLogin and frmMain. After successful login from frmLogin I am showing the frmMain form to the user by doing something like this:

frmLogin表单button_click事件中:

frmMain main = new frmMain();
main.Show();
this.Hide();

frmMain 中,当用户注销时,我想显示相同的 frmLogin 表单(不是实例).如何做到这一点?

In frmMain when the user logs out I want to show the same frmLogin form (not the instance). How to do this?

我尝试了这段代码:(创建另一个我不想要的 frmLogin 实例)

I tried this code: (creating another instance of frmLogin which I don't want)

frmMain表单中button_click事件:

if (MessageBox.Show("Do you really want to log out?", "Alert", MessageBoxButtons.YesNo).Equals(DialogResult.Yes))
{
    this.FormClosing -= frmMain_FormClosing;
    //
    Process p = new Process();
    p.StartInfo.FileName = Application.ExecutablePath;
    p.Start();
    //
    this.Dispose();
}

我也尝试过使用 internal 说明符,但没有用.

I have also tried using internal specifier but no use.

作为实习生,我不允许使用 Static 关键字和更改 program.cs.如果上述方法需要限制方法(我已经提到过),请建议我另一种方法.

As a trainee, I am not allowed to use Static keyword and altering program.cs. If the above approach requires restricted methods (which I have mentioned) then please suggest me an alternate approach.

推荐答案

您所要做的就是将登录页面指定为 要打开的 nextform 的所有者

All what you have to do is assign login page as owner of nextform to be opened

在您的登录页面中调用以下要打开 nextForm 的函数

In your login Page call following function where you want to open nextForm

void openNextForm()
{
    Form f2 = new YourForm();    
    f2.owner=this;
    f2.Show();
    this.Hide();
}

在你的 nextForm(例如 mainForm)中写下你的按钮点击

In your nextForm (e.g mainForm) write following aginst your button click

void ButtonLogOut_Click(object sender, EventArgs e)
{
     this.Owner.Show();
     this.Hide();
     this.Dispose();
}

这篇关于显示之前隐藏的相同表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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