Access Dynamically created controls on a Form?

D

D Hass

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.
 
A

Armin Zingler

D Hass said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top