Results 1 to 7 of 7

Thread: Restting all Checked "type" controls in containers.

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2009
    Posts
    547

    Restting all Checked "type" controls in containers.

    I know how to do it individually but not sure if it's possible to merge into one.

    I have 6 containers that contain either Checkbox or RadioButtons.

    Struggling sending the type of control and also wondered do i really need to call clear 6 times? Can't VB search each container for a control that can be checked?

    vb Code:
    1. #Region "Form Events"
    2.     Private Sub ClearPizzaCreation(Of t As Control)(ByVal root As Control)
    3.         For Each ctrl As Control In root.Controls
    4.             If TypeOf ctrl Is CheckBox OrElse TypeOf ctrl Is RadioButton Then
    5.                 DirectCast(ctrl, t).Checked = False
    6.             End If
    7.         Next ctrl
    8.     End Sub
    9. #End Region
    10.  
    11.     Private Sub btnStartAgain_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles _
    12.                                                                                                 btnStartAgain.Click
    13.         ClearPizzaCreation(Of CheckBox)(flpnlToppings)
    14.         ClearPizzaCreation(flpnlSize)
    15.         ClearPizzaCreation(flpnlBase)
    16.         ClearPizzaCreation(flpnlDrink)
    17.     End Sub

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,420

    Re: Restting all Checked "type" controls in containers.

    are the container controls all panels?

    vb Code:
    1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    2.     For Each p As Panel In Me.Controls.OfType(Of Panel)()
    3.         For Each cb As CheckBox In p.Controls.OfType(Of CheckBox)()
    4.             cb.Checked = False
    5.         Next
    6.         For Each rb As RadioButton In p.Controls.OfType(Of RadioButton)()
    7.             rb.Checked = False
    8.         Next
    9.     Next
    10. End Sub

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2009
    Posts
    547

    Re: Restting all Checked "type" controls in containers.

    Hi paul they are all flow panels if that helps. Instea dof looping twice inside the loop can it not be sent. The control type i mean?

  4. #4
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,420

    Re: Restting all Checked "type" controls in containers.

    this should work:

    vb Code:
    1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    2.     For Each flp As FlowLayoutPanel In Me.Controls.OfType(Of FlowLayoutPanel)()
    3.         For Each cb As CheckBox In flp.Controls.OfType(Of CheckBox)()
    4.             cb.Checked = False
    5.         Next
    6.         For Each rb As RadioButton In flp.Controls.OfType(Of RadioButton)()
    7.             rb.Checked = False
    8.         Next
    9.     Next
    10. End Sub

  5. #5
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,104

    Re: Restting all Checked "type" controls in containers.

    You will have to loop somewhere.

    There is a theoretical alternative where you use reflection to figure out whether or not there is a .Checked property of the given object, but there is no sound reason to do this. Even without testing, I feel pretty certain that such a loop would perform much worse than the code .Paul. posted.
    My usual boring signature: Nothing

  6. #6

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2009
    Posts
    547

    Re: Restting all Checked "type" controls in containers.

    Works a charm.

    SH i understand completly what you are saying. But i was more getting at i know which flow panel contrains either a check or a radio control. So when i am calling the sub i can send for example

    CallMySub(FlowPanelName, Checkbox)

    or

    CallMySub(FlowPanelName, Radiobutton)

  7. #7
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,104

    Re: Restting all Checked "type" controls in containers.

    Depending on what you are doing, it may not matter whether you pass in a checkbox or a radiobutton. Those two controls are so similar that for most situations, passing in a Control would work as long as it was one of those two types. That may not make anything easier, since it would just be a matter of an OR that wouldn't be so good, but there are cases where it would be useful.
    My usual boring signature: Nothing

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width