|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
OpenFileDialogand 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 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 Show quoteHide quote "Mike" <ward***@swbell.net> wrote in message news:D6cog.29317$VE1.1286@newssvr14.news.prodigy.com... > 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 > > Claes,
> You've never defined any variable with the name OpenFileDialog1. Your code OpenFileDialog1 is the name given by the designer when dragging it.> looks rather weird. You have named your class OpenFileDialog1 (bad choice > of name imho) Cor 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()' -- Show quoteHide quoteTerry "Mike" wrote: > 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 > > > 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 Show quoteHide quote "Terry" <Terry@nospam.nospam> wrote in message news:210B72EC-ABD6-4909-BF18-7C4C3C567962@microsoft.com... > 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()' > -- > Terry > > > "Mike" wrote: > >> 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 >> >> >>
Show quote
Hide quote
"Mike" <ward***@swbell.net> schrieb: \\\> 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 /// Show quoteHide quote > 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 > ' InitializeOpenFileDialog() /// Show quoteHide quote > 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 -- M S Herfried K. Wagner M V P <URL:http://dotnet.mvps.org/> V B <URL:http://classicvb.org/petition/> 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. :-) CorShow quoteHide quote "Herfried K. Wagner [MVP]" <hirf-spam-me-here@gmx.at> schreef in bericht news:%23lWvdmgmGHA.3880@TK2MSFTNGP02.phx.gbl... > "Mike" <ward***@swbell.net> schrieb: >> 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 > /// > >> 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 >> ' > > \\\ > InitializeOpenFileDialog() > /// > >> 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 > > -- > M S Herfried K. Wagner > M V P <URL:http://dotnet.mvps.org/> > V B <URL:http://classicvb.org/petition/> |
|||||||||||||||||||||||