user2262982
user2262982

Reputation: 41

Play a sound in vb

Okay, so I have nearly perfected a game called lucky 7 using visual studio 2010. I want to play a sound when I have won the game (got 7 on one of the three slots). Here is the code:

Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click



    For i = 0 To 2
        slots(i) = rand.Next(10)
    Next

    Label1.Text = (slots(0).ToString)
    Label2.Text = (slots(1).ToString)
    Label3.Text = (slots(2).ToString)

    If slots(0) = 7 Or slots(1) = 7 Or slots(2) = 7 Then
        score = score + 100 'REM 10 points for each win 
        Label4.Text = (score.ToString)
        PictureBox1.Visible = True 'REM If you have a PictureBox
        PlaySound "C:\WINDOWS\MEDIA\TADA.WAV",
    Else
        PictureBox1.Visible = False
    End If

    If score = 500 Then

        MsgBox("You Scored 500 Points!", vbInformation)
    End If


End Sub

Thing is, I get an error on the 'playsound' statement and I really want to play that sound when the player wins the game!

I also tried to create a reset button, which sets the score and all three slots back to 0, but when I click it, nothing happens. Here is the code for the reset button:

 Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
    score = 0
    PictureBox1.Visible = False
End Sub

Upvotes: 0

Views: 4084

Answers (4)

Ale865
Ale865

Reputation: 126

You should try this to play a .wav sound file:

My.Computer.Audio.Play("C:\WINDOWS\MEDIA\TADA.WAV",AudioPlayMode.Background)

Upvotes: 0

Michael Parr
Michael Parr

Reputation: 134

As someone mentioned the sound files may not appear on every computer. You should look into using the system beep sound which can be modified to make some weird chimes. I had a lot of fun with it...

As for your reset button... You haven't coded the labels text to reset. I'm guessing you only want a visual change, as the values will still be changed when you play again. However being a reset button i guess you may want to reset each labels text to string.empty or something.

Upvotes: 0

ajakblackgoat
ajakblackgoat

Reputation: 2149

From your reset button event, your score is reset to 0 but not the text. No change to the text of your 3 slots as well.

Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
  Label1.Text = "0"
  Label2.Text = "0"
  Label3.Text = "0"
  Label4.Text = "0"
  score = 0
  PictureBox1.Visible = False
End Sub

Upvotes: 0

DBNickel
DBNickel

Reputation: 31

Try using My.Computer.Audio.Play

Have you walked through the code to make sure nothing is happening on the button click event? Clicking to the far left of the line "score = 0" will set a break-point. This will pause the code when you run it. You can hover over the variable score, and it will show it's value. Pressing [F8] will execute it, and you can hover over it and see that it's changed.

Upvotes: 2

Related Questions