1
votes

I am working with a tabcontrol on which I create page one with the designer. I am creating new tab pages with controls on the pages programmatically. On each page is a several panels, each with two radiobuttons (one yes,another no). There is a panel nested inside the first panel with its visible property set to false. If the user selects yes, I want the nested panel's visible property set to true which will reveal several more radiobuttons from which they must make more choices.

My problem is in changing the nested panel's property on any page other than page one.. I can detect the radiobuttons, but I can't seem to find a way to make the nested panel visible.

Public Class ControlProgram

Dim pageindx as integer

 Private Sub btnAddPrgm1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAddPrgm1.Click

    Dim newTab As New TabPage()
    pageindx = (TabControl1.TabPages.Count + 1)
    newTab.Text = "Step  " & pageindx
    'define fill panel controls
    Dim newpnlFill As New Panel
    Dim newlblFill As New Label
    Dim newFillY As New RadioButton
    AddHandler newFillY.CheckedChanged, AddressOf CheckforCheckedChanged
    Dim newFillN As New RadioButton
    AddHandler newFillN.CheckedChanged, AddressOf CheckforCheckedChanged

    'add fill panel controls
    With newTab.Controls
        .Add(newpnlFill)
        With newpnlFill
            .Location = New System.Drawing.Point(6, 6)
            .Size = New System.Drawing.Size(171, 137)
            .BorderStyle = BorderStyle.FixedSingle
            .Controls.Add(newlblFill)
            With newlblFill
                .Name = "Fill"
                .Text = "Fill ?"
                .Font = New Font(newlblFill.Font, FontStyle.Bold)

                .Location = New Drawing.Point(5, 3)
            End With
            .Controls.Add(newFillY)
            With newFillY
                .Name = "FillY"
                .Text = "Yes"
                .Location = New Drawing.Point(23, 28)
                .Size = New System.Drawing.Size(43, 17)
            End With
            .Controls.Add(newFillN)
            With newFillN
                .Name = "FillN"
                .Text = "No"
                .Location = New Drawing.Point(88, 28)
                .Size = New System.Drawing.Size(39, 17)
            End With
            .Controls.Add(newpnlFill2)
            With newpnlFill2
                .Location = New System.Drawing.Point(2, 60)
                .Size = New System.Drawing.Size(164, 68)
                .BorderStyle = BorderStyle.FixedSingle
                .Visible = False
            End With
      End With
   End With
Private Sub CheckforCheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs)
    If TypeOf sender Is RadioButton Then

        bEvent = CType(sender, RadioButton).Name
     End If
End Sub

End Class

I have since figured out a solution to my delima, using your suggestions as a starting point.

I added a few varribles: Dim rb as Control Dim bEvent as String Dim booFillY as Boolean Dim booFillN as Boolean

I also added the TabControl TabControl1.TabPages.Add(newTab)

I also made these changes :

    Private Sub CheckforCheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs)
    If TypeOf sender Is RadioButton Then

        rb = sender
        bEvent = CType(sender, RadioButton).Name
        If bEvent = "FillY" Then
            Dim newpnlFill2 As Panel = rb.Parent.Controls(3)
            newpnlFill2.Visible = True
        End If
        If bEvent = "FillN" Then


            Dim newpnlFill2 As Panel = rb.Parent.Controls(3)
            newpnlFill2.Visible = False

        End If
    End If
End Sub

Now I can make the nested panel(newpnlFill2) visible or not visible by cicking the Yes or No radiobuttons on any of the tab pages created. thanks for your help. I doubt I would have ever gotten there on my own.

2

2 Answers

1
votes

Might not be quite what you were looking for, but should be helpful get you where you need to go.

When I create an application, I like to build a list of all the controls for a given page in the load event so I can access them at any point. This is helpful because WinForms can be very picky about showing you child controls within a tabpage or groupbox, etc.

'Declare this variable within the class for your form (whatever)
Public arrControlsRecursive As New List(Of Control)

'method to recursively check all controls and build to array
Private Sub BuildControlsArrayRecursive(ByVal InControl As Control)
    For Each con As Control In InControl.Controls
        If con.Controls.Count > 0 Then
            BuildControlsArrayRecursive(con)
        End If
        If TypeOf con Is Control Then
            arrControlsRecursive.Add(con)
        End If
    Next
End Sub

'Call from MyBase.Load Event
BuildControlsArrayRecursive(Form1)

You can also just assemble a list of all tabs, for example, by changing the If statement to Is TypeOf con Is TabPage

Now you can loop through this collection or query it with LINQ. Find a single control by calling the first or single method. Cast to the type you want and do anything to any control anywhere within your form.

0
votes

I don't really understand what you want to access, you first talk about

changing the nested panel's property on any page other than page one

So I assume you want to access to the other tabs, then, you talk about:

I can't seem to find a way to make the panel visible

Anyway, here's the two solutions:

Access other panels:

Private Sub CheckforCheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs)
    If TypeOf sender Is RadioButton Then
        bEvent = CType(sender, RadioButton).Name '**Where is "bEvent" declared??**

        Dim newpnlFill2 as Panel = bEvent.Parent.Controls(3), Panel)
        newpnlFill2.Visible = bEvent.Checked
     End If
End Sub

You access to Parent that will be newpnlFill, then access to Controls(3) that it should be newpnlFill2.

Access other tabs:

Private Sub CheckforCheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs)
    If TypeOf sender Is RadioButton Then
        bEvent = CType(sender, RadioButton).Name '**Where is "bEvent" declared??**

        Dim TabControl as TabControl = bEvent.Parent.Parent.Parent, TabControl)
        'Then, you can access all of the other tabs with:
        'TabControl.TabPages(n)
     End If
End Sub

This assume that somewhere you add your newTab to a TabControl. I see that you never add newTab to any TabControl, so you'll never see it.. The first Parent will be newpnlFill, the second one will reference to newTab and the last one is the TabControl that hold the Tab.

Anyway, it's something really gross, cause it assumes that your Tab is always created in this manner. For example, if you will add another panel before newpnlFill, it will not be the 4th Control in the Panel anymore, so you need to change you access code.

My advice is to create your own UserControl that inherit from TabPage, in this way you can create private variables that will always reference to the Panels you want to change. Moreover, the btnAddPrgm1_Click event will be much more clear, moving the build of the page in your class constructor. Something like:

Private Sub btnAddPrgm1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAddPrgm1.Click
    Dim newTab As New MyTabPage()
    pageindx = (TabControl1.TabPages.Count + 1)
    newTab.Text = "Step  " & pageindx
    TabControl1.TabPages.Add(newTab)
End Sub