1

Overview: Creating a small app to assist with reviewing log files for SCCM.

I just started working on a small app to analyze log files. The main form has an empty TabControl named "tcMain". It also has a button (For testing purposes) which will create a new "LogAssembly" class. This class contains the UI elements for a TabPage and other nested controls.

Inside of the LogAssembly class is a RichTextBox. It is anchored in a fashion where the text box will grow with the form size.

Problem: The first "LogAssembly" created works as intended. tcMain is populated and everything looks well. The richtextbox resizes as expected.

Any subsequent LogAssembly have issues. The rich textbox is huge and seems fill beyond the controls in which it is nested.

I suspect the issue has to do with the order inwhich i am creating / manipulating the controls. However, I do not have firm grasp of control management at runtime. I hope someone can point out what I am doing wrong here.

Here is the pertinent code:

Form1: (Press btnOptions to create new "LogAssembly" on tcMain)

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 Class:

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
Wariv
  • 15
  • 6
  • Breaking on the button push and stepping through the process seems to resolve the Anchoring issue. I threw a ton of Application.DoEvents() in the class file so I could see which part was causing the drawing issues. However, it did not reveal anything useful. I removed the breakpoint and left the "DoEvents()". Problem returned. Perhaps this is a threading, or timing issue? – Wariv Aug 20 '18 at 19:29
  • Drawing issue? I thought this was a control size problem? – LarsTech Aug 20 '18 at 19:31
  • Why are you assigning a `Point` to the `Size` properties? **Enable`Option Strict`**. – TnTinMn Aug 20 '18 at 19:32
  • The only difference I can think of when stepping through in debug is that I can click on the tabpage immediately after it's been drawn. and then continue stepping through, watching the remaining controls get drawn. – Wariv Aug 20 '18 at 19:33
  • I call it a drawing issue. It's still an anchoring issue to the best of my knowledge. Because if I remove the anchoring property line. it will draw fine. As far as the Point and size. I copied how I saw it done in the designer code. – Wariv Aug 20 '18 at 19:37
  • The first thing I would do is enabling Option Strict and cut the `ByRef` keyword in the class contructor `parent` parameters. IMO, you should refactor the whole class. All those objects *newed* in that way... You should have a custom control, a container for all other objects. It'ld allow you to have a more efficient, manageable reference for each control when you have to decide its behaviour. For example, I don't see any events attached to the newly added controls. What are they doing? Also, check out the properties you are setting. Add the controls after you have set their properties. – Jimi Aug 20 '18 at 19:38
  • Well, this is a learning experience for me. I am not a programmer by trade. And I am going by what I have been comfortable using in the past. This particular endeavor I just wanted to learn how to work with controls at runtime. Create somethign a bit less static. As for events. This is literally a UI issue I encountered almost immediately in the project, I have not started with events yet. I've never worked with custom controls before. I just assumed they were a class, like I created here. – Wariv Aug 20 '18 at 19:42
  • I fixed the Points oversight. I also Turned on strict mode and explicitly defined all variables. Issue still persists. As for the ByRef, Can you explain how that would be causing an issue? I understand, from what you were saying, there is a better way to accomplish this. And I will research custom controls Shortly. However, I don't see how the ByRef would cause an issue here. Legitimate asking to improve my knowledge. – Wariv Aug 20 '18 at 19:54
  • Instead of setting the width and height of your controls, you should be using the ClientSize Width and Height property of the parent container to lay things out correctly. Anchoring doesn't resize things for you, it just binds a side to the parent's side, so if it moves, it moves with it. – LarsTech Aug 20 '18 at 19:57
  • [Passing Arguments by Value and by Reference (Visual Basic)](https://learn.microsoft.com/en-us/dotnet/visual-basic/programming-guide/language-features/procedures/passing-arguments-by-value-and-by-reference). -- [ByRef vs ByVal Clarification](https://stackoverflow.com/questions/4383167/byref-vs-byval-clarification). - [ByVal and ByRef with reference type](https://stackoverflow.com/questions/19707622/byval-and-byref-with-reference-type?answertab=active#tab-top). – Jimi Aug 20 '18 at 20:19

1 Answers1

0

It has to do with the anchors and the order you add child controls to containers. If you take a look how the designer works there is an order:

Container0,1,2,... Container0 in your example is parent:

Add Container2 to Container1
Initialize Container1
Add Container3 to Container2
Initialize Container2
Add Container4 to Container3
Initialize Container3
...
...
in the end Add Container1 to Container0

You need also to suspend layout in the same order, so:

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)
  • Thank you so much. I appreciate the time to help me figure that out. As the others had mentioned my implementation probabaly won't win any awards. But Fundamentally it should work, and you helped me get past that particular hurdle. I will practice up with the SuspendLayout and add it to my bag of tricks. – Wariv Aug 20 '18 at 21:10