-
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
-
Re: How can I call a this Function that returns a RadioButtom name in a GroupBox
-
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()
-
Re: How can I call a this Function that returns a RadioButtom name in a GroupBox
vb Code:
MsgBox(WhatRadioIsSelected(YourGroupBox))
-
Re: How can I call a this Function that returns a RadioButtom name in a GroupBox
Works !!!. Your are the best. Thank you !!!.
-
Re: How can I call a this Function that returns a RadioButtom name in a GroupBox
Works !!!. Your are the best. Thank you !!!
-
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.
-
Re: How can I call a this Function that returns a RadioButtom name in a GroupBox
np :p
Personally, I would consider replacing MsgBox with the Messagebox class, since that is the dot Net way to show messages.
-
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 :
-
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
-
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
-
1 Attachment(s)
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 :
Attachment 148965
-
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:
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
For Each control As Control In Me.groupboxSex.Controls.OfType(Of RadioButton)
If DirectCast(control, RadioButton).Checked Then MessageBox.Show(control.Name)
Next
End Sub
-
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:wave:
-
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?
-
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)
-
Re: How can I call a this Function that returns a RadioButtom name in a GroupBox
-
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 :wave:
-
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:
Private Function WhatRadioIsSelected(ByVal grp As GroupBox) As String
Dim result As String = Nothing
For Each control As Control In grp.Controls.OfType(Of RadioButton)
If DirectCast(control, RadioButton).Checked Then result = control.Text
Next
Return result
End Function
Screenshot
-
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.:wave:
-
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.
-
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
-
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.
-
Re: How can I call a this Function that returns a RadioButtom name in a GroupBox
-
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
-
Re: How can I call a this Function that returns a RadioButtom name in a GroupBox
Quote:
Originally Posted by
Inferrd
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
-
Re: How can I call a this Function that returns a RadioButtom name in a GroupBox
Quote:
Originally Posted by
ebellounisoft
Going to try it. I'll be back soon. Thank you
Got two blank dialoboxes.
-
Re: How can I call a this Function that returns a RadioButtom name in a GroupBox
Quote:
Originally Posted by
Inferrd
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.
-
Re: How can I call a this Function that returns a RadioButtom name in a GroupBox
Quote:
Originally Posted by
ebellounisoft
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. :)
-
Re: How can I call a this Function that returns a RadioButtom name in a GroupBox
Quote:
Originally Posted by
Inferrd
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).
-
Re: How can I call a this Function that returns a RadioButtom name in a GroupBox
Quote:
Originally Posted by
ebellounisoft
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.
-
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.:wave:
-
Re: How can I call a this Function that returns a RadioButtom name in a GroupBox
Quote:
Originally Posted by
Inferrd
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..
-
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.:wave:
-
Re: How can I call a this Function that returns a RadioButtom name in a GroupBox
Quote:
Originally Posted by
ebellounisoft
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
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
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.
-
Re: How can I call a this Function that returns a RadioButtom name in a GroupBox
Quote:
Originally Posted by
Inferrd
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.:wave:
-
1 Attachment(s)
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.
-
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.
-
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.
-
1 Attachment(s)
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:
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 :p You can compare for yourself why the Usercontrol example is a good example :)