OpenFileDialog


M

Mike

Hello all, I'm trying to work with the openfiledialog and being new to vb
and vb.net I'm having some problems. Wherever I have Me.OpenFileDialog1 I
keep getting the following error:

C:\Visual Studio Projects\ImageLoad\OpenFileDialog1.vb(66):
'OpenFileDialog1' is not a member of 'ImageLoad.OpenFileDialog1'.

Can someone please tell me why and how can I correct it. Here is my code.
Thank you in advance.


Public Class OpenFileDialog1
Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

Public Sub New()
MyBase.New()

'This call is required by the Windows Form Designer.
InitializeComponent()

'Add any initialization after the InitializeComponent() call
InitializeOpenFileDialog()

End Sub

'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub

'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer

'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents fileButton As System.Windows.Forms.Button
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()
Me.fileButton = New System.Windows.Forms.Button
Me.SuspendLayout()
'
'fileButton
'
Me.fileButton.Location = New System.Drawing.Point(128, 168)
Me.fileButton.Name = "fileButton"
Me.fileButton.TabIndex = 0
Me.fileButton.Text = "FileButton"
'
'OpenFileDialog1

'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(292, 266)
Me.Controls.Add(Me.fileButton)
Me.Name = "OpenFileDialog1"
Me.Text = "OpenFileDialog1"
Me.ResumeLayout(False)

End Sub

#End Region

Private Sub OpenFileDialog1_Load(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles MyBase.Load

End Sub
Private Sub fileButton_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles fileButton.Click
OpenFileDialog1.ShowDialog()
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
End Class
 
Ad

Advertisements

C

Claes Bergefall

You've never defined any variable with the name OpenFileDialog1. Your code
looks rather weird. You have named your class OpenFileDialog1 (bad choice of
name imho) and it seems as if you're trying to access it as a member
variable?!? That's not how classes work.

Me.OpenFileDialog1 is a reference to a member variable called
OpenFileDialog1 in the local class. It has nothing to do with what the class
is called. In this case though you don't need any member variable. Just
create the OpenFileDialog when you need it. Write your fileButton_Click
button as follows:

Private Sub fileButton_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles fileButton.Click
Dim dlg As New OpenFileDialog
' Set the file dialog to filter for graphics files.
dlg.Filter = "Images (*.BMP;*.JPG;*.GIF)|*.BMP;*.JPG;*.GIF|All files
(*.*)|*.*"
' Allow the user to select multiple images.
dlg.Multiselect = True
dlg.Title = "My Image Browser"
If dlg.ShowDialog() = DialogResult.OK Then
'Do stuff with the file(s)
End If
dlg.Dispose()
End Sub

and remove the InitializeOpenFileDialog method.

/claes
 
G

Guest

Mike,
First, the name of the Dialog and the name of the Form you are calling it
from have to be different! Create a new windows project and give the form a
name different then OpenFileDialog1. Second, add your button. Third, add an
OpenFileDialog from the tool box. This is a 'component' and not a control.
Its default name will be OpenFileDialog1. In the Form load event, initialize
the OpenFileDialog1 properties like 'filter' and 'multiselect'.
Finally, in the button click event show the dialog -
'OpenFileDialog1.ShowDialog()'
 
H

Herfried K. Wagner [MVP]

Mike said:
Public Class OpenFileDialog1
Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

Public Sub New()
MyBase.New()

'This call is required by the Windows Form Designer.
InitializeComponent()

'Add any initialization after the InitializeComponent() call
InitializeOpenFileDialog()

End Sub

'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub

'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer

\\\
Private OpenFileDialog1 As OpenFileDialog
///
 
M

Mike

Thanks for the help. This is now working properly. Here is the new code:

Public Class Form1
Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

Public Sub New()
MyBase.New()

'This call is required by the Windows Form Designer.
InitializeComponent()

'Add any initialization after the InitializeComponent() call
InitializeOpenFileDialog()

End Sub

'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub

'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer

'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents OpenFileDialog1 As System.Windows.Forms.OpenFileDialog
Friend WithEvents FileButton As System.Windows.Forms.Button
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()
Me.OpenFileDialog1 = New System.Windows.Forms.OpenFileDialog
Me.FileButton = New System.Windows.Forms.Button
Me.SuspendLayout()
'
'FileButton
'
Me.FileButton.Location = New System.Drawing.Point(200, 128)
Me.FileButton.Name = "FileButton"
Me.FileButton.TabIndex = 0
Me.FileButton.Text = "Button1"
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(292, 266)
Me.Controls.Add(Me.FileButton)
Me.Name = "Form1"
Me.Text = "Form1"
Me.ResumeLayout(False)

End Sub

#End Region

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles FileButton.Click

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

End Class
 
C

Cor Ligthert [MVP]

Claes,
You've never defined any variable with the name OpenFileDialog1. Your code
looks rather weird. You have named your class OpenFileDialog1 (bad choice
of name imho)

OpenFileDialog1 is the name given by the designer when dragging it.

Cor
 
Ad

Advertisements

C

Cor Ligthert [MVP]

Herfried,

It is the very correct answer, but I had to look very long before I saw it..

Would you not have written that the openfiledialog was only created in the
procedure which was going out of scope after that it was done; and that
therefore in this way with the two methods were it was used it should be a
global object. (I did not see first that there where two methods to open the
dialog).

As well for the OP of course.

:)

Cor
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top