nepaluz
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!
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?
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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).
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
Oops...this is the button I was talking about to show all files...
Untitled.png
Untitled.png
Oops again, wrong file. ;)
Untitled.png
Untitled.png
ASKER
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)