-
Jun 28th, 2017, 06:11 PM
#41
Thread Starter
Addicted Member
Re: How can I call a this Function that returns a RadioButtom name in a GroupBox
Originally Posted by _powerade_
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:
Private Function WhatRadioIsSelected(ByVal MainPanel As Panel) As String 'Note that it is iterating through the main panel controls and its child controls Dim result As String = Nothing Dim ctl As Control = MainPanel.GetNextControl(MainPanel, True) 'Get the first control in the tab order. Do Until ctl Is Nothing If TypeOf ctl Is GroupBox Then For Each ControlInGroupBox As Control In DirectCast(ctl, GroupBox).Controls.OfType(Of RadioButton) If DirectCast(ControlInGroupBox, RadioButton).Checked = True Then result = ControlInGroupBox.Text Next End If ctl = MainPanel.GetNextControl(ctl, True) 'Get the next control in the tab order. Loop Return result End Function
Instead of using the groupbox as a parameter, use the main panel instead.
vb.net Code:
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.
-
Jun 28th, 2017, 06:42 PM
#42
Thread Starter
Addicted Member
Re: How can I call a this Function that returns a RadioButtom name in a GroupBox
Originally Posted by _powerade_
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:
Private Function WhatRadioIsSelected(ByVal MainPanel As Panel) As String 'Note that it is iterating through the main panel controls and its child controls Dim result As String = Nothing Dim ctl As Control = MainPanel.GetNextControl(MainPanel, True) 'Get the first control in the tab order. Do Until ctl Is Nothing If TypeOf ctl Is GroupBox Then For Each ControlInGroupBox As Control In DirectCast(ctl, GroupBox).Controls.OfType(Of RadioButton) If DirectCast(ControlInGroupBox, RadioButton).Checked = True Then result = ControlInGroupBox.Text Next End If ctl = MainPanel.GetNextControl(ctl, True) 'Get the next control in the tab order. Loop Return result End Function
Instead of using the groupbox as a parameter, use the main panel instead.
vb.net Code:
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.
-
Jun 28th, 2017, 06:45 PM
#43
Thread Starter
Addicted Member
Re: How can I call a this Function that returns a RadioButtom name in a GroupBox
Originally Posted by Sitten Spynne
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.
-
Jun 28th, 2017, 06:56 PM
#44
Thread Starter
Addicted Member
Re: How can I call a this Function that returns a RadioButtom name in a GroupBox
Originally Posted by Sitten Spynne
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.
-
Jun 28th, 2017, 06:59 PM
#45
Re: How can I call a this Function that returns a RadioButtom name in a GroupBox
Originally Posted by ebellounisoft
Had problem opening yours with Visual Studio 2015
You should do a Build before opening the form
-
Jun 28th, 2017, 07:02 PM
#46
Re: How can I call a this Function that returns a RadioButtom name in a GroupBox
Originally Posted by ebellounisoft
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.
-
Jun 28th, 2017, 07:15 PM
#47
Thread Starter
Addicted Member
Re: How can I call a this Function that returns a RadioButtom name in a GroupBox
Originally Posted by _powerade_
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.
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|