Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Windows not opening on top

Tags:

c#

winforms

Relates to my previous question.

In my previous question I asked why my dialogs seem to be opening behind other windows. (Other windows not necessarily belonging to my program, e.g. Excel, Windows Explorer, etc.)

I was told to use the overloaded ShowDialog() and pass the parent as a parameter.

That's fine, and I've replaced all the ShowDialog()s with the overload.

However I still had the same problem here:

static class Program
{
  /// <summary>
  /// The main entry point for the application.
  /// </summary>
  [STAThread]
  static void Main()
  {
    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);

    OpenFileDialog ofd = new OpenFileDialog();
    ofd.ShowDialog(); // First dialog

    // Do some stuff here...

    SaveFileDialog sfd = new SaveFileDialog();
    sfd.ShowDialog(); // Second dialog;
  }
}

In this example (don't comment on the code - I just wrote it up as a simple example), the user double-clicks the executable with several windows on their screen. The OFD appears on top, and the SFD appears underneath everything.

I usually have this sort of OFD/SFD set up in my simple programs which accept CSV or XLS files and do some simple processing of them. In this case, I put the OFD code in the static Main() method and load the dialog if no parameters were passed to the exe.

like image 631
Ozzah Avatar asked May 30 '11 02:05

Ozzah


2 Answers

There is an other - I think the easiest - solution:

        ofd.ShowDialog(new Form() { TopMost=true });

It opens a new form, that will be immediately disposed, but it will give TopMost property to your ofd. You can give more properties like "TopLevel":

        ofd.ShowDialog(new Form() { TopMost=true, TopLevel=true });
like image 110
etveszprem Avatar answered Sep 24 '22 23:09

etveszprem


c# console application folder browser dialog TopMost c# console application open file dialog TopMost

    [STAThread]
    static void Main(string[] args)
    {
        var threadFolderBrowserDialog = new Thread(voidFolderBrowserDialog);
        threadFolderBrowserDialog.IsBackground = true;
        threadFolderBrowserDialog.SetApartmentState(ApartmentState.STA);
        threadFolderBrowserDialog.Start();
        Console.WriteLine("Запуск выбора папки и файла в новом потоке");

        bool Exit = false;
        while (!Exit)
        {
            var exit = Console.ReadLine() == "exit" ? Exit = true : Exit = false;
            Console.WriteLine("Выход из программы по команде exit");
        }
    }
    [STAThread]
    static void voidFolderBrowserDialog()
    {
        FolderBrowserDialog fbd = new FolderBrowserDialog();
        if (fbd.ShowDialog(new Form() { TopMost = true, WindowState = FormWindowState.Minimized }) == DialogResult.OK)
        {
            Console.WriteLine(fbd.SelectedPath);
        }
        OpenFileDialog ofd = new OpenFileDialog();
        if (ofd.ShowDialog(new Form() { TopMost = true, WindowState = FormWindowState.Minimized }) == DialogResult.OK)
        {
            Console.WriteLine(ofd.FileName);
        }
    }
like image 28
Rynosce Avatar answered Sep 21 '22 23:09

Rynosce