在运行时vb.net期间在父控件中添加子控件

时间:2014-12-08 07:34:27

标签: vb.net parent-child

我有一个ItemPanel控件。它共有11个按钮。当程序在State1中而其他程序在State2中时,其中五个是可见的。我的问题是,即使在面板中添加了所有控件并且开头的可见性为true,在任何给定时间都只能看到前5个控件。我正在使用DotNetBar的ItemPanel控件。这是我的代码:

Private Sub w_ShortcutPanel()   <<create panel and add buttons>>

    If IsNothing(ShortcutItemPanel) Then

        ShortcutItemPanel = New ItemPanel

        Dim bi As New ButtonItem()
        bi.Image = My.Resources.GeneralSettingNew
        bi.Name = "biGSForm"
        bi.Tooltip = "General Settings Form"
        'bi.Size = New Size(80, 80)
        bi.ItemAlignment = eItemAlignment.Near
        AddHandler bi.Click, AddressOf w_SomeFunction
        'ShortcutItemPanel.Items.Add(bi)
        ShortcutItemPanel.Items.Insert(0, bi)

        bi = New ButtonItem
        bi.ColorTable = eDotNetBarStyle.StyleManagerControlled
        bi.Image = My.Resources.V_Reinforcement
        bi.Name = "biReinfForm"
        bi.Tooltip = "Reinforcement Settings Form"
        'bi.Size = New Size(80, 80)
        bi.ItemAlignment = eItemAlignment.Near
        AddHandler bi.Click, AddressOf w_SomeFunction
        'ShortcutItemPanel.Items.Add(bi)
        ShortcutItemPanel.Items.Insert(1, bi)

        bi = New ButtonItem
        bi.ColorTable = eDotNetBarStyle.StyleManagerControlled
        bi.Image = My.Resources.sectionreport
        bi.Name = "biCreateSection"
        bi.Tooltip = "Create Sections"
        'bi.Size = New Size(80, 80)
        bi.ItemAlignment = eItemAlignment.Near
        AddHandler bi.Click, AddressOf w_SomeFunction
        'ShortcutItemPanel.Items.Add(bi)
        ShortcutItemPanel.Items.Insert(2, bi)

        bi = New ButtonItem
        bi.Style = eDotNetBarStyle.StyleManagerControlled
        bi.Image = My.Resources.createregionnewBig
        bi.Name = "biRegionDetailing"
        bi.Tooltip = "Create Region"
        'bi.Size = New Size(80, 80)
        bi.ItemAlignment = eItemAlignment.Near
        AddHandler bi.Click, AddressOf w_SomeFunction
        'ShortcutItemPanel.Items.Add(bi)
        ShortcutItemPanel.Items.Insert(3, bi)

        bi = New ButtonItem
        bi.Style = eDotNetBarStyle.StyleManagerControlled
        bi.Image = My.Resources.Autodesignnew
        bi.Name = "biAutoDesign"
        bi.Tooltip = "Auto-Design"
        'bi.Size = New Size(80, 80)
        bi.ItemAlignment = eItemAlignment.Near
        AddHandler bi.Click, AddressOf w_SomeFunction
        'ShortcutItemPanel.Items.Add(bi)
        ShortcutItemPanel.Items.Insert(4, bi)

        'Designed View Items
        bi = New ButtonItem
        bi.Style = eDotNetBarStyle.StyleManagerControlled
        bi.Image = My.Resources.layout_report
        bi.Name = "biLayout"
        bi.Tooltip = "Create GA Drawing"
        'bi.Size = New Size(80, 80)
        bi.ItemAlignment = eItemAlignment.Near
        AddHandler bi.Click, AddressOf w_SomeFunction
        'ShortcutItemPanel.Items.Add(bi)
        ShortcutItemPanel.Items.Insert(5, bi)

        bi = New ButtonItem
        bi.Style = eDotNetBarStyle.StyleManagerControlled
        bi.Image = My.Resources.reinfreport
        bi.Name = "biReinforcement"
        bi.Tooltip = "Create Reinforcement Drawing"
        'bi.Size = New Size(80, 80)
        bi.ItemAlignment = eItemAlignment.Near
        AddHandler bi.Click, AddressOf w_SomeFunction
        'ShortcutItemPanel.Items.Add(bi)
        ShortcutItemPanel.Items.Insert(6, bi)

        bi = New ButtonItem
        bi.Style = eDotNetBarStyle.StyleManagerControlled
        bi.Image = My.Resources.unlockDesign
        bi.Name = "biUnlockDesign"
        bi.Tooltip = "Unlock Design"
        'bi.Size = New Size(80, 80)
        bi.ItemAlignment = eItemAlignment.Near
        AddHandler bi.Click, AddressOf w_SomeFunction
        'ShortcutItemPanel.Items.Add(bi)
        ShortcutItemPanel.Items.Insert(7, bi)

        bi = New ButtonItem
        bi.Style = eDotNetBarStyle.StyleManagerControlled
        bi.Image = My.Resources.Failurediagnostic
        bi.Name = "biFailureDiag"
        bi.Tooltip = "Failure Diagnostic"
        ' bi.Size = New Size(80, 80)
        bi.ItemAlignment = eItemAlignment.Near
    AddHandler bi.Click, AddressOf w_SomeFunction
        'ShortcutItemPanel.Items.Add(bi)
        ShortcutItemPanel.Items.Insert(8, bi)

        bi = New ButtonItem
        bi.Style = eDotNetBarStyle.StyleManagerControlled
        bi.Image = My.Resources.calreport
        bi.Name = "biCalculationReport"
        bi.Tooltip = "Calculation Report"
        'bi.Size = New Size(80, 80)
        bi.ItemAlignment = eItemAlignment.Near
    AddHandler bi.Click, AddressOf w_SomeFunction
        'ShortcutItemPanel.Items.Add(bi)
        ShortcutItemPanel.Items.Insert(9, bi)

        bi = New ButtonItem
        bi.Style = eDotNetBarStyle.StyleManagerControlled
        bi.Image = My.Resources.detailreport
        bi.Name = "biDetailReport"
        bi.Tooltip = "Detailed Report"
        'bi.Size = New Size(80, 80)
        bi.ItemAlignment = eItemAlignment.Near
    AddHandler bi.Click, AddressOf w_SomeFunction
        'ShortcutItemPanel.Items.Add(bi)
        ShortcutItemPanel.Items.Insert(10, bi)

        Dim x, y As Double
        x = vpMainControl.Width - ShortcutItemPanel.Width - 50
        y = vpMainControl.Location.Y + 10
        Dim location As New System.Drawing.Point(x, y)
        ShortcutItemPanel.Location = location
        ShortcutItemPanel.Margin = New System.Windows.Forms.Padding(0, 0, 10, 0)
        ShortcutItemPanel.Anchor = AnchorStyles.Top + AnchorStyles.Right
        ShortcutItemPanel.BackColor = vpMainControl.BackColor
        ShortcutItemPanel.AutoSize = True
        ShortcutItemPanel.AutoScaleMode = Windows.Forms.AutoScaleMode.Inherit
        ShortcutItemPanel.ItemSpacing = 0
        ShortcutItemPanel.HorizontalItemAlignment = eHorizontalItemsAlignment.Center
        vpMainControl.Controls.Add(ShortcutItemPanel)
        ShortcutItemPanel.Visible = True

    End If

End Sub

&LT;&LT;这个函数在一个单独的类中,我将“vpMainControl”作为参数传递给它。所以我访问ShortcutItemPanel为vpMainControl.ShortcutItemPanel&gt;&gt;

Private Sub w_RefreshShortcutPanelItemsByViewMode()  <<set visibility when state changes>>

    For i As Integer = 0 To vpMainControl.ShortcutItemPanel.Items.Count - 1

        Select Case vpMainControl.ShortcutItemPanel.Items(i).Name
            Case "biGSForm", "biReinfForm", "biCreateSection", "biRegionDetailing", "biAutoDesign"
                If State1 Then vpMainControl.ShortcutItemPanel.Items(i).Visible = False
                If Staate2 Then vpMainControl.ShortcutItemPanel.Items(i).Visible = True
            Case "biFailureDiag", "biLayout", "biReinforcement", "biCalculationReport", "biDetailReport", "biUnlockDesign"
                If State1 Then vpMainControl.ShortcutItemPanel.Items(i).Visible = True
                If State2 Then vpMainControl.ShortcutItemPanel.Items(i).Visible = False
        End Select

    Next

End Sub

1 个答案:

答案 0 :(得分:0)

确定。我暂时解决了这个问题,但我仍然想知道为什么容器控件不会随其中的子控件调整大小。

Private Sub w_AdjustShortcutPanelUI(ByVal v_Count As Integer)
    vpMainControl.ShortcutItemPanel.Width = (From itm As Object In vpMainControl.ShortcutItemPanel.Items
                                              Select itm.Size.Width).Max * v_Count
    Dim x, y As Double
    x = vpMainControl.Width - vpMainControl.ShortcutItemPanel.Width - 50
    y = vpMainControl.Location.Y + 10
    Dim location As New System.Drawing.Point(x, y)
    vpMainControl.ShortcutItemPanel.Location = location
End Sub

我从之前发布的刷新函数调用此函数:

Private Sub w_RefreshShortcutPanelItemsByViewMode()

    Dim iCount As Integer = 0
    For i As Integer = 0 To vpMainControl.ShortcutItemPanel.Items.Count - 1
        With vpMainControl.ShortcutItemPanel.Items(i)
            Select Case .Name
                Case "biGSForm", "biReinfForm", "biCreateSection", "biRegionDetailing", "biAutoDesign"
                    If zbAutoDesign Then .Visible = False
                    If Not zbAutoDesign Then .Visible = True
                Case "biFailureDiag", "biLayout", "biReinforcement", "biUnlockDesign", "biCalculationReport", "biDetailReport"
                    If zbAutoDesign Then .Visible = True
                    If Not zbAutoDesign Then .Visible = False
            End Select
            If .Visible = True Then iCount = iCount + 1
        End With
    Next

    'this fix is a work around for the problem of ShortcutPanel size.
    'ths size of the panel does not adjust according to the items in it.
    w_AdjustShortcutPanelUI(iCount)


End Sub

任何想法都可能有所帮助,因为我认为这不是最佳解决方案。