Page 2 of 2 FirstFirst 12
Results 41 to 47 of 47

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

  1. #41

    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 _powerade_ View Post
    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
    Going to try both. Be back. Thank you.

  2. #42

    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 _powerade_ View Post
    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
    Very good code example. Did you read my posts # 38, 39. Yours has a only a Main Panel. Correct me if I am wrong please. Mine has two a main and a child panels.

  3. #43

    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 Sitten Spynne View Post
    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.
    Very good code example. Yours has a only a Main Panel only. Correct me if I am wrong please. Mine has two a main and a child panels. Had problems open it. with Visual studio 2015.

  4. #44

    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 Sitten Spynne View Post
    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.
    Good code example. Can see it has a Main Panel, my project has two a parent and a child as you can see in my posts. Had problem opening yours with Visual Studio 2015. It has the groupbox and radiobuttons at design time. Sorrry , I post this twice ups !!!
    Last edited by ebellounisoft; Jun 28th, 2017 at 07:04 PM.

  5. #45
    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

    Quote Originally Posted by ebellounisoft View Post
    Had problem opening yours with Visual Studio 2015
    You should do a Build before opening the form
    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

  6. #46
    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

    Quote Originally Posted by ebellounisoft View Post
    Very good code example. Did you read my posts # 38, 39. Yours has a only a Main Panel. Correct me if I am wrong please. Mine has two a main and a child panels.
    The number of panels is not an issue, even if you had 1000 panels inside your main panel, it would loop through them all until it found a groupbox with radiobuttons.
    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

  7. #47

    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 _powerade_ View Post
    The number of panels is not an issue, even if you had 1000 panels inside your main panel, it would loop through them all until it found a groupbox with radiobuttons.
    Got it. Going to work with it and tomorrow I get back to you ok ?. Thank you.

Page 2 of 2 FirstFirst 12

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