Hide() vs setting WindowState = FormWindowState.Minimized

M

mac

Brand Newbie Alert!

I am working through the "Build a Program Now!" book by Microsoft Press.
The last chapter has a little app that should stay in the system tray when
the user clicks on the 'X' to close the form. It should only shutdown when
the user chooses an option from the context menu.

The book suggests writing code in the FormClosing event of the form "Main"
to modify the behavior of the 'X' menu on the form. I have reproduced the
code here:

private void Main_FormClosing(object sender, FormClosingEventArgs e)
{
if (e.CloseReason == CloseReason.UserClosing)
{
e.Cancel = true;
this.Hide(); // Book says to Hide() the form.

/*this.WindowState = FormWindowState.Minimized;
* I was inclined to set the WindowState instead. Is
* there a difference, and which approach is better?
*/
}
}


As I indicate in my comments, my first thought was to set the WindowState.
Which one makes more sense, and is there a difference?

Thanks,

Mac

P.S. Is it okay to send HTML in newsgroups, or is that bad form? I know it
makes the messages more readable, but are there a lot of front ends that can
only read plain text?
 
M

Marc Gravell

Which one makes more sense, and is there a difference?

Depends on a few things; if showing modally (ShowDialog()), then Hide()
will be the same as Close(), in that the dialog will die. Minimised
forms will show in the task-bar (if ShowInTaskbar == true); they may
appear differently in task manager too.

So it depends how you want it to behave.

Marc
 
L

Lau Lei Cheong

Setting WindowState to Minimized is like minimizing a form, there's
(usually) an icon on the taskbar that you can restore it. Hide() will make a
form invisible, so it just disappeared. They're just different concept.

See here for explaination of their behaviour:
http://msdn.microsoft.com/library/d...nterface/windowing/windows/windowfeatures.asp

For your case, minimize the window would be better.

mac said:
P.S. Is it okay to send HTML in newsgroups, or is that bad form? I know
it makes the messages more readable, but are there a lot of front ends
that can only read plain text?
It depends.

Not only the news client may not support HTML format, if the newsgroup is
subscribed by other Bulletin Board System, your post will be presented as
complete garbage to them.

So in general, I'll advise NOT to use HTML format when posting in
newsgroups, as you'll get better chance for questions to be answered.
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,
private void Main_FormClosing(object sender, FormClosingEventArgs e)
{
if (e.CloseReason == CloseReason.UserClosing)
{
e.Cancel = true;
this.Hide(); // Book says to Hide() the form.

/*this.WindowState = FormWindowState.Minimized;
* I was inclined to set the WindowState instead. Is
* there a difference, and which approach is better?
*/
}
}


As I indicate in my comments, my first thought was to set the WindowState.
Which one makes more sense, and is there a difference?

What is what you want to do?
Hide just hide the form, which is equivalent to set visible to false. In a
form it will dissapear from the taskbar
If you minimize it you still can see the form in the taskbar and an ALT+TAB
will allow you to navigate again to it.

Thanks,

Mac

P.S. Is it okay to send HTML in newsgroups, or is that bad form? I know
it makes the messages more readable, but are there a lot of front ends
that can only read plain text?

I think that almosteverybody can read HTML, but some pundits may says that
you should post in plain text.
 
M

mac

Thanks all for the help. I was having trouble seeing the difference until
Ignacio mentioned being able to get to minimized window via ALT TAB. I had
the ShowInTaskbar property set to False, but I didn't think about ALT TAB.

Hide() is what I need and I apprciate all the help.

Thanks,

Mac
 

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

Top