C# 如何在C中隐藏或取消隐藏窗体#

C# 如何在C中隐藏或取消隐藏窗体#,c#,winforms,C#,Winforms,我原以为这是一个法律声明,但显然不是我如何隐藏或取消隐藏基于此的表单 TrainingEventAddTraineesSearchForm searchform = new TrainingEventAddTraineesSearchForm(context); if (searchform == null) searchform.ShowDialog(); else searchform.Visible = true; 要显示或隐藏Windows窗体,请使用show()或hi

我原以为这是一个法律声明,但显然不是我如何隐藏或取消隐藏基于此的表单

TrainingEventAddTraineesSearchForm searchform = new TrainingEventAddTraineesSearchForm(context);
if (searchform == null)
    searchform.ShowDialog();
else
    searchform.Visible = true;

要显示或隐藏Windows窗体,请使用show()或hide()方法,如:
searchform.show()
searchform.Hide()

你可能想考虑这个代码:

TrainingEventAddTraineesSearchForm searchform = new TrainingEventAddTraineesSearchForm(context);
if (searchform.Visible == false)
{    searchform.Show();   }
else
{    searchform.Hide();   }

要显示或隐藏Windows窗体,请使用show()或hide()方法,如:
searchform.show()
searchform.Hide()

你可能想考虑这个代码:

TrainingEventAddTraineesSearchForm searchform = new TrainingEventAddTraineesSearchForm(context);
if (searchform.Visible == false)
{    searchform.Show();   }
else
{    searchform.Hide();   }

好的,我的Form1代码有一个显示Form2的按钮:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        Form2 f2 = null;

        public Form1()
        {
            InitializeComponent();
        }

        private void btnShowForm2_Click(object sender, EventArgs e)
        {
            if (f2 == null) { f2 = new Form2(); }
            f2.Show();
        }
    }
}
在Form2上,我放置了一个没有事件的文本框(但它的文本在隐藏和显示Form2之间被记住),并且它有一个隐藏其表单的按钮。以下是Form2的代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        private void btnHideMe_Click(object sender, EventArgs e)
        {
            this.Hide();
        }
    }
}

好的,我的Form1代码有一个显示Form2的按钮:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        Form2 f2 = null;

        public Form1()
        {
            InitializeComponent();
        }

        private void btnShowForm2_Click(object sender, EventArgs e)
        {
            if (f2 == null) { f2 = new Form2(); }
            f2.Show();
        }
    }
}
在Form2上,我放置了一个没有事件的文本框(但它的文本在隐藏和显示Form2之间被记住),并且它有一个隐藏其表单的按钮。以下是Form2的代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        private void btnHideMe_Click(object sender, EventArgs e)
        {
            this.Hide();
        }
    }
}


如果您在if语句上方实例化searchform,它将永远不会为null。即使它为null,您也会得到一个NullPointerException,试图在它为null时显示它。有什么错误或不起作用?那么您希望发生什么呢?您可以调用formName.Hide()来隐藏表单。我认为您可以简单地使用.Hide()和.Show()方法,仅此而已。如果您在if语句上方实例化searchform,它将永远不会为空。即使它是空的,您也会得到一个NullPointerException,当它为空时尝试显示它。有什么错误或不起作用?你希望发生什么呢?你可以调用formName.Hide()来隐藏一个表单。我认为你可以简单地使用.Hide()和.Show()方法,仅此而已。我正在调用表单,然后在第二个表单的关闭按钮上调用Hide()我会用这种方式调用它,表单会记住控件的值。如果您希望第二个表单隐藏自己,您可能希望捕获Closing()事件,而不是Close()事件。然后在Closing()事件中,只需隐藏表单并取消关闭事件(使用
e.cancel=true
,其中“e”是
FormClosingEventArgs
变量。这会阻止它真正关闭,但只会隐藏它。然后当你重新显示它时,它应该记住所有内容。这是我尝试的第一件事。:)哦,前面的评论假设您试图在关闭事件中隐藏它。没有关系。如果你只是通过点击按钮来隐藏它,那么我认为它也会记住控制值,比如隐藏表单上文本框中的文本-(我在子窗体上单击搜索时关闭它隐藏了子窗体,但是我想重新加载在按钮单击时隐藏的相同窗体?啊,但是当您声明
TrainingEventAddTrainesseArchForm searchform
时,我想您是在button事件方法内创建了searchform变量?这会导致问题不记得它。它需要在主窗体中,但在您的按钮事件方法之外。我正在调用窗体,然后在第二个窗体的关闭按钮上调用hide(),我会用这种方式调用它。窗体将记住控件的值如果您希望第二个窗体隐藏自己,您可能希望捕获关闭()事件而不是Close()事件。然后在Closing()事件上,只需隐藏表单并取消Closing事件(使用
e.Cancel=true
时,其中'e'是
FormClosingEventArgs
变量。这会阻止它真正关闭,但只是隐藏它。然后当你重新显示它时,它应该记住所有内容。这是我尝试的第一件事。:)哦,前面的评论假设您试图在关闭事件中隐藏它。没关系。如果您只是在单击按钮时隐藏它,那么我假设它也会记住控件值,比如隐藏窗体上文本框中的文本-(我在子窗体上单击搜索时关闭它隐藏了子窗体,但是我想重新加载在按钮单击时隐藏的相同窗体?啊,但是当您声明
TrainingEventAddTrainesseArchForm searchform
时,我想您是在button事件方法内创建了searchform变量?这会导致问题不记得了。它需要在主窗体中,但在您的按钮事件方法之外。@Edaward,但正如您看到的,我正在将我的实体上下文传递给TrainingEventAddTraineeSearchForm searchform=new TrainingEventAddTraineeSearchForm(上下文);我需要能够传递在表单加载句柄中声明的上下文对象好的,我修改了代码以在button事件中创建表单。但是请注意,我必须在button方法之外声明我的
f2
变量,这样它就不会被遗忘。因此,在测试它是否为null时,您与原始版本非常接近。:)但是,您需要在表单范围内声明变量,而不是在方法内。我的代码向您展示了有关如何执行此操作的完整源代码。:)快乐编码,@DavidB!这是我在粘贴箱中的完整代码,向您展示我所遇到的情况。对不起,我在防火墙后面,目前无法访问该防火墙。我的休息时间已经结束。我将尝试今晚在家查看它,看看您是否解决了问题,如果没有,我将尝试从您的代码中查看您需要的内容。:)@Edaward,但正如您在这里看到的,我将实体上下文传递给TrainingEventAddTraineeSearchForm searchform=new TrainingEventAddTraineeSearchForm(上下文);我需要能够传递在表单加载句柄中声明的上下文对象好的,我修改了代码以在button事件中创建表单。但是请注意,我必须在button方法之外声明我的
f2
变量,这样它就不会被遗忘。因此,在测试它是否为null时,您与原始版本非常接近。:)但是您需要在表单范围内声明变量,而不是在方法内。“我的代码”向您展示了如何完成此操作的完整源代码。:)