OpenFileDialog example not working - but why?

Hi

Wonder if anyone can help out

Copied directly from system help on OpenFileDialog -> FileNames.

Started with blank form1, and as per example setup instructions, added button named filebutton, picturebox named picturebox1, and created form load with

InitializePictureBox()
InitializeOpenFileDialog()

When I run the Application, and step through the Sub OpenFileDialog1_FileOk routine, I can see the 'file' variable is holding the proper data for the file(s) that were selected, but when it gets to the

PictureBox1.Image = System.Drawing.Image.FromStream(fileStream)

Application.DoEvents()

then nothing is displayed in the picturebox. Why The files selected were JPG's. Below is the full code.

Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
InitializePictureBox()
InitializeOpenFileDialog()
End Sub
Private Sub InitializePictureBox()
Me.PictureBox1 = New System.Windows.Forms.PictureBox
Me.PictureBox1.BorderStyle = _
System.Windows.Forms.BorderStyle.FixedSingle
Me.PictureBox1.SizeMode = PictureBoxSizeMode.StretchImage
Me.PictureBox1.Location = New System.Drawing.Point(72, 112)
Me.PictureBox1.Name = "PictureBox1"
Me.PictureBox1.Size = New System.Drawing.Size(160, 136)
Me.PictureBox1.TabStop = False
End Sub
Private Sub InitializeOpenFileDialog()
Me.OpenFileDialog1 = New System.Windows.Forms.OpenFileDialog

' Set the file dialog to filter for graphics files.
Me.OpenFileDialog1.Filter = _
"Images (*.BMP;*.JPG;*.GIF)|*.BMP;*.JPG;*.GIF|All files (*.*)|*.*"

' Allow the user to select multiple images.
Me.OpenFileDialog1.Multiselect = True
Me.OpenFileDialog1.Title = "My Image Browser"
End Sub
Private Sub fileButton_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles filebutton.Click
OpenFileDialog1.ShowDialog()
End Sub
' This method handles the FileOK event. It opens each file
' selected and loads the image from a stream into PictureBox1.
Private Sub OpenFileDialog1_FileOk(ByVal sender As Object, _
ByVal e As System.ComponentModel.CancelEventArgs) _
Handles OpenFileDialog1.FileOk
Me.Activate()
Dim file, files() As String
files = OpenFileDialog1.FileNames

' Open each file and display the image in PictureBox1.
' Call Application.DoEvents to force a repaint after each
' file is read.
For Each file In files
Dim fileInfo As System.IO.FileInfo = New System.IO.FileInfo(file)
Dim fileStream As System.IO.FileStream = fileInfo.OpenRead()
PictureBox1.Image = System.Drawing.Image.FromStream(fileStream)
Application.DoEvents()
fileStream.Close()

' Call Sleep so the picture is briefly displayed,
'which will create a slide-show effect.
System.Threading.Thread.Sleep(2000)
Next
PictureBox1.Image = Nothing
End Sub
End Class






Answer this question

OpenFileDialog example not working - but why?

  • jegar

    Yes ReneeC, I have and am learning by doing larger projects. I have managed to finish 3 and am on my fourth.

    In the fourth one, I need to use the multiple file name feature returned by the OpenFileDialog, and so was looking for examples of its use - which lead to the non working example I quoted. I also intend trying to find if a filedialogbox of any kind supports check boxes - but that will be my next investigation.

    Even though the example wasn't working correctly, I have indeed managed to find at least some of the information I was looking for regarding the FileNames property.

    You are pushing yourself at a much higher level than me :)



  • c_shah

    Hi,

    In the Sub InitializePictureBox first line is actually replacing existing PictureBox with new one. I am not sure why this would not work, but if remove the line
    Me.PictureBox1 = New System.Windows.Forms.PictureBox
    everything seems to work.

    Max



  • Jeff Blake

    Hi Leshay,

    I deleted my post because I wrote before my second cup of coffee and I erred in what I said.

    Leshay, I wouldn't receommend that you learn primarily from Microsoft examples. I'd recommend that you conceive of a project that's just slightly larger in depth than you are right now and proceed to write it. that's how I grow anyway.

    Right now, I'm having trouble with a search algorithm and I remarked on that to Cgraus and he said, "Oh well, MSDN's search algorithm's don't work. It makes me all the more determined you know because they don't work. As a matter of fact the search algorithms on most boards don't work.

    So.... I'm pushing myself!

    Renee



  • Clemson

    I hope so, I've been in software for 35 years.....



  • Marcin Książek

    Hi ReneeC

    Well, I am just trying to get the example working. This is an MS example takes straight from the system Help.

    I realise that what you say is probably more useful, but I just can't get the supplied example to work.

    If MS can't provide a working example, what chance do we newbies have :)

    To answer your question. The program is supposed to provide a slideshow effect, by loading each image sequentially into the picturebox, pause, display next until finished file names.

    I am actually trying to get to understand the use of the 'FilesNames' property of the OpenFileDialog, rather than picturebox manipulation. This was the example provided under that help topic.

    btw - your post, the one this is a reply to, has disappeared

    Have a nice day :) (see, I can speak American too :)



  • soul creator

    Thanks Maksim

    That does make it work. Very strange though. Does MS supply non working code as examples The example was taken directly from the help system, and the instructions called for the picturebox to be created on the form via the designer.

    Commenting out that offending line does work.

    Removing the designer picturebox instead of commenting out, that line doesn't create a new picturebox. Indeed, it needs the picturebox to be on the form for

    Me.PictureBox1 = New System.Windows.Forms.Picturebox

    to be valid in the code. Very strange ..................



  • wpf michelle

    I had this same problem before.

    You might have accidentally put the picture box to the back and something was ontop of it....Making it not visible.

    What ever though, you got it working!


  • arkiboys

    The OpenFileDialog is a great tool, and I use it all the time, but it is rather inflexible. One way to expand your programming knowledge is to learn the System.IO functions and write your own OpenFileDialog, perhaps with a couple of added features that you feel you'll use in the future (and/or take out features you feel you'll never use). Take it slowly, and you'll learn a lot of how .Net handles file I/O, which is always a good thing to learn about a programming language!


  • OpenFileDialog example not working - but why?