2013-03-03 45 views
1

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

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

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,使用您的建议作为出发点。

我添加了几个varribles: 昏暗RB作为控制 昏暗bEvent作为字符串 昏暗booFillY布尔 昏暗booFillN布尔

我还增加了的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)O可见通过在创建的任何标签页上选择是或否单选按钮来看不到。 感谢您的帮助。我怀疑我会自己得到那里。

回答

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和最后一个是TabControl的持有Tab。

无论如何,这是非常严重的事情,因为它假设您的标签总是以这种方式创建的。例如,如果您将在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 
1

不太是你所寻找的,但超heplful,将可能把你带到任何你需要去。

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

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

你也可以装配所有选项卡的列表,例如,通过改变如果语句Is TypeOf con Is TabPage

现在你可以遍历这个集合或者用LINQ来查询它。通过调用第一种或单一方法来查找单个控件。转换为您想要的类型,并对表单中任何位置的任何控件执行任何操作。

相关问题