is RadioButton Checked or not

using option strict, array and loops

Page 1 of 1

3 Replies - 21196 Views - Last Post: 31 October 2008 - 03:03 PM Rate Topic: -----

#1 ddollar  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 40
  • Joined: 16-January 08

is RadioButton Checked or not

Posted 31 October 2008 - 09:58 AM

I have a pumpkin picking program that finds the delivery charge for the selected size. The user can select an item from a groupbox using five radio buttons or from a listbox that has the same choices.

My idea was if the user selected an item from the groupbox (radio button) then I would set the same item in the listbox. So if the user selected the first choice the listbox first choice would automatically be selected.

I've managed to loop thru all of the correct objects without working with the CHECKED radio button.

I've seen simular posts that use RadioButton.Checked = True but that creates a problem with Option Strict.

Anyone have any advice on how I can figure out if the current radio button is checked before I compare
its name and highlight the matching listbox item?

With five radio buttons I could just do a CheckChanged for each by was wanting to use the Handles Clause instead.

' the item selected in the listbox should match the radio button selected by user.

' (array) list of possible radio button names.
Dim ListOFNames() As String = {"OneLbRadioButton", "ThreeLbRadioButton", "FiveLbRadioButton", _
											"TenLbRadioButton", "MoreTenLbRadioButton"}
' manifest all controls.
  For Each IsAControl As Control In Controls
	For Each Child As Control In IsAControl.Controls

	' if this is a radio button, do work with it.
	If TypeOf Child Is RadioButton Then

' Should probably find out if the radio button is CHECKED right here. If not no point in next part.

	  ' set index number to match (array) list of names.
	  For i As Integer = 0 To ListOFNames.Length - 1
		' if current child name matches list name, do work with it.
		If String.Equals(Child.Name, ListOFNames(i), StringComparison.InvariantCultureIgnoreCase) Then

			' select mathing listbox item.
			  WeightListBox.SelectedIndex = i
		End If
	  Next
	End If
  Next
Next



Thanks,
ddollar

Is This A Good Question/Topic? 0
  • +

Replies To: is RadioButton Checked or not

#2 dbasnett  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 109
  • View blog
  • Posts: 603
  • Joined: 01-October 08

Re: is RadioButton Checked or not

Posted 31 October 2008 - 10:35 AM

Option Strict On 'what i always use
If RadioButton1.Checked = True Then

End If
doesn't have a problem with this.

it could be
If RadioButton1.Checked

End If

same goes for this
RadioButton1.Checked = True

This post has been edited by dbasnett: 31 October 2008 - 10:36 AM

Was This Post Helpful? 0
  • +
  • -

#3 magicmonkey  Icon User is offline

  • D.I.C Regular

Reputation: 106
  • View blog
  • Posts: 484
  • Joined: 12-September 08

Re: is RadioButton Checked or not

Posted 31 October 2008 - 12:39 PM

You need to cast it to a radiobutton in order to access the Checked property of the radiobutton.

  If TypeOf Child Is RadioButton Then
     ' Should probably find out if the radio button is CHECKED right here. If not no point in next part.
     Dim selRadioButton as RadioButton = DirectCast(Child, RadioButton)
     If selRadioButton.Checked Then




Was This Post Helpful? 0
  • +
  • -

#4 ddollar  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 40
  • Joined: 16-January 08

Re: is RadioButton Checked or not

Posted 31 October 2008 - 03:03 PM

Perfect, Thank You. Solution just in case someone else is an over-thinker.

' (array) list of possible RadioButton names.
Dim ListOFNames() As String = {"OneLbRadioButton", "ThreeLbRadioButton", "FiveLbRadioButton", _
									"TenLbRadioButton", "MoreTenLbRadioButton"}
' approach each control on form.
For Each TypeOfControl As Control In Controls
	' approach each contained control to see if it is a RadioButton.
	For Each ChildType As Control In TypeOfControl.Controls
		' if this is a RadioButton, do work with it.
		If TypeOf ChildType Is RadioButton Then
			' cast ChildType into RadioButton so we can access its CHECKED property.
			Dim StateOfRadioButton As RadioButton = DirectCast(ChildType, RadioButton)
			' if CHECKED do work with it.
			If StateOfRadioButton.Checked = True Then
				' set index number to match (array) list of names.
				For i As Integer = 0 To ListOFNames.Length - 1
					' if current RadioButton name matches list name do work with it.
					If String.Equals(ChildType.Name, ListOFNames(i), StringComparison.InvariantCultureIgnoreCase) Then
						' (highlight) select mathing listbox item.
						WeightListBox.SelectedIndex = i
						' should stop processing when match is found. :)
						Exit Sub
					End If ' end of name matching.
				Next ' end of known length of index numbers.
			End If ' end of CHECKED property setting.
		End If ' end of RadioButton type check.
	Next ' end of RadioButton manifest.
Next ' end of form objects manifest.



ddollar
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1