Is this a safe way to hide the main form?

P

Peter Steele

I'm writing a tray application and I of course want to hide the main form
when the appstarts. I've experimented with a couple of techniques to do
this, including setting Opacity to 0, but this seems a bit kludgy. I ended
up doing this:

static void Main()
{
new MyAppForm();
Application.Run();
}

This differs from a normal Main by calling the void version of the Run
method and moving the new call out as a separate statement. I haven't been
able to find any good description as to whether this is a safe thing to do.
It seems to work fine and my service behavs as expected, responding properly
to its context menu accesses. I like this better than setting Opacity to 0.
Another solution I experiented with was to use the normal Run call:

Application.Run(new MyAppForm());

and in the constructor of the main form call Hide(), but for some reason
this doesn't work. So, unless someone has a better solution or tells me that
I shouldn't the void Run call, I'll stick with doing it this way.

Peter Steele
CIENA Corp.
 
V

Vijaye Raji

So, if you're not ever going to show your main form, why don't you create a
console application? Or why don't you get rid of your main form altogether?

-vJ
 
M

Michael Bray

Peter Steele said:
I'm writing a tray application and I of course want to hide the main
form when the appstarts. I've experimented with a couple of techniques
to do this, including setting Opacity to 0, but this seems a bit
kludgy. I ended up doing this:

My method was to set the main form properties:

ShowInTaskBar = False
WindowState = Minimized

-mdb
 
D

DalePres

Do you ever have a problem restoring the form after hiding it that way? I
have some apps that have worked great for a year that suddenly have begun
crashing, on new machines, when I set ShowInTaskBar = true and WindowState =
WindowState.Normal.

I get an error that the window could not be created. Weird, since it was
created and displayed before I hid it.

Dale
 
M

Michael Bray

Do you ever have a problem restoring the form after hiding it that
way? I have some apps that have worked great for a year that suddenly
have begun crashing, on new machines, when I set ShowInTaskBar = true
and WindowState = WindowState.Normal.

I get an error that the window could not be created. Weird, since it
was created and displayed before I hid it.

Not seen that problem myself... But if I remember correctly I usually just
use Show() and Hide() during execution of the program, instead of changing
WindowState, which I only set in the designer for form startup. I think I
also change ShowInTaskBar after calling Show() to prevent the form from
going behind another window with no way to get at it other than minimizing
or moving the other window.

-mdb
 
P

Peter Steele

This doesn't work for me. The task doesn't show up in the taskbar, but what
I do see is a minimized window that parks itself just above the Start
button. So instead of using WindowState I use Opacity instead and set it to
0. This works, but like I said in my original posting it just doesn't feel
right.
 

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

Similar Threads


Top