468,241 Members | 1,678 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,241 developers. It's quick & easy.

Radio Buttons in Groupboxes - Code check

Hi

I have two radio buttons in a groupbox on a Windows form and have wrote the
code below to find out which button is selected. Can anyone tell me if
there is an easier / better way to code this as it seems a bit long?

Thanks

Rich

Dim rbarray As RadioButton()
Dim strValue As String

rbarray = new RadioButton() {rb1, rb2}

Dim i As Integer

For i = 0 To 1
If rbarray(i).Checked Then
strValue = rbarray(i).Text
End If
Next
Nov 20 '05 #1
2 1096
Hi Rich,

There are a lot of other methods, however your code very short and I think
there i nothing wrong with.

You can also do
\\
for each ctr as mygroupbox.controls
if ctr.Checked Then
strValue = ctr.Text
End If
Next
///
This you can do when there are sure there are only radiobuttons, otherwise
you have to put it in a
\\\
if typeof ctr is radiobutton then
......
end if
///
And than it is again longer

I hope this give some idea's?

Cor
Nov 20 '05 #2
On Tue, 27 Apr 2004 12:27:23 +0100, Rich wrote:
Hi

I have two radio buttons in a groupbox on a Windows form and have wrote the
code below to find out which button is selected. Can anyone tell me if
there is an easier / better way to code this as it seems a bit long?

Thanks

Rich

Dim rbarray As RadioButton()
Dim strValue As String

rbarray = new RadioButton() {rb1, rb2}

Dim i As Integer

For i = 0 To 1
If rbarray(i).Checked Then
strValue = rbarray(i).Text
End If
Next


Another point is inside the If, after setting the value, put Exit For.
Since only one radio button can be selected (all the others would bel
cleared) as soon as you find the one that is selected, there is no need to
continue looking:

For i = 0 To 1
If rbarray(i).Checked Then
strValue = rbarray(i).Text
Exit For
End If
Next
--
Chris

To send me an E-mail, remove the underscores and lunchmeat from my E-Mail
address.
Nov 20 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Craig Keightley | last post: by
4 posts views Thread by Jay | last post: by
2 posts views Thread by jimi_xyz | last post: by
3 posts views Thread by tshad | last post: by
reply views Thread by NPC403 | last post: by
reply views Thread by kermitthefrogpy | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.