By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,990 Members | 2,138 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,990 IT Pros & Developers. It's quick & easy.

Access Dynamically created controls on a Form?

P: n/a
Radio Buttons are added to my form During the Load event,
the quantity determined by User input. They are placed in
pairs on groupboxes with code like this: (edited a bit for
brevity)

Public Function LayoutForm(ByVal intNumber As Integer)
Dim intCount As Integer
Panel1.Controls.Clear()
For intCount = 1 To intGames

'Visitors Button
Dim RadioButton1 As System.Windows.Forms.RadioButton
RadioButton1 = New System.Windows.Forms.RadioButton
RadioButton1.Text = GetSchedule(intWeek, "Visitor",
intCount)
RadioButton1.Name = "RadioButtonV" & intCount '+ 1
RadioButton1.Visible = True
Me.Controls.Add(RadioButton1)

'Home Button
Dim RadioButton2 As System.Windows.Forms.RadioButton
RadioButton2 = New System.Windows.Forms.RadioButton
RadioButton2.Text = GetSchedule(intWeek, "Home",
intCount)
RadioButton2.Name = "RadioButtonH" & intCount '+ 1
RadioButton2.Visible = True
'Me.Controls.Add(RadioButton2)

'Group
Dim grp As New System.windows.forms.GroupBox
grp.Top = 40 * count + 10
grp.Left = 25
grp.Height = 40
grp.Width = 500
'grp.SendToFront()
grp.Controls.Add(RadioButton1)
grp.Controls.Add(RadioButton2)
Panel1.Controls.Add(grp)
Next

End Function
This works quite well and lays things out as it should.
However, i have a button ("btnSave") whose click event
collects the Text property of each selected button and
compiles them into an XML file. Well, that is the intent.
I can't seem to get my program to recognize the
RadioButtons as belonging to Me.Controls collection. My
code works when in another portion of the program where i
am collecting data from dynamically created TextBoxes, but
those are not in groups. This is the method i am trying to
use to read the data:
Private Sub btnSave_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles btnSave.Click
'Save picks for current player
Dim ctl As System.Windows.Forms.Control
Dim radiobtn As System.Windows.Forms.RadioButton
Dim MyPicksDocument As New XmlDocument
Dim xmlPicksData As String
Dim intGameCount As Integer = 1
For Each ctl In Me.Controls
Console.WriteLine(ctl.Text) ' for debugging
If TypeOf ctl Is RadioButton Then
radiobtn = CType(ctl, RadioButton)
'xmlPicksData String building info here
intGameCount +=1
Else
intGameCount += 1
End If
Next

MyPicksDocument.LoadXml(xmlPicksData)
'commands to save file
End Sub

When i run this, and click "Save", the only controls whose
text gets written to the Console window are the ones
placed on the form in the designer. It never hits the
groupboxes or radiobuttons. Is there a way to make them
recognized?

Any help is appreciated.

Nov 20 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
"D Hass" <ju*******@hotmail.com> schrieb

When i run this, and click "Save", the only controls whose
text gets written to the Console window are the ones
placed on the form in the designer. It never hits the
groupboxes or radiobuttons. Is there a way to make them
recognized?


The Form.Controls collection contains only the controls *directly* placed on
the Form, not the nested controls. Put your loop into a procedure and call
it in btnsave_click. Within the for-next loop, to make the sub recursive,
write something like:

if typeof ctl is groupbox then
<nameofthenewsub>(ctl.controls, ...)
elseif typeof ctl is radiobutton then
'current code
end if

Note: The type of the first argument of the new submust be
Control.ControlCollection, not Form.ControlCollection.
--
Armin

http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html

Nov 20 '05 #2

P: n/a
Thank-you, Armin
Nov 20 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.