Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C#, WPF - OpenFileDialog does not appear

I have been searching up and down the web and unfortunately never came across an issue quite like mine, so here goes:

My C# WPF application won't show me no OpenFileDialogs or SafeFileDialogs.

private void btnBrowseNet_Click(object sender, RoutedEventArgs e)
    {
        OpenFileDialog ofd = new OpenFileDialog();
        ofd.CheckPathExists = true;
        ofd.Multiselect = false;
        ofd.Title = "Open Network Configuration Batch file...";
        ofd.ValidateNames = true;
        ofd.Filter = "Comma Seperated Value Files|*.csv";

        if (ofd.ShowDialog() == true)
        {
           //...
        }
    }

This exact code does in one occasion exactly what it is supposed to do and hardly five minutes later I can click the button all I want, nothing happens but the mouse pointer turning into a little busy-indicator and then nothing. I can step through the method or do something like this

bool? shown = ofd.ShowDialog();

But no matter what, the dialog won't show. Of course, shown will be false in that case. I wasted one and a half hours searching yesterday and right when I quit I tried it again and all of a sudden it worked. Sometimes it works, sometimes it doesn't. But it seems to be project specific because I can paste the same code into a new project and it works like it is supposed to do. Also, that's the only thing about the project that seems fishy. Everything else works as intended.

Has anyone on here ever experienced something similar and thus an idea of what on earth I could do? Any help weould be highly appreciated.

like image 478
Koarl Avatar asked Feb 04 '10 16:02

Koarl


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is C in C language?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

Is C language easy?

Compared to other languages—like Java, PHP, or C#—C is a relatively simple language to learn for anyone just starting to learn computer programming because of its limited number of keywords.


2 Answers

I am experiencing a similar problem, and as Garrett suggested, it is an STA issue. I have been struggling with STA issues a lot in the past few months, as I need to launch screens from a console window (Testing purposes) - this means that the calling thread is not STA, but can be simulated in something like the following:

    [STAThread]
    private void Execute() {
        try {
            Thread t = new Thread(() => {
                OpenFileDialog dlg = new OpenFileDialog();
                // The following would not return the dialog if the current
                // thread is not STA
                var result = dlg.ShowDialog();
            });

            t.SetApartmentState(ApartmentState.STA);
            t.Start();
        } catch (Exception ex) {
            // Handle the exception
            ex.LogException();
        }
    }

Unforunately, it did not work for me to just mark the method as STAThread, I had to launch the operation in a thread marked as STA.

like image 132
Johan Aspeling Avatar answered Oct 07 '22 19:10

Johan Aspeling


sometime [staThread] not working, you can try this:

    public void showOpenFileDialog()
    {
        OpenFileDialog im = new OpenFileDialog();
        if (im.ShowDialog() == DialogResult.OK)
        {
            textBox1.Text = im.FileName;
        }
    }


    private void select_button_Click(object sender, EventArgs e)
    {
        Thread newThread = new Thread(new ThreadStart(showOpenFileDialog));
        newThread.SetApartmentState(ApartmentState.STA);
        newThread.Start();
    }
like image 25
Duo.Liu Avatar answered Oct 07 '22 20:10

Duo.Liu