How to change the image of an Image Button once clicked?

224 Views Asked by At

I am trying to change the image of my Bunifu Image Button once it is clicked, because it's a volume on/off button. But my code has an exception that says the "path is not a legal form".

Here is my code:

Private Sub BtnMute_Click(sender As Object, e As EventArgs) Handles BtnMute.Click

    Dim OpenFileDialog As New OpenFileDialog
    Dim Filename = "D:\Storage\Team Tag\Corporation\Corporation 3D Logo (PNG).png"

    Dim mute As New System.Drawing.Bitmap(OpenFileDialog.FileName)

    If MissionBriefingPlayer.settings.volume > 0 Then
        MissionBriefingPlayer.settings.volume = 0
        BtnMute.Image = mute
    Else
        MissionBriefingPlayer.settings.volume = 100
    End If

End Sub
1

There are 1 best solutions below

0
Chase Rocker On BEST ANSWER

The OpenFileDialog isn't needed.

Here's a better method. Also, another thing you can do is instead of setting the volume to 100 when you unmute, save the volume in a static variable before muting it, then when you unmute, set it back to the saved volume:

Private Sub BtnMute_Click(sender As Object, e As EventArgs) Handles BtnMute.Click

    Static _savedVolume as Integer = 0

    If MissionBriefingPlayer.settings.volume > 0 Then
        _savedVolume = MissionBriefingPlayer.settings.volume

        MissionBriefingPlayer.settings.volume = 0

        BtnMute.Image = New System.Drawing.Bitmap("D:\Storage\Team Tag\Corporation\Corporation 3D Logo (PNG).png")
    Else
        MissionBriefingPlayer.settings.volume = _savedVolume
    End If

End Sub