VB 2010 Initialising a Toolstriplabelin code

I am trying to declare a few ToolStripLabels in code using VB2010, however the owner property does not seem to "stick", and therefore, the labels are not shown on their parent toolstrips. The image below shows that owner is nothing despite clearly  naming / declaring it!
showing values after break
When I add the code inside a function that is called on form load, then the labels get their owners. Is this a known bug, design flaw or am I missing something or breaking some obscure programming rule?
LVL 17
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Todd GerbertIT ConsultantCommented:
I'd wager that ToolStrip2 hasn't been created yet when that class-level declaration you're using is executed.

Declare the variable Public WithEvents at the class level, but don't set it until Form_Load.
Public Class Form1
    Public WithEvents Home_List As ToolStripLabel
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Home_List = New ToolStripLabel With {.Owner = ToolStrip1, .Name = "aList", .Text = "Account List"}
    End Sub
End Class

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
nepaluzAuthor Commented:
Toolstrip2 is actually part of the form (drag and drop!) Also, like I stated in my question, when I set owner inside a function called during form load (or on form load as you suggest), the owner IS set.

So the question still stands, is there some obscure programming rule that I have broken? (there certainly is no warning in the IDE regarding the code)
Todd GerbertIT ConsultantCommented:
Even when you drag & drop something onto your form, Visual Studio writes some code for you that initializes it.  The class-level declaration that's giving you trouble is running before that hidden-behind-the-scenes initialization code runs.  ToolStrip2 is Nothing, so when you assign it to the Owner then Owner becomes Nothing.

In fact, if you click the button to show hidden files in Visual Studio you'll see that your form is actually composed of several files - the FormName.Designer.vb file contains a sub called InitializeComponent that sets up your ToolStrip (InitializeComponent is called by the Form's constructor). Class-level variables, like the Public WithEvents Home_List As New ToolStripLabel With { .Owner = ToolStrip2 } statement you're using, are initialized before any sub or function runs, including InitializeComponent.

What I suggested in http:#a35248461 is the correct way to setup such variables (it doesn't have to be in Form_Load, just any sub/function).

 <System.Diagnostics.DebuggerStepThrough()> _
    Private Sub InitializeComponent()
        Me.ToolStrip1 = New System.Windows.Forms.ToolStrip()
        Me.ToolStrip2.Location = New System.Drawing.Point(0, 0)
        Me.ToolStrip2.Name = "ToolStrip2"
        Me.ToolStrip2.Size = New System.Drawing.Size(284, 25)
        Me.ToolStrip2.TabIndex = 0
        Me.ToolStrip2.Text = "ToolStrip2"
        Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
        Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
        Me.ClientSize = New System.Drawing.Size(284, 262)
        Me.Name = "Form1"
        Me.Text = "Form1"

    End Sub

Open in new window

Todd GerbertIT ConsultantCommented:
Oops...this is the button I was talking about to show all files...
Todd GerbertIT ConsultantCommented:
Oops again, wrong file. ;)
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.