BlakeMcKenna
asked on
Accessing dynamically created controls within a Windows Form?
Ok,
This question is kinda in conjunction with what I have been working on lately and along with several other questions I have posted.
I need to find how or what is the most efficient way to access dynamically created UserControls from within a Form. The following Sub-Routine exists in my Windows application.
Using the above code, two UserControls are created: uC1 and uC2 with assigned names of ucChannel? and ucEquipment1? respectively. Each instance of these controls are placed on its own TabPage, which is created in the Designer. So, 12 UC's = 12 TabPages.
How can I access ucChannel? and ucEquipment? from within other Functions/SubRoutines within the same Form?
Code examples would greatly be appreciated.
Thanks!
This question is kinda in conjunction with what I have been working on lately and along with several other questions I have posted.
I need to find how or what is the most efficient way to access dynamically created UserControls from within a Form. The following Sub-Routine exists in my Windows application.
Private Sub BuildChannelControlTabs()
Try
EH.ErrorMessage = String.Empty
If tabTests.TabPages.Contains(tabChannels) Then
For x As Integer = 0 To stcChannels.Length - 1
Dim tab As New TabPage
Dim uC1 As New userChannelSettings
uC1.Name = "ucChannel" & x + 1
uC1.Tag = stcChannels(x).channel_ID
tab.Controls.Add(uC1)
uC1.ForeColor = tab.ForeColor
AddHandler uC1.Load, AddressOf ucChannel_Load
tab.Name = "tChannel" & x + 1
tab.Tag = stcChannels(x).channel_ID
tab.Text = "Channel: " & stcChannels(x).channel
If Not tabChannelList.TabPages.Contains(tab) Then
tabChannelList.TabPages.Add(tab)
End If
Next
Else
EH.ErrorMessage = "Invalid Control Creation!" & "~E"
GoTo ProcessMessage
End If
If tabTests.TabPages.Contains(tabEquipmentByChannel) Then
For x As Integer = 0 To stcChannels.Length - 1
Dim tab As New TabPage
Dim uC2 As New userChannelEquipment()
uC2.Name = "ucEquipment" & x + 1
uC2.Tag = stcChannels(x).channel_ID
uC2.Location = New Point(5, 5)
tab.Controls.Add(uC2)
uC2.ForeColor = tab.ForeColor
AddHandler uC2.Load, AddressOf ucEquipment_Load
tab.Name = "tEquip" & x + 1
tab.Tag = stcChannels(x).channel_ID
tab.Text = "Channel: " & stcChannels(x).channel
If Not tabChannelEquipment.TabPages.Contains(tab) Then
tabChannelEquipment.TabPages.Add(tab)
End If
Next
Else
EH.ErrorMessage = "Invalid Control Creation!" & "~E"
End If
ProcessMessage:
Catch ex As Exception
EH.ErrorMessage = "frmCalibration_3/BuildChannelControlTabs() - " & ex.Message & "...Contact Engineering!" & "~E"
End Try
End Sub
Using the above code, two UserControls are created: uC1 and uC2 with assigned names of ucChannel? and ucEquipment1? respectively. Each instance of these controls are placed on its own TabPage, which is created in the Designer. So, 12 UC's = 12 TabPages.
How can I access ucChannel? and ucEquipment? from within other Functions/SubRoutines within the same Form?
Code examples would greatly be appreciated.
Thanks!
ASKER
Mike,
If, at design time, I add a single UserControl to a tabpage, but then when I run the application, I remove the same tabpages. At some point, the user has the option to add the same tabpage back onto the tab control. When this happens the UserControl is not present. Shouldn't the UC stay on the tabpage even though it is removed at runtime?
If, at design time, I add a single UserControl to a tabpage, but then when I run the application, I remove the same tabpages. At some point, the user has the option to add the same tabpage back onto the tab control. When this happens the UserControl is not present. Shouldn't the UC stay on the tabpage even though it is removed at runtime?
ASKER
Mike,
I tried what you suggested and I tested it but looping through the control on the UC. Each UC has 4 groupbox controls in which resides another UC. Do I need to access this UC by first prefacing it with the groupbox? For example:
groupBox1.userEnvironment. txtMeasure d
Is there a better way?
I tried what you suggested and I tested it but looping through the control on the UC. Each UC has 4 groupbox controls in which resides another UC. Do I need to access this UC by first prefacing it with the groupbox? For example:
groupBox1.userEnvironment.
Is there a better way?
If that's the way they are setup, then that should work. Without having the app in front of me, and a better understanding of all the code, I can't really know if there is a "better" way.
ASKER
Mike,
I have attached a Screenshot of the UI in designer mode. Hopefully that will give you a more clearer picture of what's going on. Below is the code, that add's the tabPages back onto the Tab Control. There are two For Loops, one is for the "Channels" TabPage and the other is for the "Equipment By Channel" TabPage.
I have attached a Screenshot of the UI in designer mode. Hopefully that will give you a more clearer picture of what's going on. Below is the code, that add's the tabPages back onto the Tab Control. There are two For Loops, one is for the "Channels" TabPage and the other is for the "Equipment By Channel" TabPage.
Private Sub BuildChannelControlTabs()
Try
EH.ErrorMessage = String.Empty
If tabTests.TabPages.Contains(tabChannels) Then
For x As Integer = 0 To stcChannels.Length - 1
Dim tab As New TabPage
tab.Name = "tChannel" & x + 1
tab.Tag = stcChannels(x).channel_ID
tab.Text = "Channel: " & stcChannels(x).channel
If Not tabChannelList.TabPages.Contains(tab) Then
tabChannelList.TabPages.Add(tab)
End If
Next
Else
EH.ErrorMessage = "Invalid Control Creation!" & "~E"
GoTo ProcessMessage
End If
If tabTests.TabPages.Contains(tabEquipmentByChannel) Then
For x As Integer = 0 To stcChannels.Length - 1
Dim tab As New TabPage
tab.Name = "tE" & x + 1
tab.Tag = stcChannels(x).channel_ID
tab.Text = "Channel: " & stcChannels(x).channel
If Not tabChannelEquipment.TabPages.Contains(tab) Then
tabChannelEquipment.TabPages.Add(tab)
End If
Next
Else
EH.ErrorMessage = "Invalid Control Creation!" & "~E"
End If
ProcessMessage:
Catch ex As Exception
EH.ErrorMessage = "frmCalibration_3/BuildChannelControlTabs() - " & ex.Message & "...Contact Engineering!" & "~E"
End Try
End Sub
Screenshot.jpg
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks Ark...I did not know that TabPages could be hidden...
Actually, they cannot be hidden (see quotes around "hide") :)
You can create a list of tab pages (incapulated in a class with some extra properties) and then just insert/remove those pages into tab control instead of show/hide
You can create a list of tab pages (incapulated in a class with some extra properties) and then just insert/remove those pages into tab control instead of show/hide
Open in new window
Then you can access them with "uC1s(index)" and "uC2s(index)" from anywhere in the form.
You could also use a Dictionary instead, using the control name as the key.