Tuller45
Tuller45

Reputation: 37

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?

Upvotes: 0

Views: 989

Answers (2)

Joel Coehoorn
Joel Coehoorn

Reputation: 415665

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

Upvotes: 1

Idle_Mind
Idle_Mind

Reputation: 39122

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

Upvotes: 1

Related Questions