我想在VB.NET中创建一个按钮,该按钮具有两个可变的图片和两个可变的声音,例如:如果单击该按钮,则按钮的背景将显示pic1并播放sound1.wav,然后如果再次单击该按钮,将pic2显示为按钮背景并播放sound2.wav
我可以使用此代码更改按钮背景,但是我不知道如何添加两种播放声音:
Public Class Form1
Dim Index As Integer = 0
Dim Image1 As New List(Of Image)
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Index = Index Xor 1
Button1.BackgroundImage = Image1(Index)
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Image1.Add(Image.FromFile("D:\pic1.png"))
Image1.Add(Image.FromFile("D:\pic2.png"))
End Sub
End Class
答案 0 :(得分:0)
Image类具有tag属性,因此您可以使用它存储列表中要加载的每个图像的声音路径。
Public Class Form1
Dim Index As Integer = 0
Dim Image1 As New List(Of Image)
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Index = Index Xor 1
Button1.BackgroundImage = Image1(Index)
My.Computer.Audio.Play(Image1(Index).Tag, AudioPlayMode.WaitToComplete)
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Image1.Add(Image.FromFile("D:\pic1.png"))
Image1(0).tag = "d:\sound1.wav"
Image1.Add(Image.FromFile("D:\pic2.png"))
Image1(1).tag = "d:\sound2.wav"
End Sub
End Class
希望这会有所帮助