Dumbest question ever; starting up a form as invisible.

wyrd

Senior Contributor
Joined
Aug 23, 2002
Messages
1,405
Location
California
For the life of me I cannot figure out how to start up the main application form as invisible. There has to be something I'm missing, because I cannot imagine this being a difficult thing to do. :P
 
I got around this once by setting the WindowState to Minimize and not showing the form in the taskbar, then of course putting the values back when the form became active.
 
what is it you are trying to do???

are you trying to do some processing before the form is actually visible and you want that processing to complete before the form is visible and if the pre-visible process doesnt complete just exit?

if so, put your process code in the Main method before the call to Application.Run

C# code:

Code:
public class Form1 : System.Windows.Forms.Form
{
   static bool PreProcess()
   {
    // Do something here -  keep in mind this is a static method 
    //so you cant refer to instance variables
   }

[STAThread]
   static void Main() 
   {
     if (Form1.PreProcess())
        Application.Run(new Form1());
   }
}

Do you need PreProcess to initialize some variables to be used by the form?

Code:
public class FormVar : object{};

public class Form1 : System.Windows.Forms.Form
{
   static bool PreProcess(FormVar vars)
   {
    // Do something to vars here  
   }

   public Form1(FormVar vars):base()
  {
     // Use vars to initialize the form;

  }


[STAThread]
   static void Main() 
   {
     FormVar vars =  new FormVar();  
     if (Form1.PreProcess(vars))
        Application.Run(new Form1(vars));
   }
}

perhaps a little more information is in order?

Joe Mamma
 
Can you start in a module using Sub Main? Then you can do your processing and display the form when you are ready...?
 
I thought my question was pretty straight forward; I don't want the form to be visible on start up.

Setting this.Visible = false in the constructor of the form does no good (you'd think there'd be some sort of Form property to set this too, but there isn't).

I'm using a NotifyIcon in the system tray, and setting the form so ShowInTaskbar = false. I do not want the form to be visible until the user clicks on the NotifyIcon.

sjn: Not an option. By doing that it shows the minimized box in the lower left of the screen. Ick.

sam: That's probably the only option I have.
 
When minimized it doesn't show it on my screen so thats why I suggested it. I know what you mean *Ick*...have seen it many times in programs and its ugly.
 
What if you minimise it and then set the show in task bar to false.

ugh. Thats pretty gross though.

have you tryed using me.hide in the load event?
 
How about sizing the form to (0, 0), setting the shown in task bar to false, and setting the start-up position to (-10, -10), I do this on something and there isn't so much as a flicker of a form being there until they hit my notify icon where via code I move and resize and show in task bar. Ugly, yes, but it works.
 
If you don't want a form shown until the user clicks an option from the notify icon you can do this:
C#:
NotifyIcon ico = new NotifyIcon(); //create the icon
ico.ContextMenu = new ContextMenu(); //add some context menu to it
ico.Icon = new Icon(Application.StartupPath + @"\App.ico"); //set the icon
ico.Visible = true; //and finally make it visible
Application.Run(); //start a message loop for your application without a form
//so the app doesnt exit when the Main is done.
//Then of course you add some handlers and such to make your form appear
Then you will have to manually terminate the application using Application.Exit since no form is associated with the message loop.
 
mutant said:
If you don't want a form shown until the user clicks an option from the notify icon you can do this:
C#:
NotifyIcon ico = new NotifyIcon(); //create the icon
ico.ContextMenu = new ContextMenu(); //add some context menu to it
ico.Icon = new Icon(Application.StartupPath + @"\App.ico"); //set the icon
ico.Visible = true; //and finally make it visible
Application.Run(); //start a message loop for your application without a form
//so the app doesnt exit when the Main is done.
//Then of course you add some handlers and such to make your form appear
Then you will have to manually terminate the application using Application.Exit since no form is associated with the message loop.


Hmm. . . I may be missing something but

Main is a static method, you have no form to add yet.
Application.Run() is an infinite loop.
How do you add the window handle of a non existent form to the Apps winproc queue while in the run loop?

i.e.
Code:
   Application.Run();
//  Program will not get here until Exit() is called
    PostProcess();
Yes, you can call a method in Main as long as the method one of the following (I am assuming you weren't being sarcastic) :
  1. Static (shared in VB, Another reason VB sucks - non standard OOP constructs)
  2. a Constructor
  3. a Method of an instanced variable
    [/list=1]

    The reason you can't call hide in the load event is because the last thing Run does after it triggers the OnLoad event is call Form.Visible = true; (why I like Delphi is you have the source code to actually trace into the internals to see what happens)

    Again, why do you want to keep the form hidden?
    What happens while the form is hidden?
    What state change must occur for the form to become visible?
    the reason I ask is that the problem domain will dictate how you want to implement the solution.

    I have a feeling you will need to declare an EventHandler in your form, lets call it OnAppIdle.

    In Main, you will instance your form (visible = false), add OnAppIdle to the Application.Idle event list and then call Application.Run();

    In your OnAppIdle, create the NotifyIcon that will manage the form state and then remove OnAppIdle from the Application.Idle Event list

    Boy, this was so easy to do in Delphi :) Its easy in C#, just not quite as straight forward, On the bright side, the .NET framework offers little bit more freedom. Doing it in VB.NET is probably confusing if you haven't much experience in Events/Delegates

    I am beginning to feel like Cato. Insead of
    '. . .and Carthage must burn'
    at the end of my diatribes I will add
    '. . . and VB plain sucks!!!'

    smile g-d dammit!!! I'm kidding!!!

    Joe Mamma
 
Uhh, anyway...

Code:
// In app main entry point.

YourForm form = new YourForm();
form.Visible = false;
Application.Run();
 
Wyrd's latest solution post causes the form to flash for an instant on my machine.

What I do is add a 'Component' to my application and set its 'main' method as the project startup. Then I can put my NotifyIcon and Menu on this guy and let menu clicks show the form when needed.

This way, you never have anything flash on the screen.
 
Hmm, interesting. Nothing ever flashes on my screen? And I'm only running an 800mhz athlon. Also, isn't a form created as hidden by default? (the form.visible = false should be unnecessary).

I looked at Jay1b's code, his solution is identical to mine (unless I missed something).
 
Restarting???

wyrd said:
Hmm, interesting. Nothing ever flashes on my screen? And I'm only running an 800mhz athlon. Also, isn't a form created as hidden by default? (the form.visible = false should be unnecessary).

I looked at Jay1b's code, his solution is identical to mine (unless I missed something).

The program prevents the computer from restarting. Any idea?
 
Back
Top