Check if Form exists

  • Thread starter Thread starter News Microsoft
  • Start date Start date
N

News Microsoft

Hi there.

I would like to know how can I test if a Form exists.

This is the situation: I have a single Form in my application. My objective
is, when I minimize the form, it will disapear and a TrayIcon apears in the
task bar. When I click that icon, the form apears again.

The problem is that when I click the icon more than once, the form apears
serveral times (several instances).

How can I resolve this?

Thanks in advance,

Marco
 
When you minimize to the tray, do you close or hide the form?

Perhaps it would also help if you posted code snippets of the
closing/showing/hiding/renewing of the form.

-Jeroen
 
Does the existing form reappear, or do you create a new instance of the
form?

Typically, you would just but the toolbar component on a form, and then
Hide() the current form. In the click event on the tray-icon, you just
Show() the current form, since your event handler will normally be
running in the context of that form... you shouldn't need a "new" in
the event handler...

Marc
 
Hi again.

Right now, all I do is:

***********************
private void Form1_Deactivate(object sender, EventArgs e)
{
this.Hide();
notifyIcon1.Visible = true;
}

private void Form1_Activated(object sender, EventArgs e)
{
notifyIcon1.Visible = false;
}

private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
{
x.Show();
}
***********************
The trayIcon visible property is set to false-

What happens when I run this code?
1: The form disapear and the icon appears;
2: when I click the icon, the form appears, but the icon stills visible;
3: Therefore, when I minimize the form again, another icon appears :(

Theres another problem.. if the form just loose its focus, it disappears. I
would like it to disappear only when I minimize it.

Thanks!..
 
What is x? I would have expected "this"... since "this" is almost
certainly the form you are interested in.

Re hiding when losing focus... well, you did tell it to! (Deactivate)
If you want it to respond to minimise, then hook the resize event as
below

Marc

[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
using (Form form = new Form())
using (NotifyIcon icon = new NotifyIcon())
{
icon.Icon = SystemIcons.Question;
icon.Click += delegate {
form.WindowState = FormWindowState.Normal;
form.Show();
form.WindowState = FormWindowState.Normal;
form.Activate();
form.BringToFront();
icon.Visible = false;
};
form.SizeChanged += delegate
{
if (form.WindowState == FormWindowState.Minimized)
{
icon.Visible = true;
form.Hide();
}
};
Application.Run(form);
}

}
 
Hi Marco,

The Activated and Deactivated events do not correspond with the minimized and
maximized window states. Those events correspond to changes in the focus of
the window.

Try the following code instead:

private bool isFormHidden;

// constructor
public Form1()
{
InitializeComponent();

Resize += new EventHandler(Form1_Resize);
notifyIcon1.Click += new EventHandler(notifyIcon1_MouseDoubleClick);
}

private void Form1_Resize(object sender, EventArgs e)
{
if (this.WindowState == FormWindowState.Minimized)
{
notifyIcon1.Visible = true;
Hide();

isFormHidden = true;
}
else if (isFormHidden)
{
if (!this.Visible)
// this ensures that the size of the Form wasn't changed
// while it was hidden
return;

isFormHidden = false;
notifyIcon1.Visible = false;
}
}

private void notifyIcon1_MouseDoubleClick(object sender, EventArgs e)
{
this.Show();
this.WindowState = FormWindowState.Normal;
}
 
Sorry...

I use to have: Form1 x =new Form1();

The code that you sent should do what I intend to do?

Thanks,

Marco
 
No; you already have a Form1, because that is where your code
(including handlers) is living. So to answer the OP subject "Check if
Form exists"... "I handle, therefore I am".

You know the form exist, because the code that is executing *is in it*,
so just show yourself, which is where "this" comes in. Dave posted a
good example of this. My code could be more confusing, as since I
haven't inherited from Form, I can't use "this"... I am therefore using
my form reference. But the concept is the same.

Marc
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top