Reputation: 3980
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;
Upvotes: 1
Views: 5168
Reputation: 1255
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();
}
}
}
Upvotes: 4
Reputation: 1255
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(); }
Upvotes: 7