1

我正在使用一个选项卡控件,在该选项卡控件上与设计器一起创建第一页。我正在以编程方式在页面上创建带有控件的新标签页。每页都有几个面板,每个面板都有两个单选按钮(一个是,另一个不是)。在第一个面板内嵌套了一个面板,其可见属性设置为 false。如果用户选择是,我希望嵌套面板的可见属性设置为 true,这将显示更多的单选按钮,他们必须从中做出更多选择。

我的问题是在除第一页之外的任何页面上更改嵌套面板的属性。我可以检测到单选按钮,但我似乎无法找到使嵌套面板可见的方法。

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

结束类

此后,我以您的建议为起点,找到了解决我的 delima 的方法。

我添加了一些变量: Dim rb as Control Dim bEvent as String Dim booFillY as Boolean Dim booFillN as Boolean

我还添加了 TabControl TabControl1.TabPages.Add(newTab)

我也做了这些改变:

    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

现在,我可以通过在创建的任何标签页上单击“是”或“否”单选按钮来使嵌套面板(newpnlFill2)可见或不可见。谢谢你的帮助。我怀疑我自己会不会到达那里。

4

2 回答 2

1

可能不是您要找的东西,但应该有助于您到达需要去的地方。

当我创建应用程序时,我喜欢在加载事件中为给定页面构建所有控件的列表,以便我可以随时访问它们。这很有帮助,因为 WinForms 对在标签页或组框等中显示子控件非常挑剔。

'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)

您也可以只组装所有选项卡的列表,例如,通过将 If 语句更改为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.

于 2013-03-07T02:15:09.693 回答
0

不太明白你要访问什么,你先说说

在第一页以外的任何页面上更改嵌套面板的属性

所以我假设你想访问其他选项卡,然后,你谈论:

我似乎找不到使面板可见的方法

无论如何,这是两个解决方案:

访问其他面板:

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

您可以访问Parent它是 newpnlFill,然后访问Controls(3)它应该是newpnlFill2.

访问其他选项卡:

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

这假设您将您添加newTab到 TabControl 的某个地方。我看到你从来没有添加newTab到任何 TabControl,所以你永远不会看到它。第一个ParentnewpnlFill,第二个是引用,newTab最后一个是保存 Tab 的 TabControl。

无论如何,这真的很恶心,因为它假定您的标签总是以这种方式创建的。例如,如果您之前 newpnlFill要添加另一个面板,则它不再是面板中的第 4 个控件,因此您需要更改访问代码。

我的建议是创建您自己的继承自 TabPage 的 UserControl,这样您就可以创建始终引用您要更改的面板的私有变量。此外,btnAddPrgm1_Click 事件将更加清晰,在您的类构造函数中移动页面的构建。就像是:

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
于 2013-03-05T13:05:12.797 回答