Why do I have to cancel OpenFileDialog twice for it to close - vb.net

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.

Related

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.

VB SaveFileDialog if cancel then

When the user presses the "Cancel" button in SaveFileDialog I want to rename a textlabel.
Private Sub SaveFileDialog1_FileOk(sender As Object, e As System.ComponentModel.CancelEventArgs) Handles SaveFileDialog1.FileOk
Label6.Text = "Saved!"
End sub
This is working for the "Save" Button. I don't know how to do it for the "Cancel" button.
Thanks
Instead of using that event, why not compare the result returned from the ShowDialog() method?
If SaveFileDialog1.ShowDialog() = DialogResult.Ok
Label6.Text = "Saved!"
Else
Label6.Text = "Cancelled!"
End If
I prefer the use of cases for something like this
Select Case SaveFileDialog1.ShowDialog()
Case DialogResult.Ok
Label6.Text = "Saved!"
Case DialogResult.Cancel
Label6.Text = "Cancelled!"
End Select

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

VB.net Problems with Openfiledialog

I'm making this music player thing and I need help with some coding. I want my program to play a certain file when Checkbox1 is checked. I'm using OpenFileDialog but i'm not sure that's the right thing to do. I can't get it to work. Here's my code:
If CheckBox1.Checked = True Then
OpenFileDialog1.OpenFile()
AxWindowsMediaPlayer1.URL = OpenFileDialog1.FileName
AxWindowsMediaPlayer1.Ctlcontrols.play()
ElseIf CheckBox1.Checked = False Then
AxWindowsMediaPlayer1.Ctlcontrols.stop()
End If
Can someone please help me?
You need to show the dialog:
If CheckBox1.Checked Then
If OpenFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then
AxWindowsMediaPlayer1.URL = OpenFileDialog1.FileName
AxWindowsMediaPlayer1.Ctlcontrols.Play()
End If
Else
AxWindowsMediaPlayer1.Ctlcontrols.Stop()
End If
If you want the dialog to display so the user can select a file, use ShowDialog() and check the return value to ensure a file was actually selected by the user:
If CheckBox1.Checked = True Then
If OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
AxWindowsMediaPlayer1.URL = OpenFileDialog1.FileName
AxWindowsMediaPlayer1.Ctlcontrols.play()
End If
ElseIf CheckBox1.Checked = False Then
AxWindowsMediaPlayer1.Ctlcontrols.stop()
End If

Double openDialog VBA

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