Turning a radio button into a toggle button

J

Jeff Ciaccio

I would like to create a toggle button, so I chose a radio and set its
apperance to button. Is seems my method always makes this .checked = false.
Can somebody help me to see the error in my ways? I'm guessing that I just
need an event that triggers before it actually changes the value of the
button.

Private Sub radSound_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles radSound.Click
If radSound.Checked = True Then radSound.Checked = False Else
radSound = True
End Sub
 
J

Jeff Ciaccio

I've got a very clunky workaroud: two buttons "Sound" and "No Sound" (see
code), but there's go to be a more elegant way :) Thanks!

Private Sub radSound_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles radSound.Click
radSound.Checked = True
radNoSound.Checked = False
radSound.Visible = False
radNoSound.Visible = True
End Sub
Private Sub radNoSound_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles radNoSound.Click
radNoSound.Checked = True
radSound.Checked = False
radNoSound.Visible = False
radSound.Visible = True
End Sub
 
A

Andrew Cooper

Jeff said:
I would like to create a toggle button, so I chose a radio and set its
apperance to button. Is seems my method always makes this .checked =
false. Can somebody help me to see the error in my ways? I'm guessing
that I just need an event that triggers before it actually changes the
value of the button.

Private Sub radSound_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles radSound.Click
If radSound.Checked = True Then radSound.Checked = False Else
radSound = True
End Sub

Jeff,

I'd use a Checkbox control with the Appearance set to Button instead of
the Radio control. Then you don't need any code to control what it
looks like you are trying to do. If you can tell me what you are
attempt to accomplish with the code, I'd be very willing to attempt to
help you more.

Andrew Cooper
 
A

Andrew Cooper

Jeff said:
Hey Andrew,

I just want to allow the user to toggle the sound on and off.

That's what I thought. Use the Checkbox control and not the Radio
control. Then all you need is the following event.

Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged

If CheckBox1.Checked = True Then
'Turn sound on.
Else
'Turn sound off.
End If
End Sub
 
A

Andrew Cooper

Jeff said:
Thanks - that did the trick. Why I didn't think of that ... :(
Jeff,

Everything seems obvious after the fact. :) If we all posted the times
we overlooked something simple everyone one of us would look pretty silly.

Andrew
 
K

kimiraikkonen

Hey Andrew,

I just want to allow the user to toggle the sound on and off.

You can use a checkbox to do it even with its text:

Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged
If CheckBox1.Checked = True Then
' Turn on sound - code here
' Also,
CheckBox1.Text = "Sound: ON"
ElseIf CheckBox1.Checked = False Then
'Turn off sound
' Also,
CheckBox1.Text = "Sound: OFF"
End If

End Sub

And still if you want to use radio buttons, then you'd need 2 radio
buttons like named "radiobutton1" and "radiobutton2" :

Private Sub RadioButton1_CheckedChanged(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
RadioButton1.CheckedChanged
If RadioButton1.Checked = True Then
'Turn on Sound - Code here
MsgBox("Sound is turned ON")
End If
End Sub

Private Sub RadioButton2_CheckedChanged(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles RadioButton2.CheckedChanged
If RadioButton2.Checked = True Then
' Turn off Sound - Code here
MsgBox("Sound is turned OFF")
End If
End Sub


Hope this helps,

Onur Güzel
 
C

Cor Ligthert[MVP]

Everything seems obvious after the fact. :) If we all posted the times
we overlooked something simple everyone one of us would look pretty silly.

Andrew

Right

:)

Cor
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top