Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I can't get a Waiting mouse cursor with OpenFileDialog, whatever I try

I want my user to open files and I use the OpenFileDialog class for that in C#. Sometimes it takes a while before the filedialog to open files appears. I set a waiting cursor to inform the user that things take a while, but it is not showing. In fact, I see the cursor change very briefly in a waiting cursor, but then it returns to normal, while the open file dialog is still not showing.

I started out with things like:

Cursor = Cursors.Waiting;

which works fine for all lengthy operations in my application, but not for the OpenFileDialog. Searching the Internet, and mostly SO, I found a better way to do this:

Application.UseWaitCursor = true;
// open file dialog
Application.UseWaitCursor = false;

but that doesn't do the job either... I tried the last option with and without the Application prefix, but neither gives the desired result.

I've even found some custom WaitCursor class on another site and implemented that, but to no avail.

This is what I have now (and doesn't work):

public string[] LetUserChoosePdfFiles()
{
    string[] pdfFileNames;

    Application.UseWaitCursor = true;   // this doesn't work for the OpenFileDialog and can't find a solution on the Internet?

    using (OpenFileDialog openFileDialog = new OpenFileDialog())
    {
        openFileDialog.Filter = "PDF files (*.pdf)|*.pdf|All files (*.*)|*.*";
        openFileDialog.FilterIndex = 1;
        openFileDialog.RestoreDirectory = false;
        openFileDialog.Multiselect = true;

        if (openFileDialog.ShowDialog() == DialogResult.OK)
        {
            pdfFileNames = openFileDialog.FileNames;
        }
        else
        {
            pdfFileNames = null;
        }
    }

    Application.UseWaitCursor = false;

    return pdfFileNames;
}
like image 862
Hespeet Avatar asked Dec 04 '25 08:12

Hespeet


1 Answers

I got successful results with this method.

Application.DoEvents();
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
    Cursor.Current = Cursors.WaitCursor;

    // transactions

    Cursor.Current = Cursors.Default;
}
like image 92
Cenk Yenikoylu Avatar answered Dec 06 '25 21:12

Cenk Yenikoylu