473,387 Members | 1,463 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Check if Form exists

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
Nov 14 '06 #1
8 4158
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
News Microsoft wrote:
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
Nov 14 '06 #2
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

Nov 14 '06 #3
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!..
"Jeroen" <me******@gmail.comwrote in message
news:11**********************@e3g2000cwe.googlegro ups.com...
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
News Microsoft wrote:
>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

Nov 14 '06 #4
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(fals e);
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);
}

}

Nov 14 '06 #5
Sorry...

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

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

Thanks,

Marco

"Marc Gravell" <ma**********@gmail.comwrote in message
news:11**********************@i42g2000cwa.googlegr oups.com...
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(fals e);
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);
}

}

Nov 14 '06 #6
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;
}

--
Dave Sexton

"Marco Pais" <marco.pais@[IGNORE]gmail.comwrote in message
news:O6*************@TK2MSFTNGP02.phx.gbl...
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!..
"Jeroen" <me******@gmail.comwrote in message
news:11**********************@e3g2000cwe.googlegro ups.com...
>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
News Microsoft wrote:
>>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


Nov 14 '06 #7
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

Nov 14 '06 #8
Thanks to you all! It works fine!

Regards,

Marco

"Dave Sexton" <dave@jwa[remove.this]online.comwrote in message
news:uW**************@TK2MSFTNGP03.phx.gbl...
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;
}

--
Dave Sexton

"Marco Pais" <marco.pais@[IGNORE]gmail.comwrote in message
news:O6*************@TK2MSFTNGP02.phx.gbl...
>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!..
"Jeroen" <me******@gmail.comwrote in message
news:11**********************@e3g2000cwe.googlegr oups.com...
>>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
News Microsoft wrote:
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



Nov 14 '06 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Matt | last post by:
<input type="file" size=50"> will produce the browse button and browse text box. The user can either select the file from browse button, or enter a path in browse text box manually. My question...
2
by: Chris Windsor | last post by:
I hope the following describe what I'm trying to do: I have created a tool to be used by product analysts when studying different cell phone designs. Part of the tool is a set of 11 forms on a...
1
by: Onur Bozkurt | last post by:
Is there anyway to check if an e-mail address exists..... I mean someone entered a mail address in a form... I wan't to check if that e-mail reallly exists, doesn't matter if he/she really owns it....
5
by: sword | last post by:
How can I check whether a file exists?
14
by: Dan | last post by:
Hello, we have an intranet application using Windows Integrated Authentification. When an user starts the application, he gets a form for inputting data. The first time he does that, the...
3
by: pollygw | last post by:
I have a page that dynamically adds rows to a table and the user can also delete any of the rows in no specific order. When the form is submitted I need to do some validation. I can't loop through...
7
by: Jeff | last post by:
I need a way to do the following and cannot seem to find a solution via google. 1. Have a method from the main app to get all open forms 2. Check each open form for a public method 3. If this...
1
by: gurmet | last post by:
Hi All I have been looking around for help, and finally post this problem. I created a form to edit a record. Before i can click save button on the edit form i need to check if the data that...
2
by: qwedster | last post by:
Folk! How to programattically check if null value exists in database table (using stored procedure)? I know it's possble in the Query Analyzer (see last SQL query batch statements)? But how...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.