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.
Related
I developed program and tried it on multi PCs, it works perfectly in some of them.
There are 2 PCs with 32 bit, which have given stop working error.
I upgraded one of them into 64 bit, but still has the same problem!
The error appeared at the same moment:
I choose picture from computer then it will shown at picture box
I save data on DB (data include text and picture (ole object))
Then I try add new record and choose new picture the Error message come (program stop working).
program developed using visual basic.net by Visual Studio and access DB
the code of showing picture :
Dim opf As New OpenFileDialog
opf.Filter = "Choose Image(*.JPG;*.PNG;*.GIF)|*.jpg;*.png;*.gif"
If opf.ShowDialog = Windows.Forms.DialogResult.OK Then
nameOfFile = opf.FileName
PicBox.Image = Image.FromFile(opf.FileName)
End If
If it just crashes out of your application, you might want to handle any exceptions. Once you handle the exception you can messagebox the error and it might give you an idea why the application has a problem.
As a guess it might be an issue with 32bit/64bit drivers but best to see why application has the issue first.
Here is the code that #Dennis suggested.
Try
Dim opf As New OpenFileDialog
opf.Filter = "Choose Image(*.JPG;*.PNG;*.GIF)|*.jpg;*.png;*.gif"
If opf.ShowDialog = Windows.Forms.DialogResult.OK Then
nameOfFile = opf.FileName
PicBox.Image = Image.FromFile(opf.FileName)
End If
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
I would guess that the error is more likely in the Save code. If this does not produce a MessageBox then try the same thing on the save code.
If this solves your problem pleas accept #Dennis's answer because it was his idea.
Hello I am very new to Visual Basic and have almost no clue what I am doing. I have encountered an issue saying the following.
openFileDialog1 is not a member of windowsapp1.my
I can not seem to fix it. Here is my code:
Public Class Form1
Private Sub Form1_Load()
Dim OpenFileDialog1 As New OpenFileDialog
My.openFileDialog1.Title = "Please select a DB file"
openFileDialog1.InitialDirectory = "C:\"
openFileDialog1.Filter = "DB Files|*.extensionHERE"
End Sub
End Class
I have tried to no avail to fix this and so I hope that some one can help, thanks!
I think you're mis-reading what the examples are saying. When you read tutorial/example code, where it lists "OpenFileDialog1," for example (or DataGridView1, etc.) that refers to whatever name you are using for your particular OpenFileDialog1 control. If you had multiple openfiledialogs in your code, having them all named OpenFileDialogN can get confusing it a larger application. Using a meaningful name is a good practice.
Using the "My" isn't necessary in your code snippet. In VB, the "My" refers to a namespace indicating the system on which the application runs. Since all your code is doing is opening a file dialog, that isn't necessary.
Check out this handy doc by MS: https://learn.microsoft.com/en-us/dotnet/visual-basic/developing-apps/development-with-my/index
I have a VSTO on MS Project. I use VB.NET. What I need is when I press the button I created on the ribbon, it will perform some codes which will update the info of some task, however, I would need to close the MS Project automatically. I tried application.FileCloseEx(), but it only closes the file, the MS Project is still loaded. I need similar to clicking the x button of the window.
Thanks,
Gilbert
If your MS Project application object is represented by "appMSProject" then it's as simple as:
appMSProject.Quit
OR say in a macro running under Project:
Application.Quit
Here's how I do it in VBA from Excel or Access. As far as I can tell the objects & methods are the same in VB.NET. Bottom line is that I create an instance of the MS Project object which starts the app & opens a file, execute some work, close the file, then destroy the MS Project object by setting it to Nothing. That has the effect of closing the app. You can also use "appMSProject.Quit" followed by setting it to Nothing. Frankly the 2nd option looks more orderly & easier to understand in code. Anyway, here's a sample of the way I do it:
Dim appMSProject As MSProject.Application
Dim prjPrj As MSProject.Project
Dim strPrjFile As String
strPrjFile = "C:\where_is_my_file\file_name.mpp"
Set appMSProject = New MSProject.Application
appMSProject.FileOpenEx Name:=strPrjFile
Set prjPrj = appMSProject.ActiveProject
'''Do something in here with the prjPrj
'Close the file, in my case w/o saving
appMSProject.FileCloseEx pjDoNotSave
'Destroy the objects
Set prjPrj = Nothing
Set appMSProject = Nothing
FYI - In this example I'm doing background work so I don't show the app. I also use "early binding".
Here's an MSDN example that does show the app with more info on early -vs- late binding - https://msdn.microsoft.com/en-us/library/office/ff865152.aspx
I have some task to select files in VB6 with the control "CommonDialog1".When the project is migrated this control is converted to openfile dialog.When some of the properties are not converted it is showing as commented.
This line was commented
'CommonDialog1.Flags = CommonDialog1.Flags Or &H80000 ' Dont ALLOWMULTISELECT
I want to know do we have any propery in VB.NET for Open File Dialog Control?
Just make a quick search, or use the help of IntelliSense if you're using Visual Studio.
Dim myOpenFile as New OpenFileDialog()
myOpenfile.Multiselect = False
Documentation
We have created a workflow process as an SSIS package and would like to find a way of gaining access to this code so that through vb.net we can dynamically access and run that code. For example, we would like to change the data sources being used, or change the column mappings of existing packages and then execute them from a vb.net application. Please advise the best way to do this.
You will find some of your tasks easy, others not so much.
Generally speaking, you'll be interested in reading the Developers Guide to Integration Services. Of particular interest will be Building Packages Programmatically and Running and Managing Packages Programmatically.
For example, to run a package from VB.NET
Imports Microsoft.SqlServer.Dts.Runtime
Module Module1
Sub Main()
Dim pkgLocation As String
Dim pkg As New Package
Dim app As New Application
Dim pkgResults As DTSExecResult
pkgLocation = _
"C:\Program Files\Microsoft SQL Server\100\Samples\Integration Services" & _
"\Package Samples\CalculatedColumns Sample\CalculatedColumns\CalculatedColumns.dtsx"
pkg = app.LoadPackage(pkgLocation, Nothing)
pkgResults = pkg.Execute()
Console.WriteLine(pkgResults.ToString())
Console.ReadKey()
End Sub
End Module
To change the connection manager programmatically, it'd be the VB.NET equivalent of
ConnectionManager item = ep.Connections["MyConnectionManagerName"]
item.ConnectionString = #"Provider=SQLNCLI10.1;Data Source=Server1;Initial Catalog=ABC;Integrated Security=SSPI;";
Changing column mappings, that's where it's going to get interesting, for all definitions of the word interesting. I'm have a distilled example but it takes some work and you'll want to really understand the whole object model (and I hope you like COM). EzAPI might be of some assistance in that area.