C # OpenFileDialog Modeless Possible

Is it possible to create / have a modeless .net OpenFileDialog. I have a user interface element in the main dialog box, which should always be accessible to the user.

+6
c # modal-dialog openfiledialog
Jan 25 '09 at 23:02
source share
4 answers

No, OpenFileDialog and SaveFileDialog are both derived from FileDialog , which is modal in nature, so (as far as I know) there is no way to create a non-modal version of any of them.

+5
Jan 25 '09 at 23:15
source share

You can create a stream and have an OpenFileDialog stream host. There is no synchronization in the sample code, but it works.

public partial class Form1 : Form { OFDThread ofdThread; public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { ofdThread = new OFDThread(); ofdThread.Show(); } } public class OFDThread { private Thread t; private DialogResult result; public OFDThread() { t = new Thread(new ParameterizedThreadStart(ShowOFD)); t.SetApartmentState(ApartmentState.STA); } public DialogResult DialogResult { get { return this.result; } } public void Show() { t.Start(this); } private void ShowOFD(object o) { OpenFileDialog ofd = new OpenFileDialog(); result = ofd.ShowDialog(); } } 

With this code, you can add something to trigger an event in the user interface thread (be careful when calling!) To know when they will be done. You can access the results of the dialog.

 DialogResult a = ofdThread.DialogResult 

from your user interface thread.

+6
Jul 23 '09 at 21:33
source share

I know that I'm a little late, but you can create a new form, borderless, transparent or outside the borders of the display, and show the file change dialog box in this window.

+1
Mar 22 '11 at 13:45
source share

This is an old post, but I spend 2 days on the result that I want to present here (with a "context" and a complete but simplified code) @ Joshua's answer worked for me (finally when I believe in .ConfigureAwait (true), see First code example). Perhaps I was able to write fewer lines based on the long MSDN Threading Model article that I still need to read again.

My context is WPF (basic MVVM), and I have to select a file to write some backup .CSV (from datagrid). I need the (member) ChooseFileFromExtension() function to be asynchronous with a non-blocking FileDialog

 class MainWindowExportToExcelCSV : ICommand { ... public async void Execute(object parameter) { var usr_ctrl = parameter as UserControl; MyFileDialog fd = new MyFileDialog(); const bool WhenIComeBackIStillNeedToAccessUIObjectAndThusINeedToRetrieveMyOriginalUIContext = true; string filename = await fd.ChooseFileFromExtension("CSV files (*.csv)|*.csv|All files (*.*)|*.*").ConfigureAwait( WhenIComeBackIStillNeedToAccessUIObjectAndThusINeedToRetrieveMyOriginalUIContext); Visual visual = (Visual)usr_ctrl.Content; for (int i = 0; i < VisualTreeHelper.GetChildrenCount(visual); i++) { //look for datagrid element } } } 

and class code MyFileDialog

 using Microsoft.Win32; ... class MyFileDialog { //https://msdn.microsoft.com/en-us/library/ms741870(v=vs.110).aspx //Article on Threading Model private delegate void OneArgStrDelegate(string str); private void MyExternalDialog(string extensions) { SaveFileDialog fd = new SaveFileDialog(); fd.Filter = extensions; fd.ShowDialog(); tcs.SetResult(fd.FileName); } private TaskCompletionSource<string> tcs; public Task<string> ChooseFileFromExtension(string file_ext) { //Cf Puppet Task in Async in C#5.0 by Alex Davies tcs = new TaskCompletionSource<string>(); OneArgStrDelegate fetcher = new OneArgStrDelegate(this.MyExternalDialog); fetcher.BeginInvoke(file_ext, null, null); return tcs.Task; } } 

fetcher.BeginInvoke() launches (asynchronously) SaveFileDialog ShowDialog() on another thread, so that the main thread / user interface window (... ++) is not blocked and is not blocked, as it would with a simple direct call ShowDialog() . TaskCompletionSource<string> tcs not a WPF interface object, so its access to another "single" thread is fine.

This is another field that I need to study further. I feel that there is no β€œfinal” documentation / book on this subject (maybe you need to look again at books like the ones Steven Cleary had). This code should be improved, at least with the c-sharp-aynchronous-call-without-endinvoke theme

It works with Microsoft.Win32 FileDialog namespace

0
Feb 28 '17 at 15:52
source share



All Articles