简体   繁体   中英

How do I hide or unhide a form in C#

I thought this would have been a legal statement but obviously not How do I hide or unhide a form based on this?

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

to show or hide a Windows Form, you use the Show() or Hide() methods, like this: searchform.Show(); or searchform.Hide();

You might want to consider this code:

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

Okay, my code for Form1 which has a button that shows 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();
        }
    }
}

and on Form2, I put a textbox with no events (but its text is remembered between hiding and showing Form2), and it has a button that hides its form. here's the code for 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();
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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