Frage

I'm facing a small problem with my message boxes. If I run the program directly (as in, double clicking it and using it from there), they work fine.

I have some .ext files than when double clicked, run through the program (they are associated with my program). Now when message boxes run through there, they are shown, but they are minimized and I have to manually click them on the Task Bar to see them.

Does anyone know why this is? I have this code running in my Program.cs:

static void Main(string[] args)
{
    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);
    var MyForm = new Form1();
    if (args.Length != 0)
    {
        MyForm.RunMsg(); // this is a function I am calling
        Application.Exit();
    }
    else
        Application.Run(MyForm);
}

Any help would be appreciated. And yes I can confirm the message boxes are only minimized when running the .ext files.

War es hilfreich?

Lösung

I very seriously doubt they are actually minimized. Much more likely is that they are hidden behind the window of another application. Yes, that's likely in this scenario because you don't create a window right away. You probably crunch on the passed file for a while, then try to tell the user that you are done. Too late to still be able to acquire the focus, Windows has very strict rules about this to avoid the "throw a window in the user's face" syndrome. Not just annoying, it is also likely to fail because the user might accidentally close the window while mousing or keyboarding without even noticing that there was a window. If you ever accidentally started a Windows Update install then you know what I mean (now fixed).

Don't use a message box. Either create a progress form so that you can acquire the focus and show progress or use a NotifyIcon.

Andere Tipps

A better approach may be to add a new constructor to your form which accepts an argument or arguments which represent the values passed in the command line, as follows:-

static void Main(string[] args)
{
    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);
    var parsedArgs = Parse(args);
    Application.Run(new Form1(parsedArgs));
}

For the purpose of the example, Parse() is a static method which, given the command line arguments, returns some kind of appropriate type. If there are no command line arguments supplied, the return type can either be null, or can be non-null with default values for it's properties.

Using this method, your form can decide how to act appropriately according to the argument(s) passed in the constructor, and you still get the benefit of the setup, lifetime and teardown which Application.Run() does for you.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top