Link to home
Start Free TrialLog in
Avatar of nepaluz
nepaluzFlag for United Kingdom of Great Britain and Northern Ireland

asked on

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!
User generated image
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?
ASKER CERTIFIED SOLUTION
Avatar of Todd Gerbert
Todd Gerbert
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of nepaluz

ASKER

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)
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.SuspendLayout()
        '
        'ToolStrip1
        '
        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"
        '
        'Form1
        '
        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.Controls.Add(Me.ToolStrip1)
        Me.Name = "Form1"
        Me.Text = "Form1"
        Me.ResumeLayout(False)
        Me.PerformLayout()

    End Sub

Open in new window

Oops...this is the button I was talking about to show all files...
Untitled.png
Oops again, wrong file. ;)
Untitled.png