By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,626 Members | 2,202 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,626 IT Pros & Developers. It's quick & easy.

Radio Buttons in Groupboxes - Code check

P: n/a
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
Share this Question
Share on Google+
2 Replies


P: n/a
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

P: n/a
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.