vb.net - how to programmatically hide toolstrip buttons

LeighWardle
LeighWardle used Ask the Experts™
on
Hi Experts,

I want to be able to programmatically hide some toolstrip buttons.

I've tried code like:

ToolStripButtonTLD.Visible = False

Open in new window


But that doesn't work.

Regards,
Leigh
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Paul JacksonSoftware Engineer
Top Expert 2011

Commented:
You need to use the Available property.

ToolStripButtonTLD.Available= False

Open in new window


https://docs.microsoft.com/en-us/dotnet/api/system.windows.forms.toolstripbutton

Author

Commented:
Hi Paul,

Thanks, but the .Available property is not working for me.

Do I need to combine it with the .Visible property? or something else?

Regards,
Leigh
Paul JacksonSoftware Engineer
Top Expert 2011

Commented:
Can you post some code, in what scenario are you setting the .Visible/.Available properties ?
Expert Spotlight: Joe Anderson (DatabaseMX)

We’ve posted a new Expert Spotlight!  Joe Anderson (DatabaseMX) has been on Experts Exchange since 2006. Learn more about this database architect, guitar aficionado, and Microsoft MVP.

Author

Commented:
Hi Paul,

I created a simple test code - and .Available worked fine.

My production code is many thousands of lines, but here is the excerpt that attempts to toggle the visibility of the toolbarstrip buttons:

            ' Configure toolbar icons
            Select Case g_enumDesignMethodCurrent
                Case enumDesignMethod.enumDesignMethod_AustroadsDesign '"Austroads Pavement Design (2017)"

                    'deactivate/activate various features
                    ToolStripButtonTLD.Available = True
                    ToolStripButtonHaulTrucks.Available = False
                    ToolStripButtonLoads.Available = False
                    ToolStripButtonTraffic.Available = False
                    ToolStripButtonCoordinates.Available = False
                    ToolStripButtonGraph.Available = gboolParametricAnalysis '20180319
                    ToolStripButtonMax.Available = False
                    PlotResultsToolStripMenuItem.Available = False

                Case enumDesignMethod.enumDesignMethod_GeneralDesign, enumDesignMethod.enumDesignMethod_GeneralAnalysis '"General Design", "General Analysis"

                    ToolStripButtonTLD.Available = False
                    ToolStripButtonHaulTrucks.Available = False
                    ToolStripButtonLoads.Available = True
                    ToolStripButtonTraffic.Available = True
                    ToolStripButtonCoordinates.Available = True
                    ToolStripButtonGraph.Available = True '20180319
                    'If g_enumDesignMethodCurrent <> enumDesignMethod.enumDesignMethod_HaulRoadDesign Then
                    '    ToolStripButtonGraph.Available = True
                    'Else
                    '    ToolStripButtonGraph.Available = False
                    'End If
                    PlotResultsToolStripMenuItem.Available = True

                Case enumDesignMethod.enumDesignMethod_HaulRoadDesign '"Haul Road Design"

                    ToolStripButtonTLD.Available = False
                    ToolStripButtonLoads.Available = False
                    ToolStripButtonTraffic.Available = False
                    ToolStripButtonCoordinates.Available = False
                    ToolStripButtonGraph.Available = True '20180319
                    ToolStripButtonHaulTrucks.Available = True
                    PlotResultsToolStripMenuItem.Available = True
            End Select

Open in new window


When I step through the code one statement at a time, after executing this statement:
ToolStripButtonTLD.Available = False

Open in new window


The button, ToolStripButtonTLD, is still visible.

A watch on ToolStripButtonTLD.visible shows that it is False.

Regards,
Leigh
Software Engineer
Top Expert 2011
Commented:
Yes but in what method/function is this code called ? InitializeComponent(), Load_Form() or somewhere else?
The Visible property will often seem to have the wrong value because it only gets updated once the control is rendered, hence the question where and when is the code called.

Author

Commented:
Hi Paul,

Yes, the problematic code was being called by Load_Form().
I've refactored my code and it's working now.

Thanks for you help.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial