Page 1 of 2 12 LastLast
Results 1 to 40 of 47

Thread: How can I call a this Function that returns a RadioButtom name in a GroupBox

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jun 2017
    Posts
    165

    How can I call a this Function that returns a RadioButtom name in a GroupBox

    Code:
    Private Function WhatRadioIsSelected(ByVal grp As GroupBox) As String
            Dim rbtn As RadioButton
            Dim rbtnName As String = String.Empty
            Try
                Dim ctl As Control
                For Each ctl In grp.Controls
                    If TypeOf ctl Is RadioButton Then
                        rbtn = DirectCast(ctl, RadioButton)
                        If rbtn.Checked Then
                            rbtnName = rbtn.Name
                            Exit For
                        End If
                    End If
                Next
            Catch ex As Exception
                Dim stackframe As New Diagnostics.StackFrame(1)
                Throw New Exception("An error occurred in routine, '" & stackframe.GetMethod.ReflectedType.Name & "." & System.Reflection.MethodInfo.GetCurrentMethod.Name & "'." & Environment.NewLine & "  Message was: '" & ex.Message & "'")
            End Try
            Return rbtnName
        End Function

  2. #2

    Thread Starter
    Addicted Member
    Join Date
    Jun 2017
    Posts
    165

    Re: How can I call a this Function that returns a RadioButtom name in a GroupBox

    I try this Sub:

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Jun 2017
    Posts
    165

    Re: How can I call a this Function that returns a RadioButtom name in a GroupBox

    Code:
    Dim gbExam As New GroupBox
            WhatRadioIsSelected(gbExam)
            MsgBox(gbExam.ToString).ToString()

  4. #4
    Fanatic Member Arve K.'s Avatar
    Join Date
    Sep 2008
    Location
    Kyrksæterøra, Norway
    Posts
    518

    Re: How can I call a this Function that returns a RadioButtom name in a GroupBox

    vb Code:
    1. MsgBox(WhatRadioIsSelected(YourGroupBox))
    Last edited by Arve K.; Jun 26th, 2017 at 10:44 AM.
    Arve K.

    Please mark your thread as resolved and add reputation to those who helped you solve your problem
    Disclaimer: I am not a professional programmer

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Jun 2017
    Posts
    165

    Re: How can I call a this Function that returns a RadioButtom name in a GroupBox

    Works !!!. Your are the best. Thank you !!!.

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Jun 2017
    Posts
    165

    Re: How can I call a this Function that returns a RadioButtom name in a GroupBox

    Works !!!. Your are the best. Thank you !!!

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Jun 2017
    Posts
    165

    Re: How can I call a this Function that returns a RadioButtom name in a GroupBox

    _powerade Thank you !!!. Got stuck for 3 days jeje.

  8. #8
    Fanatic Member Arve K.'s Avatar
    Join Date
    Sep 2008
    Location
    Kyrksæterøra, Norway
    Posts
    518

    Re: How can I call a this Function that returns a RadioButtom name in a GroupBox

    np

    Personally, I would consider replacing MsgBox with the Messagebox class, since that is the dot Net way to show messages.
    Last edited by Arve K.; Jun 26th, 2017 at 01:26 PM.
    Arve K.

    Please mark your thread as resolved and add reputation to those who helped you solve your problem
    Disclaimer: I am not a professional programmer

  9. #9

    Thread Starter
    Addicted Member
    Join Date
    Jun 2017
    Posts
    165

    Re: How can I call a this Function that returns a RadioButtom name in a GroupBox

    I'm back !!!. Hello all. Me again. MsgBox(WhatRadioIsSelected(groupboxSex)) works ok in a Form project with a GroupBox, two RadioButtons (Male, Female) and a Button_Click to call the WhatRadioIsSelected(groupboxSex)) Function. I am half way to solve a problem I have been working. Here comes the real challenge. The GroupBox is a control inside (in) a Child panel which is inside (in) a Main Panel. The RadioButtons are inside the Child panel, with a TextBox for a Name, ComboBox for Age and two RadioButtons for Gender. It is a DynamicDataEntry project. Works ok adding or removing child panels with independent data. I use Button_Click to read (get) data in each child panel , and I get the Name and the Age but the gender shows a blank dialogbox . This is the code :
    Last edited by ebellounisoft; Jun 26th, 2017 at 06:32 PM.

  10. #10

    Thread Starter
    Addicted Member
    Join Date
    Jun 2017
    Posts
    165

    Re: How can I call a this Function that returns a RadioButtom name in a GroupBox

    Code:
    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
                    Next
                    MsgBox(WhatRadioIsSelected(groupboxSex))
                End If
            Next

  11. #11
    Fanatic Member Arve K.'s Avatar
    Join Date
    Sep 2008
    Location
    Kyrksæterøra, Norway
    Posts
    518

    Re: How can I call a this Function that returns a RadioButtom name in a GroupBox

    I replicated the code you provided in this thread, and I got the name of the selected Radiobutton when I clicked the button, is that the result you want your code to produce?

    Screenshot
    Arve K.

    Please mark your thread as resolved and add reputation to those who helped you solve your problem
    Disclaimer: I am not a professional programmer

  12. #12

    Thread Starter
    Addicted Member
    Join Date
    Jun 2017
    Posts
    165

    Re: How can I call a this Function that returns a RadioButtom name in a GroupBox

    Still do not get it. This my project form :
    Name:  DynamiDataEntry.jpg
Views: 469
Size:  31.0 KB

  13. #13
    Fanatic Member Arve K.'s Avatar
    Join Date
    Sep 2008
    Location
    Kyrksæterøra, Norway
    Posts
    518

    Re: How can I call a this Function that returns a RadioButtom name in a GroupBox

    I am still a little confused about what result you want, but if it is the name of the selected Radiobutton control, then try this and see if it works for you:

    vb.net Code:
    1. Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
    2.     For Each control As Control In Me.groupboxSex.Controls.OfType(Of RadioButton)
    3.         If DirectCast(control, RadioButton).Checked Then MessageBox.Show(control.Name)
    4.     Next
    5. End Sub
    Last edited by Arve K.; Jun 26th, 2017 at 07:20 PM.
    Arve K.

    Please mark your thread as resolved and add reputation to those who helped you solve your problem
    Disclaimer: I am not a professional programmer

  14. #14

    Thread Starter
    Addicted Member
    Join Date
    Jun 2017
    Posts
    165

    Re: How can I call a this Function that returns a RadioButtom name in a GroupBox

    Sorry !!!. My mistake. the RadioButton names are rbttnM for Male and rbttnF for Female. With an InputBox a put the gender of each RadioButton text property in the texbox. Male or Female. Please excuse my mistake
    Last edited by ebellounisoft; Jun 26th, 2017 at 07:31 PM.

  15. #15
    Fanatic Member Arve K.'s Avatar
    Join Date
    Sep 2008
    Location
    Kyrksæterøra, Norway
    Posts
    518

    Re: How can I call a this Function that returns a RadioButtom name in a GroupBox

    Uhm, so what you want is to show an InputBox with the selected gender in the InputBox's text field when you click the button?

    Or do you want to display the InputBox and type the gender, and then check the corresponding RadioButton based on what you typed in the InputBox?
    Last edited by Arve K.; Jun 26th, 2017 at 07:43 PM.
    Arve K.

    Please mark your thread as resolved and add reputation to those who helped you solve your problem
    Disclaimer: I am not a professional programmer

  16. #16

    Thread Starter
    Addicted Member
    Join Date
    Jun 2017
    Posts
    165

    Re: How can I call a this Function that returns a RadioButtom name in a GroupBox

    The gender is already in the TexBox as you can see that I put in using an InputBox. What I need is to iterate in the child panel with the code to get in a MsgBox the text for the checked button (get Male if checked or Female if checked)

  17. #17
    Fanatic Member Arve K.'s Avatar
    Join Date
    Sep 2008
    Location
    Kyrksæterøra, Norway
    Posts
    518

    Re: How can I call a this Function that returns a RadioButtom name in a GroupBox

    Replace .Name with .Text
    Arve K.

    Please mark your thread as resolved and add reputation to those who helped you solve your problem
    Disclaimer: I am not a professional programmer

  18. #18

    Thread Starter
    Addicted Member
    Join Date
    Jun 2017
    Posts
    165

    Re: How can I call a this Function that returns a RadioButtom name in a GroupBox

    Replace it, but still a Blank dialogbox. I am going to continue tomorrow (here time is 8:00 p.m) and kind of tired. Thank you very much. See U

  19. #19
    Fanatic Member Arve K.'s Avatar
    Join Date
    Sep 2008
    Location
    Kyrksæterøra, Norway
    Posts
    518

    Re: How can I call a this Function that returns a RadioButtom name in a GroupBox

    Ok, I think there must be issues with your code somewhere else then

    This works for me:
    vb.net Code:
    1. Private Function WhatRadioIsSelected(ByVal grp As GroupBox) As String
    2.     Dim result As String = Nothing
    3.  
    4.     For Each control As Control In grp.Controls.OfType(Of RadioButton)
    5.         If DirectCast(control, RadioButton).Checked Then result = control.Text
    6.     Next
    7.  
    8.     Return result
    9. End Function

    Screenshot
    Arve K.

    Please mark your thread as resolved and add reputation to those who helped you solve your problem
    Disclaimer: I am not a professional programmer

  20. #20

    Thread Starter
    Addicted Member
    Join Date
    Jun 2017
    Posts
    165

    Re: How can I call a this Function that returns a RadioButtom name in a GroupBox

    I worked your code in other project and works fine(replicate yours). Just don't get why I get in mine the messagebox but blank using both WhatRadioIsSelected Function examples. Here is the real issue.Could it be a problem of Conversion ?. DirectCast ? . Going to keep working on it.

  21. #21
    Fanatic Member Arve K.'s Avatar
    Join Date
    Sep 2008
    Location
    Kyrksæterøra, Norway
    Posts
    518

    Re: How can I call a this Function that returns a RadioButtom name in a GroupBox

    I don't think there is much more to do without seeing more of your code. Also, you should read this article about debugging, which may help you solve your problem.
    Arve K.

    Please mark your thread as resolved and add reputation to those who helped you solve your problem
    Disclaimer: I am not a professional programmer

  22. #22
    PowerPoster ChrisE's Avatar
    Join Date
    Jun 2017
    Location
    Frankfurt
    Posts
    3,129

    Re: How can I call a this Function that returns a RadioButtom name in a GroupBox

    well I tried _powerade's code an it works perfect.

    you can try this as an option, if this also wont work, then there must be aproblem somewhere in you code

    try this
    Code:
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Select Case True
                Case RadioButton1.Checked : Label1.Text = "Male"
                Case RadioButton2.Checked : Label1.Text = "Female"
                Case RadioButton3.Checked : Label1.Text = "test3"
                Case RadioButton4.Checked : Label1.Text = "test4"
                Case Else : Label1.Text = ".....blank...."
            End Select
            'MsgBox(Label1.Text)
        End Sub
    regards
    Chris

  23. #23

    Thread Starter
    Addicted Member
    Join Date
    Jun 2017
    Posts
    165

    Re: How can I call a this Function that returns a RadioButtom name in a GroupBox

    Your code works easy with RadioButtons in a Form. It is more complicated than that. The GroupBox is a control inside (in) a Child panel which is inside (in) a Main Panel (parent panel). The RadioButtons are inside the Child panel, with a TextBox for a Name, ComboBox for Age and two RadioButtons for Gender. It is a DynamicDataEntry project. Works ok adding or removing child panels with independent data. The RadioButton is checked at run time in a Sub_Click. The problem I have is to get the text of the RadioButton checked at run time with another Sub that calls a Function that returns the text.

  24. #24

    Thread Starter
    Addicted Member
    Join Date
    Jun 2017
    Posts
    165

    Re: How can I call a this Function that returns a RadioButtom name in a GroupBox


  25. #25
    Frenzied Member
    Join Date
    Jul 2011
    Location
    UK
    Posts
    1,335

    Re: How can I call a this Function that returns a RadioButtom name in a GroupBox

    This is your code from Post#10:
    Code:
    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
            Next
            MsgBox(WhatRadioIsSelected(groupboxSex))
        End If
    Next
    The highlighted line is in the wrong place. You have to find the GroupBox the same way as you found the other Controls in your child Panel.:

    Code:
    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.GroupBox) Then
                    MsgBox(WhatRadioIsSelected(DirectCast(subCtrl, GroupBox)))
                End If
    
            Next
    
        End If
    Next

  26. #26

    Thread Starter
    Addicted Member
    Join Date
    Jun 2017
    Posts
    165

    Re: How can I call a this Function that returns a RadioButtom name in a GroupBox

    Quote Originally Posted by Inferrd View Post
    This is your code from Post#10:
    Code:
    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
            Next
            MsgBox(WhatRadioIsSelected(groupboxSex))
        End If
    Next
    The highlighted line is in the wrong place. You have to find the GroupBox the same way as you found the other Controls in your child Panel.:

    Code:
    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.GroupBox) Then
                    MsgBox(WhatRadioIsSelected(DirectCast(subCtrl, GroupBox)))
                End If
    
            Next
    
        End If
    Next
    Going to try it. I'll be back soon. Thank you

  27. #27

    Thread Starter
    Addicted Member
    Join Date
    Jun 2017
    Posts
    165

    Re: How can I call a this Function that returns a RadioButtom name in a GroupBox

    Quote Originally Posted by ebellounisoft View Post
    Going to try it. I'll be back soon. Thank you
    Got two blank dialoboxes.

  28. #28

    Thread Starter
    Addicted Member
    Join Date
    Jun 2017
    Posts
    165

    Re: How can I call a this Function that returns a RadioButtom name in a GroupBox

    Quote Originally Posted by Inferrd View Post
    This is your code from Post#10:
    Code:
    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
            Next
            MsgBox(WhatRadioIsSelected(groupboxSex))
        End If
    Next
    The highlighted line is in the wrong place. You have to find the GroupBox the same way as you found the other Controls in your child Panel.:

    Code:
    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.GroupBox) Then
                    MsgBox(WhatRadioIsSelected(DirectCast(subCtrl, GroupBox)))
                End If
    
            Next
    
        End If
    Next
    Got two blank dialoboxes.

  29. #29
    Frenzied Member
    Join Date
    Jul 2011
    Location
    UK
    Posts
    1,335

    Re: How can I call a this Function that returns a RadioButtom name in a GroupBox

    Quote Originally Posted by ebellounisoft View Post
    Got two blank dialoboxes.
    I've just been looking at your posts about this in another Forum. The code you post in that Forum shows that your RadioButtons are not actually in the GroupBox. If that's the case, then it's not surprising that the above code isn't working....and you're asking the wrong question in your thread title here!

    You really should show the code you are using to create the child Panel, and the code that creates the Controls that will be placed on it. Also, you should show how you are adding the Controls to the child Panel and the child Panel to the main Panel. That would save a lot of the guessing that is going on in many places.

  30. #30

    Thread Starter
    Addicted Member
    Join Date
    Jun 2017
    Posts
    165

    Re: How can I call a this Function that returns a RadioButtom name in a GroupBox

    Quote Originally Posted by Inferrd View Post
    I've just been looking at your posts about this in another Forum. The code you post in that Forum shows that your RadioButtons are not actually in the GroupBox. If that's the case, then it's not surprising that the above code isn't working....and you're asking the wrong question in your thread title here!

    You really should show the code you are using to create the child Panel, and the code that creates the Controls that will be placed on it. Also, you should show how you are adding the Controls to the child Panel and the child Panel to the main Panel. That would save a lot of the guessing that is going on in many places.
    Good. Then I try this:
    Code:
    Dim panelM As New Panel()
            panelM.Size = New Size(350, 150)
            panelM.Location = New Point(30, addTop - 60)
            panelM.BorderStyle = BorderStyle.Fixed3D
            panelM.AutoScroll = True
            panelM.Controls.Add(txtName)
            panelM.Controls.Add(lblName)
            panelM.Controls.Add(cbAge)
            groupboxSex.Controls.Add(rbttnM)
            groupboxSex.Controls.Add(rbttnF)
            panelM.Controls.Add(groupboxSex)
    Run it. Form Shows groupbox but not the RadioButtons and show two dialog boxes , Male and Male(twice).
    Last edited by ebellounisoft; Jun 28th, 2017 at 11:46 AM.

  31. #31
    Frenzied Member
    Join Date
    Jul 2011
    Location
    UK
    Posts
    1,335

    Re: How can I call a this Function that returns a RadioButtom name in a GroupBox

    Quote Originally Posted by ebellounisoft View Post
    Good. Then I try this:
    Code:
    Dim panelM As New Panel()
            panelM.Size = New Size(350, 150)
            panelM.Location = New Point(30, addTop - 60)
            panelM.BorderStyle = BorderStyle.Fixed3D
            panelM.AutoScroll = True
            panelM.Controls.Add(txtName)
            panelM.Controls.Add(lblName)
            panelM.Controls.Add(cbAge)
            groupboxSex.Controls.Add(rbttnM)
            groupboxSex.Controls.Add(rbttnF)
            panelM.Controls.Add(groupboxSex)
    Run it. Form Shows groupbox but not the RadioButtons and show two dialog boxes , Male and Male(twice).
    That's not enough code to be able to help. You haven't shown how you create any of txtName, lblName, cbAge, rbttnM, rbttnF or groupboxSex, nor how you give values to their various properties. And you haven't shown what you add panelM to.

  32. #32

    Thread Starter
    Addicted Member
    Join Date
    Jun 2017
    Posts
    165

    Re: How can I call a this Function that returns a RadioButtom name in a GroupBox

    Lunck break. Be back !!!. Thank you and best regards.

  33. #33

    Thread Starter
    Addicted Member
    Join Date
    Jun 2017
    Posts
    165

    Re: How can I call a this Function that returns a RadioButtom name in a GroupBox

    Quote Originally Posted by Inferrd View Post
    That's not enough code to be able to help. You haven't shown how you create any of txtName, lblName, cbAge, rbttnM, rbttnF or groupboxSex, nor how you give values to their various properties. And you haven't shown what you add panelM to.
    Creating them(txtName, lblName,cbAge. groupBoxsex, rbttnM, rbttnF) is the easy part. As you can see they are in the Form. The real problem is with the Function WhatRadioIsSelected (is some where in a post) that searchs for the text of the checked RadioButton..

  34. #34

    Thread Starter
    Addicted Member
    Join Date
    Jun 2017
    Posts
    165

    Re: How can I call a this Function that returns a RadioButtom name in a GroupBox

    Lunck break. Be back !!!. Thank you and best regards.

  35. #35
    Frenzied Member
    Join Date
    Jul 2011
    Location
    UK
    Posts
    1,335

    Re: How can I call a this Function that returns a RadioButtom name in a GroupBox

    Quote Originally Posted by ebellounisoft View Post
    Creating them(txtName, lblName,cbAge. groupBoxsex, rbttnM, rbttnF) is the easy part.
    Creating Controls dynamically is also very easy to get wrong. I know because I've done it wrong more times than I care to admit .


    Quote Originally Posted by ebellounisoft View Post
    As you can see they are in the Form.
    Yeah, from the code you were using in the past.... a dozen changes ago. You need to show your most current code.


    Quote Originally Posted by ebellounisoft View Post
    The real problem is with the Function WhatRadioIsSelected (is some where in a post) that searchs for the text of the checked RadioButton..
    No. It's right in front of you in the VS IDE, as part of the Form's code. It should take no more than a couple of seconds to copy and paste the relevant code here.


    Anyways.... I see you are getting help with code in the other Forum. There's no point people taking you in multiple directions and confusing you more, so I'll leave it at that for the time being.

  36. #36

    Thread Starter
    Addicted Member
    Join Date
    Jun 2017
    Posts
    165

    Re: How can I call a this Function that returns a RadioButtom name in a GroupBox

    Quote Originally Posted by Inferrd View Post
    Creating Controls dynamically is also very easy to get wrong. I know because I've done it wrong more times than I care to admit .



    Yeah, from the code you were using in the past.... a dozen changes ago. You need to show your most current code.




    No. It's right in front of you in the VS IDE, as part of the Form's code. It should take no more than a couple of seconds to copy and paste the relevant code here.


    Anyways.... I see you are getting help with code in the other Forum. There's no point people taking you in multiple directions and confusing you more, so I'll leave it at that for the time being.
    Your right. I'm going to take a break for two days and go over the whole project from the start and review all the posts.

  37. #37
    You don't want to know.
    Join Date
    Aug 2010
    Posts
    4,578

    Re: How can I call a this Function that returns a RadioButtom name in a GroupBox

    I don't like where this thread went one bit. I never have liked code that iterates over Controls collections, then trying to translate controls to values... it's all just... messy. We're software developers, we work in abstractions, and we can do better.

    I feel like someone showed off a UserControl eariler in the thread but I can't for the life of me find it now, so I risk duplicating their answer. I've attached a project with the UserControl.

    The idea is it's a GroupBox with two buttons. There are public properties that represent the text of the buttons so you can make them any two choices you want. There is a Value property that represents the text of the currently selected RadioButton. Maintaining that isn't very complex.

    When either text property changes, it has to update the text of the radio buttons. If that radio button was checked, the private _value field is updated. When a button becomes checked, it updates _value with its own text.

    Writing this user control is more painless than trying to write recursive loops and control-to-string conversions. You can generalize it further to have any number of options and any number of radio buttons without much fuss.

    I've attached a VS 2017 project with an example. I think those run as-is in VS 2015, if not the files ought to be able to be copied into an existing project.
    Attached Files Attached Files
    This answer is wrong. You should be using TableAdapter and Dictionaries instead.

  38. #38

    Thread Starter
    Addicted Member
    Join Date
    Jun 2017
    Posts
    165

    Re: How can I call a this Function that returns a RadioButtom name in a GroupBox

    It is an example suggested in a book: Mastering Visual Basic 2010,Author: Evangelos Petroutsos. I contact Wiley, the publishing company, chat with them and they gave me the mail of the author but never answer my request of the code project DynamicDataEntry project, Chapter 6, page 240 so I am only trying to develop it by my self . As you will see the form has a Main Panel and it adds and removes at run time child panels with controls in it. If you google, a lot of people ask for this code with no luck. I think it is an interesting challenge to develop it and can be use as Data entry for other type of topics. I think it is a good template for other projects.

  39. #39

    Thread Starter
    Addicted Member
    Join Date
    Jun 2017
    Posts
    165

    Re: How can I call a this Function that returns a RadioButtom name in a GroupBox

    I am able to add and remove child panels at run time, get the text from the textbox, the age from a combobox but can not get the text (gender, Male or Female) of the radiobutton checked at run time. This is what I have till now.

  40. #40
    Fanatic Member Arve K.'s Avatar
    Join Date
    Sep 2008
    Location
    Kyrksæterøra, Norway
    Posts
    518

    Re: How can I call a this Function that returns a RadioButtom name in a GroupBox

    I think Inferrd got a point when he says that the Radiobuttons might actually not be where you think they are.

    JMC have posted a nice snippet in this thread that iterates through every sub control. Based on that example, what happen if you change your function to this?

    vb.net Code:
    1. Private Function WhatRadioIsSelected(ByVal MainPanel As Panel) As String 'Note that it is iterating through the main panel controls and its child controls
    2.     Dim result As String = Nothing
    3.  
    4.     Dim ctl As Control = MainPanel.GetNextControl(MainPanel, True) 'Get the first control in the tab order.
    5.  
    6.     Do Until ctl Is Nothing
    7.         If TypeOf ctl Is GroupBox Then
    8.             For Each ControlInGroupBox As Control In DirectCast(ctl, GroupBox).Controls.OfType(Of RadioButton)
    9.                 If DirectCast(ControlInGroupBox, RadioButton).Checked = True Then result = ControlInGroupBox.Text
    10.             Next
    11.         End If
    12.  
    13.         ctl = MainPanel.GetNextControl(ctl, True) 'Get the next control in the tab order.
    14.     Loop
    15.  
    16.     Return result
    17. End Function

    Instead of using the groupbox as a parameter, use the main panel instead.
    vb.net Code:
    1. Messagebox.Show(WhatRadioIsSelected(pnlMainPanel))

    Also I agree with Sytten, using a Usercontrol will result in a much lighter coding experience, and in my opinion adding and removing several controls and their event handlers in run time should only be used in special cases. I include a project related to your issue (which is slightly overkill for demonstrating purposes) where everything is designed in run time. The project only got a few controls, but image if there where tens or hundreds of them You can compare for yourself why the Usercontrol example is a good example
    Attached Files Attached Files
    Arve K.

    Please mark your thread as resolved and add reputation to those who helped you solve your problem
    Disclaimer: I am not a professional programmer

Page 1 of 2 12 LastLast

Tags for this Thread

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