简体   繁体   中英

VB.net, cannot access OpenFileDialog that has been added to project

I have decades of experience writing VB6 apps but today I tried to write a simple VB.net app using Microsoft Visual Studio 2019. I fell at the first hurdle. I'm sure I'm doing something really simple wrong so forgive the naive question.

Using the toolbox, I added an OpenFileDialog object to my main (one and only) form. Unlike VB6, this didn't appear on the form but in a sort of bar along the bottom of the window, but it was definitely there and I was able to set some properties.

I then added a button and wrote my first line of code: With OpenFileDialog1 Wrong: I get errpr BC30451. 'OpenFileDialog1' is not declared. It may be inaccessible due to its protection level.

But I can see it sitting there in the design view, I have spelt its name correctly and the above code is exactly copied from the example. I tried changing OpenFileDialog1's Modifiers property from Friend to Public - makes no difference.

What novice mistake have I made? How do I access an OpenFileDialog object that I have definitely added to my form?

The FolderBrowserDialog which can be dragged onto the form using the toolbox, is out of date and offers (from today's perspective) too few options. Do yourself a favor and use the Nuget Package Manager to download the modern OpenFileDialog.

Imports Microsoft.WindowsAPICodePack.Dialogs

in your Private Sub Button1_Click :

Dim Pfad As String
        Using OFD1 As New CommonOpenFileDialog
            OFD1.Title = "Datei auswählen"
            OFD1.Filters.Add(New CommonFileDialogFilter("JPG", ".jpg"))
            OFD1.Filters.Add(New CommonFileDialogFilter("JPEG", ".jpeg"))
            OFD1.Filters.Add(New CommonFileDialogFilter("Bitmap", ".bmp"))
            OFD1.Filters.Add(New CommonFileDialogFilter("PNG", ".png"))
            OFD1.IsFolderPicker = False 'true to make it an FolderBrowserDialog ;-)
            OFD1.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop)
            If OFD1.ShowDialog = CommonFileDialogResult.Ok Then
                Pfad = OFD1.FileName
            Else
                Return
            End If
        End Using

for Multiselect (with Bildpfade1 as a New List(of String) ):

Using OFD As New CommonOpenFileDialog
            OFD.Title = "Bilder auswählen"
            OFD.Filters.Add(New CommonFileDialogFilter("JPG", ".jpg;jpeg"))
            OFD.Multiselect = True
            OFD.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop)
            If OFD.ShowDialog() = CommonFileDialogResult.Ok Then
                For Each file As String In OFD.FileNames
                    Bildpfade1.Add(file)
                Next
            End If
End Using

I think I have found the answer but it looks, to me, like a bug in Visual Studio. I saved the project and closed Visual Studio. Restarted and reloaded the project. All the controls I had added to my form had vanished!

I rebuilt the now-empty project then started adding controls - lo and behold, I can now access all my controls from code whereas, previously, I couldn't access any of them. It looks like, prior to the initial build, none of the controls I thought I'd added had actually been added. Certainly they didn't survive save project, restart Visual Studio.

Seems to work now. Many thanks to those who contributed suggestions.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM