Double openDialog VBA - vb.net

When I try to run this code it opens an open dialog with no results, then it opens another and does what I want it to do. Help?
Private Sub mnuOpen_Click(sender As Object, e As EventArgs) Handles mnuOpen.Click
Dim DidWork As Integer = openFD.ShowDialog()
openFD.InitialDirectory = "C:\"
openFD.Title = "Open a text file"
openFD.Filter = "Text Files|*.txt|Word Docs|*.doc"
openFD.ShowDialog()
If DidWork = DialogResult.Cancel Then
MsgBox("Cancel Button Clicked")
Else
strFileName = openFD.FileName
MsgBox(strFileName)
End If
End Sub

You don't need the DidWork variable in your example (and as pointed out, you are calling ShowDialog twice).
I would favor checking for Ok instead of Cancel in the DialogResult:
openFD.InitialDirectory = "C:\"
openFD.Title = "Open a text file"
openFD.Filter = "Text Files|*.txt|Word Docs|*.doc"
If openFD.ShowDialog() = DialogResult.Ok Then
strFileName = openFD.FileName
MsgBox(strFileName)
Else
MsgBox("Dialog Canceled")
End If

Related

Issue with OpenFileDialog and InitialDirectory property

I have two Buttons and two OpenFileDialogs and I am facing an issue with InitialDirectory property. When I choose a file for my first OpenFileDialog and then click to choose a file for my second OpenFileDialog, I get the same InitialDirectory and not the ones I have set!!!
Here is an example of my code...
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim FileDialog As New OpenFileDialog
Dim Path As String = Nothing
If Label1.Text IsNot Nothing And My.Computer.FileSystem.FileExists(Label1.Text) Then
Path = Label1.Text
Else
Path = "::{20D04FE0-3AEA-1069-A2D8-08002B30309D}"
End If
FileDialog.Title = "Open File Dialog"
FileDialog.InitialDirectory = Path
FileDialog.Filter = "Executable (*.exe)|*.exe"
FileDialog.RestoreDirectory = True
If FileDialog.ShowDialog() = DialogResult.OK Then
Label1.Text = FileDialog.FileName
End If
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Dim FileDialog As New OpenFileDialog
Dim Path As String = Nothing
If Label2.Text IsNot Nothing And My.Computer.FileSystem.FileExists(Label2.Text) Then
Path = Label2.Text
Else
Path = "::{20D04FE0-3AEA-1069-A2D8-08002B30309D}"
End If
FileDialog.Title = "Open File Dialog"
FileDialog.InitialDirectory = Path
FileDialog.Filter = "Executable (*.exe)|*.exe"
FileDialog.RestoreDirectory = True
If FileDialog.ShowDialog() = DialogResult.OK Then
Label2.Text = FileDialog.FileName
End If
End Sub
Well I found what was wrong...
I just had to set the Path variable like this Path = IO.Path.GetDirectoryName(Label1.Text) and not like this Path = Label1.Text. Because the first one gets file's directory path (which is required) and the second one gets file's path. And I was using the second one...

Why do I have to cancel OpenFileDialog twice for it to close

Here is the code:
Private Sub btn_selectfile_Click(sender As Object, e As EventArgs) Handles btn_selectfile.Click
OpenFileDialog1.FileName = ""
OpenFileDialog1.Filter = "Text Files | *.txt"
If OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
'some code here
ElseIf OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.Cancel Then
OpenFileDialog1.Dispose()
End If
End Sub
It also happens if I reverse them and put the DialogResult.OK in the ElseIf when selecting a file.
How shall I proceed? Thanks for your help.
Call ShowDialog once, save the result, and then check it. Currently, you're calling ShowDialog twice, which shows the dialog to the user twice.
Dim result As DialogResult = OpenFileDialog1.ShowDialog();
If result = Windows.Forms.DialogResult.OK Then
'some code here
ElseIf result = Windows.Forms.DialogResult.Cancel Then
OpenFileDialog1.Dispose()
End If
I guess, when you cancel the dialog, you want to exit procedure. In this case you just need to check if the result is Cancel:
If OpenFileDialog1.ShowDialog() = DialogResult.Cancel Then Exit Sub
After that line the result is OK, so you can safely get file path.

OpenFileDialog (Cancel/Close) File Not Found Crash

This is my code:
Private Sub HuraButton1_Click(sender As Object, e As EventArgs) Handles HuraButton1.Click
Dim openFileDialog1 As New OpenFileDialog()
openFileDialog1.FileName = "Select a Text File..."
openFileDialog1.Filter = "Text Files (*.txt) | *txt"
OpenFileDialog1.InitialDirectory = "C:\Users\Public\Desktop\"
OpenFileDialog1.Title = "Select a Files"
openFileDialog1.ShowDialog()
Dim Findstring = IO.File.ReadAllText(openFileDialog1.FileName)
Dim Lookfor As String = ""
Dim result = openFileDialog1.ShowDialog()
If openFileDialog1.FileName = Windows.Forms.DialogResult.Cancel Then
MsgBox("File Not Found")
End If
If Findstring.Contains(Lookfor) Then
MsgBox("Found")
Else
MsgBox("Not Found")
End If
End Sub
Error:
System.IO.FileNotFoundException: The File'D:\1DesktopFILE\RobeVisualStudio\ShaadyyTool\bin\Debug\Select a Text File' Not Found.
I want to make sure that those who close the "OpenFileDialog1" The app doesn't crash.
I Don't know why it doesn't work,
Can you write the correct code thanks.
Sorry for my eng.
I think this is what you want
Private Sub HuraButton1_Click(sender As Object, e As EventArgs) Handles HuraButton1.Click
Dim openFileDialog1 As New OpenFileDialog()
openFileDialog1.Filter = "Text Files (*.txt) | *txt"
openFileDialog1.InitialDirectory = "C:\Users\Public\Desktop\"
openFileDialog1.Title = "Select a Files"
openFileDialog1.CheckFileExists = True
If openFileDialog1.ShowDialog() = DialogResult.OK Then
'file selected now process it
Dim Findstring = IO.File.ReadAllText(openFileDialog1.FileName)
Dim Lookfor As String = ""
If Findstring.Contains(Lookfor) Then
MsgBox("Found")
Else
MsgBox("Not Found")
End If
Else
'file not selected or user cancelled
MsgBox("file not selected")
End If
End Sub
openFileDialog1.FileName = "Select a Text File..."
i think you miss understood the above statement to set as a dialog box's title ...it is not what you think...rather it is used to find/select a file in the opened directory
You want to make some changes to this. Use the dialog result to determine if you should move forward. If they didn't select a file, don't do the code.
If openFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then
Dim Findstring as String = IO.File.ReadAllText(openFileDialog1.FileName)
Dim Lookfor As String = ""
If Findstring.Contains(Lookfor ) Then
MsgBox("Found")
Else
MsgBox("Not Found")
End If
End If
Since the OpenFileDialog uses unmanaged code you really should put it in a Using block.
Using openFileDialog1 as OpenFileDialog = New OpenFileDialog
openFileDialog1.Filter = "Text Files (*.txt) | *txt"
...
End Using
That way you can be sure that it is removed from memory completely, even if there is an exception inside the Using block.
Then you should give your OpenFileDialog an element to which it will open modally, otherwise it can happen that your OpenFileDialog vanishes in the background and your application seems unresponsive (because it waits for the OpenFileDialog to return but the user doesn't see that).
Since you seem to be in a Form anyway, just do
openFileDialog1.ShowDialog(Me)
Then you should check for the return value of openFileDialog1.ShowDialog() and only proceed if there really has a file been selected.
If openFileDialog1.ShowDialog(Me) = DialogResult.Ok Then
...
End If
If the result of openFileDialog1.ShowDialog() is not DialogResult.Ok then openFileDialog1.FileName might be Nothing.
I hope this helps a little bit.

FolderBrowserDialog open again on cancel

When i open the FolderBrowserDialog then click cancel it reopens again.
But, On the second FolderBrowserDialog when you click cancel again it properly closes.
And when you select a path on the second FolderBrowserDialog, It does or returns nothing
Can i stop the second FolderBrowserDialog on appearing when i click on cancel on the first?
I dont know why is it appearing. thanks in advance.
here is my code:
Dim apppath
Try
FolderBrowserDialog1.RootFolder = Environment.SpecialFolder.Desktop
FolderBrowserDialog1.SelectedPath = "C:\"
FolderBrowserDialog1.Description = "Select File Location Path"
If FolderBrowserDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then
apppath = FolderBrowserDialog1.SelectedPath
ElseIf FolderBrowserDialog1.ShowDialog() = Windows.Forms.DialogResult.Cancel Then
Exit Sub
End If
My.Computer.FileSystem.WriteAllText(apppath & "apppath.txt", apppath, False)
MessageBox.Show(apppath)
Catch ex As Exception
MessageBox.Show("Invalid Location")
Exit Sub
End Try
Try something like this
Dim result as Windows.Forms.DialogResult = FolderBrowserDialog1.ShowDialog()
If result = Windows.Forms.DialogResult.OK Then
apppath = FolderBrowserDialog1.SelectedPath
ElseIf result = Windows.Forms.DialogResult.Cancel Then
Exit Sub
End If

trying to output more than one filenam into textbox vb.net

Apologies if this is really simple but I'm fairly new to programming. I've created a program that uses an open dialog box and outputs the names of the file to a textbox.
Where I'm having issues is trying to get the textbox to display more than one line as all it seems to be doing is writing one line in the textbox.
The code I'm using is below, could someone please advise what I need to change so that I can get this to work.
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim strFileName As String
OpenFD.Multiselect = True
OpenFD.InitialDirectory = "\\server\filename\"
OpenFD.Title = "Open a Text File"
OpenFD.Filter = "Text Files(.txt)|*.txt"
Dim DidWork As Integer = OpenFD.ShowDialog()
strFileName = OpenFD.FileName
If DidWork = DialogResult.Cancel Then
MsgBox("Cancel Button Clicked")
Else
strFileName = OpenFD.FileName
TextBox1.Text = strFileName += 1
End If
End Sub
I've managed to get everything else to work correctly but it's just this one thing.
Dim strFileName() As String
'...
Dim DidWork As Integer = OpenFD.ShowDialog()
If DidWork = DialogResult.Cancel Then
MsgBox("Cancel Button Clicked")
Else
strFileName = OpenFD.FileNames
TextBox1.Multiline = True
TextBox1.Text = ""
For Each sFile as String in strFileName
TextBox1.Text &= sFile & System.Enviroment.NewLine()
Next
End If
Set TextBox.Multiline property to True