Newbe Question (very basic)

bskiff

Member
Joined
Jul 8, 2003
Messages
5
Im new here. My name is Bill and I might be bugging you guy regularly now! I have been developing applications for about 12 years now. I am now learning VB.net. It is very similar to the Powerbuilder that Ive been using for the past 7 years. I am working my way though the tutorials in the book Visual Basic.Net Step by Step. Im still in the beginning of the book. I am add script to a menu clicked event. I copied the following code right out of the book:

OpenFileDialog1.Filter = "Bitmaps (*.bmp)|*.bmp"
If OpenFileDialog1.ShowDialog() = DialogResult.OK Then
PictureBox1.Image = System.Drawing.Image.FromFile _
(OpenFileDialog1.FileName)
mnuCloseItem.Enabled = True
End If

I get an undeclared variable error on OpenFileDialog1. When I look at they solution in the book, it has the same code and no error. Can someone point me in the right direction to track this down? I dont want to just copy the books solution without understanding.

Thanks,
Bill
 
You need to place an OpenFileDialog component on your form.
Go to the form design view and create one from the toolbox.
 
you could also do it without phisically putting an openfile dialog on the form , like this :
Code:
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim od As New OpenFileDialog()

        With od
            .Filter = ("Bitmaps (*.bmp)|*.bmp")
            .ShowDialog()
        End With

        If Not od.ShowDialog = DialogResult.Cancel Then
            Dim im As Image
            im = im.FromFile(od.FileName)
            PictureBox1.Image = im
        End If

    End Sub
 
Im not sure if anyone else would have this problem but, when I ran the code:

Code:
If Not od.ShowDialog = DialogResult.Cancel Then
     ...
End If

It popped up the open file dialog a second time. I found that if I change the line to this:

Code:
If Not od.ShowDialog.Cancel Then
     ...
End If

it worked just fine.
 
Back
Top