10 Replies - 18100 Views - Last Post: 20 February 2012 - 03:55 PM Rate Topic: -----

#1 ddollar  Icon User is offline

  • New D.I.C Head

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

RadioButton's - Clear them all but not one at the time

Post icon  Posted 12 September 2008 - 08:33 AM

Maybe a windows form doesn't usually hold that many radio buttons. You only need so many; say four different sizes you would need a radio button for each one. In my case I have two GroupBox Containers each with 3 or 4 RadioButtons. If I have a reset button (giving the user a method of starting over) that clears any selections the user made, I have 7 lines to 'uncheck' each item individually.

Like this;

FirstRadioButton.Checked = False
SecondRadioButton.Checked = False
ThirdRadioButton.Checked = False
etc..

I always wondered if there is another method of clearing 'all' RadioButtons, or TextBoxes, or CheckBoxes more efficeintly; like a single line of code that works on the object type? I've searched around and can't find anything.

Thanks for any advise.
ddollar

Is This A Good Question/Topic? 1

Replies To: RadioButton's - Clear them all but not one at the time

#2 MrWobbles  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 31
  • View blog
  • Posts: 328
  • Joined: 11-April 08

Re: RadioButton's - Clear them all but not one at the time

Posted 12 September 2008 - 08:39 AM

You could try a loop

 Dim rBTN As RadioButton

		For Each rBTN In Me.Controls
			rBTN.Checked = False
		Next


Was This Post Helpful? 0
  • +
  • -

#3 ddollar  Icon User is offline

  • New D.I.C Head

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

Re: RadioButton's - Clear them all but not one at the time

Posted 12 September 2008 - 09:52 AM

Right. Thank you. It threw a InvalidCastException at me.

Unable to cast object of type 'System.Windows.Forms.GroupBox' to type 'System.Windows.Forms.RadioButton'.
Was This Post Helpful? 0
  • +
  • -

#4 MrWobbles  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 31
  • View blog
  • Posts: 328
  • Joined: 11-April 08

Re: RadioButton's - Clear them all but not one at the time

Posted 12 September 2008 - 10:35 AM

Dim rBTN As RadioButton
		Dim gBOX As GroupBox

		For Each gBOX In Me.Controls
			For Each rBTN In gBOX.Controls
						  If rBTN.getType = "System.RadioButton" Then
				rBTN.Checked = False
						  End If
			Next
		Next



Try this one :P

This post has been edited by MrWobbles: 12 September 2008 - 10:36 AM

Was This Post Helpful? 0
  • +
  • -

#5 ddollar  Icon User is offline

  • New D.I.C Head

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

Re: RadioButton's - Clear them all but not one at the time

Posted 12 September 2008 - 10:52 AM

I appreciate your help. Looks like type cannot be converted to string?


Overload resolution failed because no accessible '=' can be called with these arguments:
'Public Shared Operator =(a As String, b As String) As Boolean': Value of type 'System.Type' cannot be converted to 'String'.
Was This Post Helpful? 0
  • +
  • -

#6 MrWobbles  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 31
  • View blog
  • Posts: 328
  • Joined: 11-April 08

Re: RadioButton's - Clear them all but not one at the time

Posted 12 September 2008 - 10:57 AM

Sorry - it should be rBTN.getType.ToString
Was This Post Helpful? 1

#7 ddollar  Icon User is offline

  • New D.I.C Head

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

Re: RadioButton's - Clear them all but not one at the time

Posted 20 September 2008 - 08:19 AM

Hi and thanks for your help. I'm still working on basically the same problem but am working with textboxes.

I have an input box, a textbox, that I need to not clear when I loop to clear all others of the same type.

I have a piece of functioning code that works on each control of type TextBox. Is there a method of excluding one?
I know its name; I've tried to work with that but that's not working out.

My attempt was a nested if statement that inspected the name of the particular textbox. The error returned was the
control name can not be converted to string.

This is what I have;

		' Excluding StoreSalesAmountTextBox

		For Each specificTextbox In Me.Controls
			If TypeOf specificTextbox Is TextBox Then
				Dim theSpecificTextbox As TextBox = DirectCast(specificTextbox, TextBox)

' nested if statement here to exclude one particular textbox
' if its not the correct box, clear it, else do nothing and move on
' i think i need to manifest the names of the textboxes, does that sound right?

				theSpecificTextbox.Clear()

'end if
			End If
		Next
	End Sub

Thanks again for any advise.
ddollar


Was This Post Helpful? 0
  • +
  • -

#8 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1642
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: RadioButton's - Clear them all but not one at the time

Posted 20 September 2008 - 08:44 AM

Here is some code that will help. This code loops through all the controls on the form. In the loop it looks for type TextBox, once it finds a TextBox it checks it's name to see if it's the one TextBox you don't want to remove the text from. If it's that TextBox it skips it and moves on to the next one.

For the name I use "TextBoxToExclude", you'll need to replace this with the name of the TextBox you want left alone

Private Sub ClearTextBoxes()
'Loop through all the controls on the form
For Each ctrl As Control In Me.Controls
    'Check and see if this control is a TextBox
    If Object.ReferenceEquals(ctrl.GetType(), GetType(TextBox)) Then
        'Check and make sure this is the "TextBoxToExclude"
        If Not String.Equals(ctrl.Name, "TextBoxToExclude",  StringComparison.InvariantCultureIgnoreCase) Then
            'Since this isnt the TextBox we dont want to mess
            'with clear out the text in it
            CType(ctrl, TextBox).Text = String.Empty
        End If
    End If
Next

End Sub



Now this could be modified to accept multiple TextBoxes to leave alone. To Do this (an example is below) create a string array holding the names of the TextBoxes you want left alone, then in your loop add a second loop that will loop through the array of names. Something like

Private Sub ClearTextBoxes()
Dim toExclude() As String = {"TextBox1", "TextBox2", "TextBox3"}
'Loop through all the controls on the form
For Each ctrl As Control In Me.Controls
    'Check and see if this control is a TextBox
    If Object.ReferenceEquals(ctrl.GetType(), GetType(TextBox)) Then
        'Now we are going to loop through all the names
        'in our exclude array
        For i As Integer = 0 To toExclude.Length
            'Make sure this name isnt in our list
            If Not String.Equals(ctrl.Name, toExclude(i), StringComparison.InvariantCultureIgnoreCase) Then
                'Since this isnt the TextBox we dont want to mess
                'with clear out the text in it
                CType(ctrl, TextBox).Text = String.Empty
            End If
        Next
    End If
Next
End Sub


Was This Post Helpful? 1
  • +
  • -

#9 ddollar  Icon User is offline

  • New D.I.C Head

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

Re: RadioButton's - Clear them all but not one at the time

Posted 20 September 2008 - 09:33 AM

Perfect. Very nice example of clearing one or many objects of the same type. Thank you.

ddollar
Was This Post Helpful? 0
  • +
  • -

#10 mcneillyjr@yahoo.com  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 20-February 12

Re: RadioButton's - Clear them all but not one at the time

Posted 20 February 2012 - 03:37 PM

View PostMrWobbles, on 12 September 2008 - 08:39 AM, said:

You could try a loop

 Dim rBTN As RadioButton

		For Each rBTN In Me.Controls
			rBTN.Checked = False
		Next



This is the easy way to clear radio buttons put them in a group box then
[Dim Rbtn As RadioButton]


[For Each Rbtn In NumberYearsGroupBox.Controls

If TypeOf Rbtn Is RadioButton Then Rbtn.Checked = False


Next]
Was This Post Helpful? 0
  • +
  • -

#11 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1606
  • View blog
  • Posts: 5,163
  • Joined: 25-September 09

Re: RadioButton's - Clear them all but not one at the time

Posted 20 February 2012 - 03:55 PM

Did you happen to notice that post was not only resolved but that it was over 3 years old?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1