Jump to content
Xtreme .Net Talk

Recommended Posts

Posted
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
Gamer extraordinaire. Programmer wannabe.
Posted
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.
Posted

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:

 

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?

 


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

Joe Mamma

Amendment 4: The right of the people to be secure in their persons, houses, papers, and effects, against unreasonable searches and seizures, shall not be violated, and no warrants shall issue, but upon probable cause, supported by oath or affirmation, and particularly describing the place to be searched, and the persons or things to be seized.

Amendment 9: The enumeration in the Constitution, of certain rights, shall not be construed to deny or disparage others retained by the people.

Posted

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.

Gamer extraordinaire. Programmer wannabe.
Posted
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.
Posted
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.
  • *Experts*
Posted

If you don't want a form shown until the user clicks an option from the notify icon you can do this:

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.

Posted
Ah, that was the key; Application.Run() doesn't have to be passed a form. Thanks.
Gamer extraordinaire. Programmer wannabe.
Posted
If you don't want a form shown until the user clicks an option from the notify icon you can do this:

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.

  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) :

[*]Static (shared in VB, Another reason VB sucks - non standard OOP constructs)

[*]a Constructor

[*]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

Joe Mamma

Amendment 4: The right of the people to be secure in their persons, houses, papers, and effects, against unreasonable searches and seizures, shall not be violated, and no warrants shall issue, but upon probable cause, supported by oath or affirmation, and particularly describing the place to be searched, and the persons or things to be seized.

Amendment 9: The enumeration in the Constitution, of certain rights, shall not be construed to deny or disparage others retained by the people.

Posted

Uhh, anyway...

 

// In app main entry point.

YourForm form = new YourForm();
form.Visible = false;
Application.Run();

Gamer extraordinaire. Programmer wannabe.
Posted

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.

Posted

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).

Gamer extraordinaire. Programmer wannabe.
  • 1 year later...
Posted

Restarting???

 

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?

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...