Setting a ToolStripStatusLabel control properties programmatically?

I have an application in which the users can control the look and feel of the application by changing properties for controls, i.e. FontBold, ForeColor, BackColor, Italics, etc.

I have a StatusStrip on my main form and it has 3 ToolStripStatusLabel within it. I can change the StatusStrip properties, however, I'm having trouble creating an instance of the ToolStripStatusLabel to change it's properties. I know this sounds pretty elementary, but it's still eluding me. Here is my code where I try to establish a new instance.

                Dim ctlType as Control

                Select Case ctlID
                    Case 1  'Buttons
                        ctlType = New Button
                    Case 2  'DataGridViews
                        ctlType = New DataGridView
                    Case 3  'GroupBoxes
                        ctlType = New GroupBox
                    Case 4  'TextBoxes
                        ctlType = New TextBox
                    Case 5  'Forms
                        ctlType = New Form
                    Case 6  'Labels
                        ctlType = New Label
                    Case 7  'ComboBoxes
                        ctlType = New ComboBox
                    Case 8  'RadioButton
                        ctlType = New RadioButton
                    Case 9  'CheckBoxes
                        ctlType = New CheckBox
                    Case 10 'TabControl
                        ctlType = New TabControl
                    Case 11 'StatusStrip
                        ctlType = New StatusStrip
                    Case 12 'MaskedTextBox
                        ctlType = New MaskedTextBox
                    Case 13 'ToolStripStatusLabel
                        ctlType = New ToolStripStatusLabel    <--- It doesn't like this declaration!
                End Select

Who is Participating?
Jacques Bourgeois (James Burger)Connect With a Mentor PresidentCommented:
This is because your variable is a Control, and the ToolStripStatusLable is not a Control.
A Control is an object that you put in a Form. The ToolStripStatusLabel does no go in a Form, it goes in a ToolStrip. So the items you add to a ToolStrip are not controls as the other ones.

Let's look at it from the documentation, that is a very good tool to understand a lot of things, such as what is happening here.

If you look at the documentation for the class of any of the "normal" controls, such as the Button (, you see in the Inheritance Hierarchy that they all inherits, directly or indirectly, from the Control class. You can thus handle them through a Control variable as you do when you assign them to your ctlType, and work with properties and methods that are common to all controls. This capacity of handling an object through a class from which it inherits is called polymorphism.

If you look at the documentation for the ToolStripStatus class (, you see in the Inheritance Hierarchy that it does not inherit from Control. In such a case, there is no polymorphism, and you cannot assign it to a variable of type Control. You will have to treat your ToolStripStatus through something else, either a ToolStripStatus variable, or if you are working with different ToolStrip controls, through a ToolStripItem variable, since this is the common class from which all ToolStrip controls inherits.
BlakeMcKennaAuthor Commented:
So when my ctlType is equal to a StatusStrip control...there is no way for me to loop thru this control to check for any occurrence of the ToolStripStatusLabel and change the properties?
Jacques Bourgeois (James Burger)PresidentCommented:
No, because ctlType is a Control and the ToolStripStatusLabel is not a Control although we see it and use it as one.
BlakeMcKennaAuthor Commented:
That's fine, I tried what you suggested in your first post and got it working! Thanks for your help James!
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.