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.
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 });
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);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With