VB.net Problems with Openfiledialog - vb.net

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

Related

Bring Windows Forms window back to front after opening an external application

I am opening from my VB.net Windows Forms application a PDF:
Try
Process.Start(fileName)
Catch e As System.ComponentModel.Win32Exception 'no PDF viewer installed, use browser
Dim startinfo As New ProcessStartInfo
startinfo.FileName = "C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe"
startinfo.Arguments = String.Format(fileName)
startinfo.UseShellExecute = False
Process.Start(startinfo)
End Try
I want my application form to come back to front after the PDF is opened. I have tried all of those, but neither works:
Me.Activate()
Me.BringToFront()
Me.TopMost = True
Me.TopMost = False
Just using Me.TopMost=True in fact works, but I do not want to enforce my application to be in front of all others windows. I just want to bring it to front once after PDF opening. As soon as I add the command Me.TopMost = False to reset it, it does not work any more.
I don't know if you can use a specific API to do what you ask, but from my point of view you have already found the solution:
Me.TopMost = True
Me.TopMost = False
You have to use these two lines right after the end of the PDF opening operations.
Here is an example using the following code:
Imports System.Threading
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Me.BackColor = Color.Green
Label1.Text = "Start"
End Sub
Private Sub PdfOperation_Simulation(sender As Object, e As MouseEventArgs) Handles Label1.MouseDown
Me.BackColor = Color.Red
Label1.Text = "Wait..."
Me.Refresh()
Thread.Sleep(3000) 'Pdf opening... I manually bring to front VisualStudio
Me.TopMost = True
Me.TopMost = False
Me.BackColor = Color.Green
Label1.Text = "Finish"
End Sub
End Class
Output:
UPDATE
It just does not work for me. I have simplified my code to Dim
startinfo As New ProcessStartInfo startinfo.FileName
="C:\README.TXT" startinfo.UseShellExecute = True Process.Start(startinfo) Me.TopMost = True Me.TopMost = False This
opens notepad, but my application is not coming to front.
This is because the notepad window showing operation take some time. Depending on your needs, you can simply wait some seconds by using a Thread.Sleep just before Me.TopMost = False or do something like this:
Dim startinfo As New ProcessStartInfo
startinfo.FileName = "C:\README.TXT"
startinfo.UseShellExecute = True
Process.Start(startinfo)
Dim t As New Thread(
Sub()
Thread.Sleep(1000)' One second
Me.Invoke(Sub() MoveOnTop())
End Sub
)
t.Start()
Where MoveOnTop is
Private Sub MoveOnTop()
Me.TopMost = True
Me.TopMost = False
End Sub
Using the thread, your application will not be frozen by the Sleep operation.
Just another option. Have you considered setting notepad's WindowStyle to minimized? Here is an example:
Dim startinfo As New ProcessStartInfo("Notepad")
startinfo.Arguments = "C:\README.TXT"
startinfo.UseShellExecute = True
startinfo.WindowStyle = ProcessWindowStyle.Minimized
Process.Start(startinfo)

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.

How can I get my text file data to display in listbox?

I have an issue that I would like some help with.
I am trying to open a file dialog, select text file, and then display text in listbox.
I have the following code. It opens dialog, but won't display text in listbox.
Any suggestions?
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnImportKeywordList.Click
Dim oReader As StreamReader
OpenFileDialog1.CheckFileExists = True
OpenFileDialog1.CheckPathExists = True
OpenFileDialog1.DefaultExt = "txt"
OpenFileDialog1.FileName = ""
OpenFileDialog1.Filter = "Text Files (*.txt)|*.txt|All Files (*.*)|*.*"
OpenFileDialog1.Multiselect = False
If OpenFileDialog1.ShowDialog() = DialogResult.OK Then
oReader = New StreamReader(OpenFileDialog1.FileName, True)
ListBox1.Text = oReader.ReadToEnd()
End If
End Sub
Listbox display text through the Items collection not through the Text property. In a ListBox the Text property represent the text of the currently selected item
An example could be written in this way
....
If OpenFileDialog1.ShowDialog() = DialogResult.OK Then
Using oReader = New StreamReader(OpenFileDialog1.FileName, True)
While oReader.Peek <> -1
ListBox1.Items.Add(oReader.ReadLine())
End While
End Using
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.