Wrox Programmer Forums
|
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
 
Old April 6th, 2010, 03:55 PM
Authorized User
 
Join Date: Feb 2008
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
Unhappy 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..
 
Old April 6th, 2010, 04:41 PM
Friend of Wrox
 
Join Date: Dec 2008
Posts: 238
Thanks: 2
Thanked 20 Times in 19 Posts
Default

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)
 
Old April 6th, 2010, 09:03 PM
Rod Stephens's Avatar
Wrox Author
 
Join Date: Jan 2006
Posts: 647
Thanks: 2
Thanked 96 Times in 95 Posts
Thumbs up 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
__________________
Rod

Rod Stephens, Microsoft MVP

Essential Algorithms: A Practical Approach to Computer Algorithms

(Please post reviews at Amazon or wherever you shop!)
 
Old April 7th, 2010, 09:07 AM
Authorized User
 
Join Date: Feb 2008
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
Thumbs up Controls

Thanks to both people, I'll try both methods and feedback ASAP.
__________________
Pat
 
Old April 7th, 2010, 09:47 AM
Authorized User
 
Join Date: Feb 2008
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
Wink 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
 
Old April 7th, 2010, 10:23 AM
Friend of Wrox
 
Join Date: Dec 2008
Posts: 238
Thanks: 2
Thanked 20 Times in 19 Posts
Default

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.
 
Old April 7th, 2010, 10:45 PM
Rod Stephens's Avatar
Wrox Author
 
Join Date: Jan 2006
Posts: 647
Thanks: 2
Thanked 96 Times in 95 Posts
Default 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.
__________________
Rod

Rod Stephens, Microsoft MVP

Essential Algorithms: A Practical Approach to Computer Algorithms

(Please post reviews at Amazon or wherever you shop!)
 
Old April 8th, 2010, 12:02 AM
Friend of Wrox
 
Join Date: Dec 2008
Posts: 238
Thanks: 2
Thanked 20 Times in 19 Posts
Default

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.





Similar Threads
Thread Thread Starter Forum Replies Last Post
Adding controls to a form Scootterp Access VBA 2 August 4th, 2009 02:32 PM
Adding Controls Dynamically andyj00 C# 0 March 19th, 2007 08:54 AM
Adding controls dynamicly Paula222 Access VBA 21 February 9th, 2006 05:29 AM
Adding Constituent Controls AGS BOOK: Professional Web Parts and Custom Controls ASP.NET ISBN: 0-7645-7860-X 1 December 12th, 2005 06:32 AM
Adding controls to C# clojinted General .NET 1 February 12th, 2005 03:17 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.