Q: Radio Buttons

G

Geoff Jones

Hi

I'm hoping that someone can help me with the following question about Radio
Buttons.

In MFC there is a way of attaching a variable to several radio buttons so
that it can be looked at to see which of the radio buttons is checked.
However, in VB.NET, I can't see how to do this, other than doing it manually
in the code e.g.

If RadioButton1.Checked = True
m_radio_button_checked = 1
Else If RadioButton2.Checked = True
m_radio_button_checked = 2
Else.....etc

Is there a similar technique in VB.NET that I can use?

Thanks in advance

Geoff
 
K

Ken Tucker [MVP]

Hi,

You could try something like this. I added a radio button variable to
the form. I made one common checkedchanged event handler. In the
checkedchanged procedure I assign the radio button variable to the currently
check radiobutton. When you press the button it will tell you the name of
the currently checked radio button.

Dim rb As RadioButton

Private Sub RadioButton1_CheckedChanged(ByVal sender As System.Object, ByVal
e As System.EventArgs) Handles RadioButton1.CheckedChanged,
RadioButton2.CheckedChanged, RadioButton3.CheckedChanged,
RadioButton4.CheckedChanged, RadioButton5.CheckedChanged

rb = CType(sender, RadioButton)

End Sub

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

MessageBox.Show(rb.Name.ToString)

End Sub

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

RadioButton1.Checked = True

End Sub



Ken

-------------------------

Hi

I'm hoping that someone can help me with the following question about Radio
Buttons.

In MFC there is a way of attaching a variable to several radio buttons so
that it can be looked at to see which of the radio buttons is checked.
However, in VB.NET, I can't see how to do this, other than doing it manually
in the code e.g.

If RadioButton1.Checked = True
m_radio_button_checked = 1
Else If RadioButton2.Checked = True
m_radio_button_checked = 2
Else.....etc

Is there a similar technique in VB.NET that I can use?

Thanks in advance

Geoff
 
H

Herfried K. Wagner [MVP]

Geoff Jones said:
In MFC there is a way of attaching a variable to several radio buttons so
that it can be looked at to see which of the radio buttons is checked.
However, in VB.NET, I can't see how to do this, other than doing it
manually in the code e.g.

If RadioButton1.Checked = True
m_radio_button_checked = 1
Else If RadioButton2.Checked = True
m_radio_button_checked = 2
Else.....etc

Is there a similar technique in VB.NET that I can use?

\\\
Private m_CheckedRadioButton1 As RadioButton
..
..
..
Private Sub RadioButtons1_CheckedChanged( _
ByVal sender As Object, _
ByVal e As EventArgs _
) _
Handles _
RadioButton1.CheckedChanged, _
RadioButton2.CheckedChanged, _
RadioButton3.CheckedChanged
Dim SourceControl As RadioButton = DirectCast(sender, RadioButton)
If SourceControl.Checked Then
m_CheckedRadioButton1 = SourceControl
End If
End Sub
///
 
G

Geoff Jones

Thanks Ken/Herfried

Many thanks for your suggestion.

So it doesn't look as if the VC++ MFC technique is used in VB.NET. I find
this strange because, as you have indicated, to find which one of several
radio buttons is selected is awkward! Ok, in these particular cases there is
work round using a little code, but it isn't as elegant as the MFC approach
i.e. you can attach a variable to the radio buttons which stores and index
to whichever is selected.

Thanks again for your help.

Geoff
 
C

Cor Ligthert

Geoff,

The radiobutton is typical a control to place in/on a groupbox.

I assume that I make with this everything clear again.

(A simple loop in the groupbox will tell you which is checked).

I hope this helps?

Cor
 
G

Geoff Jones

Hi Cor

Ah, this had crossed my mind but I'm uncertain how to do it. If, as you
suggest, the radio buttons are in a panel, how can you determine which is
selected? Could you give some example code?

Thanks in advance

Geoff
 
C

Cor Ligthert

Geoff,

I type it here so watch typos

\\\
dim checkedbutton as string
for each ctr as control in groupbox1.controls
if typeof ctr is RadioButton then
directcast(ctr,RadioButton).checked = True then
checkedbutton = ctr.name 'or the tag when you set that
exit for
end if
next
////

I hope this helps?

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