views:

325

answers:

6

Hi, this is more of an advise thread I guess. I've been wondering how one could create a button which display "play" when it's not pressed. And then shows "pause" once it's pressed. And visa versa when it's pressed again.

I had a similar problem when trying to create an expand panel button, but that was easy because I could just set a variable to true or false if PanelCollapsed was true.

But in this case I couldn't find any property in a button that I could query. So I came up with this but I can't help thinking that this is a rather unsmart way of doing it?

    If isPlay = True Then
        If isPaused = False Then
            btnPlay.Image = Image.FromFile("iconPause.png")
            isPaused = True
            isPlay = False
        End If
        GoTo Endline
    End If

    If isPlay = False Then
        If isPaused = True Then

            btnPlay.Image = Image.FromFile("iconPlay.png")
            isPaused = False
            isPlay = True
        End If
    End If

Endline:

+1  A: 

Most .NET WinForm controls have a 'Tag' property (a button has one). You can set the Tag to be anything you want. An easy way to do this is to set the 'Tag' property to a boolean with the state of the button.

Just an idea...sure there are many other approaches.

UPDATE: Otherwise, you can maintain the state of the button in your application as its own member variable. This might have several advantages because you can pass this state to other controls that might need it. The only weakness with this approach is that the state must be maintained separately.

If you have a fairly straight-forward implementation, use the Tag property.

j0rd4n
Tag is not strong typed. Member field is a better approach.
Adrian Godong
A: 

I don't personally use Visual Basic, but I do know that Buttons in Windows Forms have a property called 'Tag'. It is of the generic object type, so you can save whatever state you want, and just use casting to get the value back out.

Tim Rupe
+7  A: 

How about using only one variable and code like this:

If isPlay Then
    btnPlay.Image = Image.FromFile("iconPause.png")
else
    btnPlay.Image = Image.FromFile("iconPlay.png")
End If

isPlay = not isPlay
Nick D
Ha, about to put similar answer. :D +1
Adrian Godong
+2  A: 

You can use the "Tag" property. Its type is "object" so you can use any object you want, but in your case a string will do:

    If Button1.Tag = "Pause" Then
        Button1.Image = Image.FromFile("iconPlay.png")
        Button1.Tag = "Play"
    Else
        Button1.Image = Image.FromFile("iconPause.png")
        Button1.Tag = "Pause"
    End If
Rodrigo Sieiro
Just tried this and it works alot better! :) The code was way shorter as well :)
Kenny Bones
A: 

How about using the "Image" property?

Rem form initialization
ImagePlay = Image.FromFile("iconPlay.png")
ImagePause = Image.FromFile("iconPause.png")
Button1.Image = ImagePlay
.
.
.
Rem on button1 click
If Button1.Image = ImagePlay Then
  Button1.Image = ImagePause
Else
  Button1.Image = ImagePlay
End If
Massa
A: 

A contrary opinion ...

... while other answers have given you some techniques to achieve your desired result, I'm going to ask you to reconsider your UI design.

Dual state buttons - ones that alternate purpose when clicked - can be a source of user frustation.

Here are two scenarios.

Scenario #1 ... if the users machine is under load (for any reason), there may be a perceptible delay between the users actual click on your button and when your click handler is executed.

Normally the time between click and handler is a few milliseconds or less, but it can run to several seconds. If this happens when the user clicks on a dual state button, they are likely to click the button again. Net effect, when the application catches up, is to toggle on, then immediately off again.

Scenario #2 ... many users habitually double click everything. Even experienced users who've been using computers for years may have this weird habit. When they try to press a dual state button, guess what happens ... the action toggles on, then immediately off again.

There are at least two solutions ...

Solution #1 ... use two buttons, one for "On", one for "Off".

Solution #2 ... write some debouncing code to suppress the effect of a second click if processed immediately (ie: < 75ms) after the first.

Bevan
Good points. But my application is very simple and it only meant to be used by me and a couple of friends of mine.I've seen several people say that dual state buttons are a weak design implementation. But I think it looks alot better in the UI and most media players have it as well.
Kenny Bones