VB.NET在运行时创建的控件上出现意外的锚定行为

时间:2018-08-20 18:50:58

标签: .net vb.net winforms

概述:创建一个小型应用程序以帮助审查SCCM的日志文件。

我刚刚开始研究一个小型应用程序以分析日志文件。主窗体有一个空的TabControl,名为“ tcMain”。它还具有一个按钮(出于测试目的),它将创建一个新的“ LogAssembly”类。此类包含TabPage和其他嵌套控件的UI元素。

在LogAssembly类的内部是一个RichTextBox。它的锚定方式是文本框将随着表单大小而增长。

问题:创建的第一个“ LogAssembly”按预期工作。 tcMain已填充,一切看起来都很好。富文本框将按预期调整大小。

任何后续LogAssembly都有问题。富文本框非常大,似乎超出了其嵌套控件的范围。

我怀疑问题与我创建/操纵控件的顺序有关。但是,我对运行时的控制管理没有把握。我希望有人可以指出我在这里做错了。

以下是相关代码:

Form1:(按btnOptions在tcMain上创建新的“ LogAssembly”)

Public Class Form1
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Me.Text = "SCCM Log Analyzer (v" & APP_VERSION & ")"
    End Sub


    Private Sub btnOptions_Click(sender As Object, e As EventArgs) Handles btnOptions.Click
        Dim TestA As New LogAssembly(tcMain, "Example_")
    End Sub

End Class

LogAssembly类:

Public Class LogAssembly
Public UI_ParentTab As TabPage = New TabPage

'Main UI Elements
Public UI_GroupBoxA As GroupBox = New GroupBox
Public UI_ChildTabControl As TabControl = New TabControl
Public UI_PageEvents As TabPage = New TabPage
Public UI_PageText As TabPage = New TabPage
Public UI_PageItem As TabPage = New TabPage

'Event View Elements
Public UI_EventsText As RichTextBox = New RichTextBox
Public UI_EventsResearch As Button = New Button


Public Sub New(ByRef parent As TabControl, ByVal data As String)
    UI_ParentTab.Text = data
    parent.TabPages.Add(UI_ParentTab)

    'Group Box UI
    UI_GroupBoxA.Anchor = CType((((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Bottom) Or System.Windows.Forms.AnchorStyles.Left) Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
    UI_ParentTab.Controls.Add(UI_GroupBoxA)
    UI_GroupBoxA.Location = New System.Drawing.Point(5, 5)
    UI_GroupBoxA.Size = New Point(912, 405)
    UI_GroupBoxA.Text = "Selected Log"

    'Tab Control UI
    UI_GroupBoxA.Controls.Add(UI_ChildTabControl)
    UI_ChildTabControl.Anchor = CType((((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Bottom) Or System.Windows.Forms.AnchorStyles.Left) Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
    UI_ChildTabControl.Location = New System.Drawing.Point(5, 15)
    UI_ChildTabControl.Size = New Point(900, 385)

    'Tab Page Events
    UI_ChildTabControl.TabPages.Add(UI_PageEvents)
    UI_PageEvents.Text = "Event View"
    UI_PageEvents.Controls.Add(UI_EventsText)
    UI_PageEvents.Controls.Add(UI_EventsResearch)
    '_TextBox
    UI_EventsText.Text = data
    UI_EventsText.Anchor = CType((((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Bottom) Or System.Windows.Forms.AnchorStyles.Left) Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
    UI_EventsText.Location = New System.Drawing.Point(5, 30)
    UI_EventsText.Size = New Point(490, 140)
    '_Button
    UI_EventsResearch.Location = New Point(5, 5)
    UI_EventsResearch.Text = "Things"


    'Tab Page Text
    UI_ChildTabControl.TabPages.Add(UI_PageText)
    UI_PageText.Text = "Text View"


    'Tab Page Item
    UI_ChildTabControl.TabPages.Add(UI_PageItem)
    UI_PageItem.Text = "Item View"

End Sub
End Class

1 个答案:

答案 0 :(得分:0)

这与锚点以及将子控件添加到容器的顺序有关。如果您看一下设计师的工作方式,则有一个命令:

  

Container0,1,2,...您的示例中的Container0是父级:

     

将Container2添加到Container1
  初始化Container1
  将Container3添加到Container2
  初始化Container2
  将Container4添加到Container3
  初始化Container3
  ...
  ...
  最后将Container1添加到Container0

您还需要以相同的顺序暂停布局,因此:

UI_ParentTab.SuspendLayout() 'Container1
UI_GroupBoxA.SuspendLayout() 'Container2
UI_ChildTabControl.SuspendLayout() 'Container3
UI_PageEvents.SuspendLayout() 'Container4
parent.SuspendLayout() 'Container0

'UI_ParentTab
UI_ParentTab.Controls.Add(UI_GroupBoxA)
UI_ParentTab.Text = data

'Group Box UI
UI_GroupBoxA.Controls.Add(UI_ChildTabControl)
UI_GroupBoxA.Anchor = CType((((System.Windows.Forms.AnchorStyles.Top _
    Or System.Windows.Forms.AnchorStyles.Bottom) _
    Or System.Windows.Forms.AnchorStyles.Left) _
    Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
UI_GroupBoxA.Location = New System.Drawing.Point(5, 5)
UI_GroupBoxA.Size = New Size(912, 405)
UI_GroupBoxA.Text = "Selected Log"

'UI_ChildTabControl
UI_ChildTabControl.TabPages.Add(UI_PageEvents)
UI_ChildTabControl.TabPages.Add(UI_PageText)
UI_ChildTabControl.TabPages.Add(UI_PageItem)
UI_ChildTabControl.Anchor = CType((((System.Windows.Forms.AnchorStyles.Top _
    Or System.Windows.Forms.AnchorStyles.Bottom) _
    Or System.Windows.Forms.AnchorStyles.Left) _
    Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
UI_ChildTabControl.Location = New System.Drawing.Point(5, 15)
UI_ChildTabControl.Size = New Size(900, 385)

'Tab Page Events
UI_PageEvents.Controls.Add(UI_EventsText)
UI_PageEvents.Controls.Add(UI_EventsResearch)
UI_PageEvents.Text = "Event View"
'_TextBox
UI_EventsText.Anchor = CType((((System.Windows.Forms.AnchorStyles.Top _
    Or System.Windows.Forms.AnchorStyles.Bottom) _
    Or System.Windows.Forms.AnchorStyles.Left) _
    Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
UI_EventsText.Text = data
UI_EventsText.Location = New System.Drawing.Point(5, 30)
UI_EventsText.Size = New Size(490, 140)
'_Button
UI_EventsResearch.Location = New Point(5, 5)
UI_EventsResearch.Text = "Things"

'Tab Page Text
UI_PageText.Text = "Text View"

'Tab Page Item
UI_PageItem.Text = "Item View"

parent.TabPages.Add(UI_ParentTab)

UI_ParentTab.ResumeLayout(False)
UI_GroupBoxA.ResumeLayout(False)
UI_ChildTabControl.ResumeLayout(False)
UI_PageEvents.ResumeLayout(False)
parent.ResumeLayout(False)