FolderBrowserDialog open again on cancel - vb.net

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

Related

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.

Can't launch .exe

Trying to make a "world of warcraft" launcher.
How it should work:
When you press " Start Authserver " you get to locate the authserver.exe. the location is then saved so you don't have to do that twice.
Once pressed, it obviously should launch the authserver.exe.
What happens:
When you press " Start Authserver " it opens and instantly closes with error:
Error, couldn't open cause authserver.conf couldn't be located.
Notice:
Authserver.exe and authserver.conf is in the same folder, I'm able to launch it manually.
Mort helped me with similar problem yesterday, i hoped that the same VB code would work since that would seem obvious.
Private filePath As String = String.Empty
Private Sub PlayButton_Click(sender As System.Object, e As System.EventArgs) Handles PlayButton.Click
Try
If filePath.Length = 0 Then
Dim diagResult As DialogResult = OpenFileDialog1.ShowDialog()
If diagResult = Windows.Forms.DialogResult.OK Then
filePath = OpenFileDialog1.FileName
If filePath.ToUpper.EndsWith("WOW.EXE") Then
Process.Start(filePath)
Else
MessageBox.Show("Wrong file selected!")
filePath = String.Empty
End If
End If
Else
Process.Start(filePath)
End If
Catch ex As Exception
MessageBox.Show(String.Concat("An error occurred in the play button click:", ex.Message))
End Try
End Sub
Anyone got an idea?
In advance: Thanks.
Try something like this:
Private Sub PlayButton_Click(sender As System.Object, e As System.EventArgs) Handles PlayButton.Click
Try
OpenFileDialog1.FileName = ""
OpenFileDialog1.Filter = "World of Warcraft (WOW.EXE)|WOW.EXE"
Dim diagResult As DialogResult = OpenFileDialog1.ShowDialog
If diagResult = Windows.Forms.DialogResult.OK Then
Dim p As New Process
Dim fn As New System.IO.FileInfo(OpenFileDialog1.FileName)
p.StartInfo.WorkingDirectory = fn.DirectoryName
p.StartInfo.FileName = fn.Name
p.Start()
End If
Catch ex As Exception
MessageBox.Show(String.Concat("An error occurred in the play button click:", ex.Message))
End Try
End Sub

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

Fileopen through the FolderBrowserDialog.. How?? HELP!

This is what i got now...
FolderBrowserDialog1.ShowDialog()
TextBox1.Text = FolderBrowserDialog1.SelectedPath
If FolderBrowserDialog1.SelectedPath = Nothing = True Then MsgBox("Select your folder..")
If FolderBrowserDialog1.SelectedPath = Nothing = True Then Button1.Enabled = False
If FolderBrowserDialog1.SelectedPath = Nothing = False Then Button1.Enabled = True
End Sub
FileOpen(1, ,,,,,, & "File" & ".dll", OpenMode.Output)
PrintLine(1, TextBox2.Text)
FileClose()
End Sub
But i want the Output folder (The place where File.dll is saved) to be the FolderBrowserDialog1.SelectedPath... how? Anyone?
Tried FileOpen(1, FolderBrowserDialog1.SelectedPath & File & .dll, OpenMode.Output) but no :(
Note sure I understand what you want to do, but you may try to set the SelectedPath property before calling ShowDialog method.