How can I set topmost at the SaveFileDialog using C#?
Asked Answered
S

4

1

I want to set topmost my SaveFileDialog. But as you know there is no property. Is there any other way to set TopMost at SaveFileDialog?

Sanborn answered 12/1, 2011 at 8:18 Comment(2)
I guess you can make the calling Form topmost.Radborne
Why isn't your dialog already popping up to the top?Knitted
S
3
class ForegroundWindow : IWin32Window
{
    [DllImport("user32.dll")]
    public static extern IntPtr GetForegroundWindow();

    static ForegroundWindow obj = null;
    public static ForegroundWindow CurrentWindow { 
        get { 
           if (obj == null) 
                obj = new ForegroundWindow(); 
           return obj; 
        } 
    }
    public IntPtr Handle {
        get { return GetForegroundWindow(); }
    }
}

SaveFileDialog dlg=new SaveFileDialog();
dlg.ShowDialog(ForegroundWindow.CurrentWindow);
Solangesolano answered 29/4, 2011 at 4:33 Comment(0)
C
0

I can only think on a hack to do this. Make a new Form and set it TopMost. When you want to show the dialog, call from it:

Form1.cs

private void Form1_Load(object sender, EventArgs ev)
{
    var f2 = new Form2() { TopMost = true, Visible = false };
    var sv = new SaveFileDialog();

    MouseDown += (s, e) =>
    {
        var result = f2.ShowSave(sv);
    };
}

Form2.cs

public DialogResult ShowSave(SaveFileDialog saveFileDialog)
{
    return saveFileDialog.ShowDialog(this);
}
Cannelloni answered 12/1, 2011 at 10:39 Comment(2)
If you want on the top of a specific form use ShowDialog(form) msdn.microsoft.com/en-us/library/9a55b9ds.aspxCannelloni
I already used ShowDialog(IWin32Window). :( Actually I use WPF windows. And I got a tab control in that window. All the tabs have an window, and each window has a single process. So I got a problem :(Sanborn
S
0

I solved this ref Bruno's answer :)

My code is this...

public System.Windows.Forms.DialogResult ShowSave(System.Windows.Forms.SaveFileDialog saveFileDialog)
{
    System.Windows.Forms.DialogResult result = new System.Windows.Forms.DialogResult();

    Window win = new Window();
    win.ResizeMode = System.Windows.ResizeMode.NoResize;
    win.WindowStyle = System.Windows.WindowStyle.None;
    win.Topmost = true;
    win.Visibility = System.Windows.Visibility.Hidden;
    win.Owner = this.shell;

    win.Content = saveFileDialog;
    win.Loaded += (s, e) =>
    {
        result = saveFileDialog.ShowDialog();
    };
    win.ShowDialog();

    return result;
}
Sanborn answered 17/1, 2011 at 2:26 Comment(1)
Can you explain this line? win.Owner = this.shell;Catechetical
G
0

As a more generic WPF-ish for any type of FileDialog:

public static class ModalFileDialog
{
    /// <summary>
    /// Open this file dialog on top of all other windows
    /// </summary>
    /// <param name="fileDialog"></param>
    /// <returns></returns>
    public static bool? Show(Microsoft.Win32.FileDialog fileDialog)
    {
        Window win = new Window();
        win.ResizeMode = System.Windows.ResizeMode.NoResize;
        win.WindowStyle = System.Windows.WindowStyle.None;
        win.Topmost = true;
        win.Visibility = System.Windows.Visibility.Hidden;
        win.Content = fileDialog;

        bool? result = false;
        win.Loaded += (s, e) =>
        {
            result = fileDialog.ShowDialog();
        };
        win.ShowDialog();
        return result;
    }
} 
Gerianne answered 14/12, 2018 at 14:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.