Results 1 to 19 of 19

Thread: Radiobutton Cast [RESOLVED]

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2005
    Posts
    384

    Resolved Radiobutton Cast [RESOLVED]

    Hello!
    I'm trying to loop through all the radiobuttons of a form, and clear their checks

    What I'vo got so far is:
    VB Code:
    1. Dim MPrd1 As Control
    2.                 For Each MPrd1 In Me.Controls
    3.                     If TypeOf MPrd1 Is RadioButton Then
    4.                           [b]MPrd1.Checked = False[/b]
    5.                     End If
    6.                 Next

    But now the compiler says that .Checked (the bolded line) is not a member of System.Windows.Forms.Control.
    What am I doing wrong?
    Last edited by GrimmReaper; Feb 14th, 2006 at 08:51 AM.

  2. #2
    Shared Member
    Join Date
    May 2005
    Location
    Kashmir, India
    Posts
    2,277

    Re: Radiobutton Cast

    Well the compiler is correct, Checked is not a member of the Control class, so can't use it. But instead what you can do is this
    VB Code:
    1. Dim rbTemp As RadioButton
    2.         For Each MPrd1 As Control In Me.Controls
    3.             If TypeOf MPrd1 Is RadioButton Then
    4.                 rbTemp = MPrd1
    5.                 rbTemp.Checked = False
    6.             End If
    7.         Next
    Use [code] source code here[/code] tags when you post source code.

    My Articles

  3. #3
    Fanatic Member staticbob's Avatar
    Join Date
    Jan 2005
    Location
    Manchestershire, UK Cabbage: I do
    Posts
    619

    Re: Radiobutton Cast

    Or just replace this line...

    MPrd1.Checked = False

    with...

    CType(MPrd1,radiobutton).Checked = False

    Bob
    "I dislike 7 am. If 7 am were a person, I would punch 7 am in the biscuits." - Paul Ryan, DailyRamblings

  4. #4
    Shared Member
    Join Date
    May 2005
    Location
    Kashmir, India
    Posts
    2,277

    Re: Radiobutton Cast

    How could I forget about CType. Bob's way of doing it is better.
    Use [code] source code here[/code] tags when you post source code.

    My Articles

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2005
    Posts
    384

    Re: Radiobutton Cast

    Ok, Thanx for both replies,
    I have tried both methods, but the radio buttons do not get unchecked
    I've got 5 rd buttons in 1 panel, then I have 3 rd buttons in another panel - do you think that it's the panels causing the problems?

  6. #6
    Shared Member
    Join Date
    May 2005
    Location
    Kashmir, India
    Posts
    2,277

    Re: Radiobutton Cast

    Quote Originally Posted by GrimmReaper
    Ok, Thanx for both replies,
    I have tried both methods, but the radio buttons do not get unchecked
    I've got 5 rd buttons in 1 panel, then I have 3 rd buttons in another panel - do you think that it's the panels causing the problems?
    Well when you have RadioButtons placed on the Panels then you will have to loop through the Controls collection of panel.
    VB Code:
    1. For Each ctTemp As Control In Me.Controls
    2.             If TypeOf ctTemp Is Panel Then
    3.                 For Each rbTemp As Control In CType(ctTemp, Panel).Controls
    4.                     If TypeOf rbTemp Is RadioButton Then
    5.                         CType(rbTemp, RadioButton).Checked = False
    6.                     End If
    7.                 Next
    8.             End If
    9.         Next
    Use [code] source code here[/code] tags when you post source code.

    My Articles

  7. #7
    Fanatic Member staticbob's Avatar
    Join Date
    Jan 2005
    Location
    Manchestershire, UK Cabbage: I do
    Posts
    619

    Re: Radiobutton Cast

    You also need to iterate through the controls in each panel. Something along the lines of....

    VB Code:
    1. For Each ctl As Control In Me.Controls
    2. 'Checks for radiobtns not in panels
    3.             If TypeOf ctl Is RadioButton Then
    4.                 CType(ctl, RadioButton).Checked = False
    5.             End If
    6. 'checks radion buttons in panels
    7.             If TypeOf ctl Is Panel Then
    8.                 For Each pctl As Control In ctl.Controls
    9.                     If TypeOf pctl Is RadioButton Then
    10.                         CType(pctl, RadioButton).Checked = False
    11.                     End If
    12.                 Next
    13.             End If
    14.         Next ctl

    Bob

    ARGH - Beat me to it.
    "I dislike 7 am. If 7 am were a person, I would punch 7 am in the biscuits." - Paul Ryan, DailyRamblings

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: Radiobutton Cast

    Here's how to visit every control on a form regradless of how deep it's nested, coupled with unchecking RadioButtons:
    VB Code:
    1. Dim ctl As Control = Me.GetNextControl(Me, True) 'Get the first control in the tab order.
    2.  
    3.         Do Until ctl Is Nothing
    4.             'Use ctl here.
    5.             If TypeOf ctl Is RadioButton Then
    6.                 DirectCast(ctl, RadioButton).Checked = False
    7.             End If
    8.  
    9.             ctl = Me.GetNextControl(ctl, True) 'Get the next control in the tab order.
    10.         Loop
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  9. #9

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2005
    Posts
    384

    Re: Radiobutton Cast

    Wow!
    I thought that the panel might be the problem - it makes sense to loop through then check if panel etc. because the panel is also a container
    Thanx, my friends - you saved my day!

  10. #10
    Addicted Member
    Join Date
    Jun 2017
    Posts
    165

    Re: Radiobutton Cast [RESOLVED]

    What if I have two radio buttons in a panel and already check one of them with an specific value or name in run time. I need to know the one checked and in a MsgBox or in a textbox the value or name assing to it.

  11. #11
    Addicted Member
    Join Date
    Jun 2017
    Posts
    165

    Re: Radiobutton Cast [RESOLVED]

    Dim rbttnM As New RadioButton()
    Dim rbttnF As New RadioButton()
    For Each ctrl As Control In Me.Controls("pnlMainPanel").Controls
    If ctrl.GetType Is GetType(System.Windows.Forms.Panel) Then
    For Each subCtrl As Control In ctrl.Controls
    If subCtrl.GetType Is GetType(System.Windows.Forms.TextBox) Then
    MsgBox(subCtrl.Text)
    End If

    If subCtrl.GetType Is GetType(System.Windows.Forms.ComboBox) Then
    MsgBox(subCtrl.Text)
    End If
    If subCtrl.GetType Is GetType(System.Windows.Forms.RadioButton) Then
    If CType(subCtrl.Controls("rbttnM"), RadioButton).Checked Then
    MsgBox("Male")
    End If
    If CType(subCtrl.Controls("rbttnF"), RadioButton).Checked Then
    MsgBox("Female")
    End If
    End If
    Next
    End If
    Next
    End Sub

  12. #12
    Addicted Member
    Join Date
    Jun 2017
    Posts
    165

    Re: Radiobutton Cast [RESOLVED]

    In code line If CType(subCtrl.Controls("rbttnM"), RadioButton).Checked Then error :

    An unhandled exception of type 'System.NullReferenceException' occurred

    Additional information: Object reference not set to an instance of an object.

  13. #13
    Addicted Member
    Join Date
    Jun 2017
    Posts
    165

    Re: Radiobutton Cast [RESOLVED]

    Code:
    Dim rbttnM As New RadioButton()
            Dim rbttnF As New RadioButton()
            For Each ctrl As Control In Me.Controls("pnlMainPanel").Controls
                If ctrl.GetType Is GetType(System.Windows.Forms.Panel) Then
                    For Each subCtrl As Control In ctrl.Controls
                        If subCtrl.GetType Is GetType(System.Windows.Forms.TextBox) Then
                            MsgBox(subCtrl.Text)
                        End If
    
                        If subCtrl.GetType Is GetType(System.Windows.Forms.ComboBox) Then
                            MsgBox(subCtrl.Text)
                        End If
                        If subCtrl.GetType Is GetType(System.Windows.Forms.RadioButton) Then
                            If CType(subCtrl.Controls("rbttnM"), RadioButton).Checked Then
                                MsgBox("Male")
                            End If
                            If CType(subCtrl.Controls("rbttnF"), RadioButton).Checked Then
                                MsgBox("Female")
                            End If
                        End If
                    Next
                End If
            Next
        End Sub

  14. #14
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: Radiobutton Cast [RESOLVED]

    Quote Originally Posted by ebellounisoft View Post
    In code line If CType(subCtrl.Controls("rbttnM"), RadioButton).Checked Then error :

    An unhandled exception of type 'System.NullReferenceException' occurred

    Additional information: Object reference not set to an instance of an object.
    That means that that container has no control with that name in it.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  15. #15
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: Radiobutton Cast [RESOLVED]

    Quote Originally Posted by ebellounisoft View Post
    Code:
    Dim rbttnM As New RadioButton()
            Dim rbttnF As New RadioButton()
            For Each ctrl As Control In Me.Controls("pnlMainPanel").Controls
                If ctrl.GetType Is GetType(System.Windows.Forms.Panel) Then
                    For Each subCtrl As Control In ctrl.Controls
                        If subCtrl.GetType Is GetType(System.Windows.Forms.TextBox) Then
                            MsgBox(subCtrl.Text)
                        End If
    
                        If subCtrl.GetType Is GetType(System.Windows.Forms.ComboBox) Then
                            MsgBox(subCtrl.Text)
                        End If
                        If subCtrl.GetType Is GetType(System.Windows.Forms.RadioButton) Then
                            If CType(subCtrl.Controls("rbttnM"), RadioButton).Checked Then
                                MsgBox("Male")
                            End If
                            If CType(subCtrl.Controls("rbttnF"), RadioButton).Checked Then
                                MsgBox("Female")
                            End If
                        End If
                    Next
                End If
            Next
        End Sub
    That code looks rather nonsensical. For one thing, it starts out by creating two RadioButton objects that will never be used.
    Quote Originally Posted by ebellounisoft View Post
    What if I have two radio buttons in a panel and already check one of them with an specific value or name in run time. I need to know the one checked and in a MsgBox or in a textbox the value or name assing to it.
    That question has a very simple answer. You certainly don't need code nearly as complex as that above. If you only have two RadioButtons then just use a simple If...ElseIf statement. The container is then irrelevant.
    vb.net Code:
    1. If firstRadioButton.Checked Then
    2.     '...
    3. ElseIf secondRadioButton.Checked Then
    4.     '...
    5. End If
    If you want to handle an arbitrary number of RadioButtons then you can do this:
    vb.net Code:
    1. Dim checkedRadioButton = myContainer.Controls.OfType(Of RadioButton)().SingleOrDefault(Function(rb) rb.Checked)
    2.  
    3. If checkedRadioButton IsNot Nothing Then
    4.     '...
    5. End If
    You might use the Text property of the RadioButton or, if you want a different value, you can set the Tag in the designer and then use that. If you know for a fact that a RadioButton will be checked then use Single instead of SingleOrDefault and omit the test for Nothing.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  16. #16
    Addicted Member
    Join Date
    Jun 2017
    Posts
    165

    Re: Radiobutton Cast [RESOLVED]

    CAN SOME ONE HELP ME. HOW DO I START A THREAD. I ASK FOR HELP IN CONTACT US OF VBForums BUT NO ANSWER. WHAT LINK IN MAIN PAGE HAVE TO CLICK TO START NEW THREAD. THANK YOU.

  17. #17
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Radiobutton Cast [RESOLVED]

    To start a new thread, go to the forum you want to post it in (probably Visual Basic .Net), then click on the "Post New Thread" button.

  18. #18
    Addicted Member
    Join Date
    Jun 2017
    Posts
    165

    Thumbs up Re: Radiobutton Cast [RESOLVED]

    Got it. Thank you !!!.

  19. #19
    Addicted Member
    Join Date
    Jun 2017
    Posts
    165

    Re: Radiobutton Cast [RESOLVED]

    I can see if it is checked. That is easy,assining a variable to the checked button but what is the code to get the text of the button checked ?. I Try to use MsgBox (ctrl.Text) for the Male button and MsgBox (ctrl.Text) for the Female button, for example, but get an error that can convert a promt to a string.

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