Shreekant
Shreekant

Reputation: 469

Open File Dialogue missing a type of files

I want user to upload a logo, for the same I created an OpenFileDialog. Everything works fine, but the dialog if opened more than once it doesn't show some type of files. When I checked it properly, I came to know that these files are of .gif type.

Here is my code -

 Private Sub PictureBox1_Click(sender As Object, e As EventArgs) Handles PictureBox1.Click
    With OpenFileDialog1
        Dim result As DialogResult = OpenFileDialog1.ShowDialog()
        .CheckFileExists = True
        .Filter = "Image files (*.jpg, *.jpeg, *.jpe, *.jfif, *.png, *.bmp, *.gif) | *.jpg; *.jpeg; *.jpe; *.jfif; *.png, *.bmp, *.gif"

        If result = DialogResult.OK Then
            Me.PictureBox1.BackgroundImage = Nothing
            PictureBox1.Image = Image.FromFile(OpenFileDialog1.FileName)
            PictureBox1.SizeMode = PictureBoxSizeMode.StretchImage
        End If

    End With
End Sub

I will show you some images, so you will get a clearer idea about the issue.

This is my control to open dialog

enter image description here

When I click it, File Dialog Opens and shows these many images

enter image description here

Now if I cancel or select any one of these images and open the file dialog again it doesn't show some files(.gif)

You can see this in image below

enter image description here

At first I thought it is because I haven't added *.gif in my code to filter images. But even after adding it, facing the same issue.

Upvotes: 1

Views: 113

Answers (1)

ItsPete
ItsPete

Reputation: 2368

Your separator changes & makes the filter invalid: *.jpg; *.jpeg; *.jpe; *.jfif; *.png, *.bmp, *.gif should be *.jpg; *.jpeg; *.jpe; *.jfif; *.png; *.bmp; *.gif.

The filter is also being set after opening the file dialog the first time. Set the filter first & then call ShowDialog():

With OpenFileDialog1
    .CheckFileExists = True
    .Filter = "Image files (*.jpg, *.jpeg, *.jpe, *.jfif, *.png, *.bmp, *.gif) | *.jpg; *.jpeg; *.jpe; *.jfif; *.png; *.bmp; *.gif"

    Dim result As DialogResult = .ShowDialog()

    If result = DialogResult.OK Then
        Me.PictureBox1.BackgroundImage = Nothing
        PictureBox1.Image = Image.FromFile(OpenFileDialog1.FileName)
        PictureBox1.SizeMode = PictureBoxSizeMode.StretchImage
    End If
End With

Upvotes: 1

Related Questions