Search code examples
c#winformsfreezeopenfiledialogshowdialog

Openfiledialog works fine on windows 10, but freezes windows app on windows 7


I have windows application, where my program have the following code:

static class Program
{
    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new MainForm());
    }
}

Now, on the MainForm(), I have few buttons, where upon clicking each of the button, I hide the MainForm and open a new form (Windows Form) using opendialog as shown in the below code:

this.Hide();
TestCenter testCenter = new TestCenter();
testCenter.ShowDialog();
this.Show();

Now, in the TestCenter form, I have a functionality (OpenFileDialog) for selecting a file, as shown in the below code:

OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = "image file |*.jpg;*.png;*.gif";
DialogResult dr = ofd.ShowDialog();
if (dr == DialogResult.Cancel)
    return;
pictureBox_PartImage.Image = Image.FromFile(ofd.FileName);
txt_ImagePath.Text = ofd.FileName;

I have a TextBox and a PictureBox, for showing the filepath and the image after making the selection in OpenFileDialog.

The weird thing is, that, when I run this program from Visual Studio or from the installed programs on my laptop (Windows 10) it is working excellently without any issue. But when I install this on client machine (Windows 7) it is freezing this Windows Form application when I click on the button which calls this OpenFileDialog().

Can someone please help me with this issue?

--------EDIT--------2/7/18--------

private void btnImage_Click(object sender, EventArgs e)
{
    OpenFileDialog ofd = new OpenFileDialog();
    // ofd.ShowHelp = true;
    ofd.Filter = "Image Files (*.png, *.gif, *.jpg)|*.png; *.gif*;*.jpg";
    DialogResult dr = ofd.ShowDialog();
    if (dr == DialogResult.Cancel)
        return;
    pictureBox_PartImage.Image = Image.FromFile(ofd.FileName);
    txt_ImagePath.Text = ofd.FileName;
}

Solution

  • Apparently, adding OLE DB Services = -1 to my Connection String solved the issue.

    In my form, I am using Access DB Connection for fetching data from the database. And this is the one that is messing up with loading of OpenFileDialog. And, this also explains why the sample code (by Wyck) is working fine (since there was no usage of DB Connection in there).

    And I'm wondering why the answer by Vikas4u for this question (which was my reference) was voted down.