 |
BOOK: Visual Basic 2005 Programmer's Reference  | This is the forum to discuss the Wrox book Visual Basic 2005 Programmer's Reference by Rod Stephens; ISBN: 9780764571985 |
|
Welcome to the p2p.wrox.com Forums.
You are currently viewing the BOOK: Visual Basic 2005 Programmer's Reference section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|

April 6th, 2010, 03:55 PM
|
Authorized User
|
|
Join Date: Feb 2008
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Adding controls in code
I am trying to add some radio buttons or checkboxes or listboxes in code.
It will not be known until runtime which control, or how many instances of the control are needed.
My code:
<code>
Dim Optionr1 As RadioButton = New RadioButton()
Dim Optionr2 As RadioButton = New RadioButton()
Optionr1.Text = "text from xml file"
Panel1.Controls.Add(Optionr1)
Optionr2.Text = "other text from xml file"
Panel1.Controls.Add(Optionr2)
'This shows one radio button not both (the first one only).
'
Optionr1.Text = "text from xml file"
Panel1.Controls.Add(Optionr1)
Optionr2.Text = "other text from xml file"
Panel2.Controls.Add(Optionr2)
'This shows both radio buttons but they are not connected so one or all can be checked.
'The radio buttons need to be in the same panel to work as a group. </code>
How can I see all the radio buttons and have then work as a group please?
__________________
Pat
Last edited by Patc4; April 6th, 2010 at 03:58 PM..
|

April 6th, 2010, 04:41 PM
|
Friend of Wrox
|
|
Join Date: Dec 2008
Posts: 238
Thanks: 2
Thanked 20 Times in 19 Posts
|
|
Do something like this:
Code:
Dim r1 = New RadioButton()
r1.ID = "r1"
r1.GroupName = "g1"
Panel1.Controls.Add(r1)
Dim r2 = New RadioButton()
r2.ID = "r2"
r2.GroupName = "g1"
Panel1.Controls.Add(r2)
Dim r3 = New RadioButton()
r3.ID = "r3"
r3.GroupName = "g1"
Panel1.Controls.Add(r3)
|

April 6th, 2010, 09:03 PM
|
 |
Wrox Author
|
|
Join Date: Jan 2006
Posts: 647
Thanks: 2
Thanked 96 Times in 95 Posts
|
|
I think they're on top of each other
I think the first piece of code might be creating both controls but they're appearing on top of each other. Try adding this code after you create the second control:
Optionr2.Top = Optionr1.Bottom + 5
|

April 7th, 2010, 09:07 AM
|
Authorized User
|
|
Join Date: Feb 2008
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Controls
Thanks to both people, I'll try both methods and feedback ASAP.
__________________
Pat
|

April 7th, 2010, 09:47 AM
|
Authorized User
|
|
Join Date: Feb 2008
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Solved
I have tried the .top and .bottom method for the radio buttons and all my controls appear, and since they are in one panel they are automatically grouped. All works well now with my prototype. Thanks Rod.
I have tried assigning to group, but the .ID gave a run time error "Public member 'ID' on type 'RadioButton' not found." I shall work on this later as this has potential to force groups I may more research. Thanks Peter.
__________________
Pat
|

April 7th, 2010, 10:23 AM
|
Friend of Wrox
|
|
Join Date: Dec 2008
Posts: 238
Thanks: 2
Thanked 20 Times in 19 Posts
|
|
Do you hand code your page, or use Visual Studio? My sample code was tested. If you still have issue with .ID, post your entire page, and we will see.
|

April 7th, 2010, 10:45 PM
|
 |
Wrox Author
|
|
Join Date: Jan 2006
Posts: 647
Thanks: 2
Thanked 96 Times in 95 Posts
|
|
No ID property
I'm pretty sure the System.Windows.Forms.RadioButton class doesn't have an ID property. I think there's one in ASP.NET but that's the button's name.
In WPF, the System.Controls.RadioButton class has a GroupName property and you can use it to make RadioButtons be in the same group.
In System.Windows.Forms, two RadioButtons are in the same group if they have the same parent. If you want to do something non-obvious, you can use an invisible parent such as a Panel.
|

April 8th, 2010, 12:02 AM
|
Friend of Wrox
|
|
Join Date: Dec 2008
Posts: 238
Thanks: 2
Thanked 20 Times in 19 Posts
|
|
Ah, just noticed that this is a forum for a book (that is not focused on ASP.Net). My example was done as ASP.Net page.
That explains all.
|
|
 |