简体   繁体   English

C# - 通过十字架关闭Form2后无法看到Form1

[英]C# - Unable to see Form1 after closing Form2 via the cross

I'm fairly new at C#, and have spent the whole day to find a soultion to this to address the issue I'm having so apologies if this is simple, or has been wlsewhere! 我在C#上相当新,并花了一整天的时间来找到一个灵魂来解决这个问题,如果这很简单,或者已经在这里,我会道歉! I have two Forms, Main and Config. 我有两个表单,Main和Config。 Main loads, and has a button to open the Config form and then hide itself as so: 主要负载,并有一个按钮打开配置表单然后隐藏自己:

private void btnConfig_Click(object sender, EventArgs e)
{
    Config config = new Config();
    config.Show(); 
    this.Visible = false;
}

It works just fine and the Config form opens. 它工作正常,Config表单打开。 The Config from has two buttons (a Save and a Close button). Config from有两个按钮(保存和关闭按钮)。 In essence, both buttons have the same end effect, close the Config form and re-show the Main form, coded as so: 本质上,两个按钮具有相同的结束效果,关闭Config表单并重新显示Main表单,编码如下:

    private void btnClose_Click(object sender, EventArgs e)
    {
        this.Hide();
        Main main = new Main();
        main.Visible = true;
    }      

The issue is that if I close the Config screen using the 'cross', Config is 'closed' but Main is not displayed and the programcontinues to run. 问题是,如果我使用'cross'关闭Config屏幕,Config将'关闭',但Main不会显示,程序将继续运行。 I've tried using OnFormClosing and got stuck in an awful loop! 我尝试过使用OnFormClosing并陷入了糟糕的循环! The Cross should do the same as the Save/Close buttons, hide the Config form and open Main again. Cross应该与Save / Close按钮相同,隐藏Config表单并再次打开Main。

If you want to show the config Form and reshow the Main form when the config has changed you normally would want something as a Config Form to show Modal . 如果你想在配置发生变化时显示配置表格并重新显示主表单,你通常会想要一些配置表格来显示模态

To show a Modal Form, you'll to use ShowDialog() . 要显示模态表单,您将使用ShowDialog() ShowDialog() is also a blocking call, which gives you the ability to pick up where you left of. ShowDialog()也是一个阻止调用,它使您能够选择离开的位置。

In the mainform: 在主要形式中:

private void btnConfig_Click(object sender, EventArgs e)
{
    var config = new Config(); 

    this.Visible = false;
    // this call blocks!
    var dialogResult = config.ShowDialog();

    // when the configform is closed, the code resumes here
    this.Visible = true;
}

This also gives you the possibitlity, to cancel the config form, by setting DialogResult property of the Config form to DialogResult.Cancel just before closing. 这也为您提供了possibitlity,取消其配置的形式,通过设置DialogResult的配置形式的财产DialogResult.Cancel刚刚闭幕前。

I'm not too sure what you're looking for exactly but this is how I would go about it. 我不太确定你到底在寻找什么,但这就是我要去做的事情。

Change your function to the following and have a private Config object under your main form class. 将您的函数更改为以下内容,并在主窗体类下面有一个私有的Config对象。

private Config config;
private void btnConfig_Click(object sender, EventArgs e)
{
    if(config==null || config.IsDisposed)
    {
        config = new Config(this); // Pass the main form into the constructor of config
    }
    config.ShowDialog(); 
    this.Visible = false;
}

Create a constructor (for Config) that takes in the main form as a parameter and have the main form as a private object. 创建一个构造函数(用于Config),它将主窗体作为参数接收,并将主窗体作为私有对象。 This allows you to show the original form as apposed to just creating a new one every time. 这使您可以将原始表单显示为每次只创建一个新表单。

private Form mainForm;
public Config(Form main)
{
    mainForm = main;
}

And change your close action. 并改变你的近距离行动。

private void btnClose_Click(object sender, EventArgs e)
{
    this.Hide();
    mainForm.Visible = true;
} 

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

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