Toolbar Control

Toggling Buttons

The toolbar control allows you to set the 'state' of the button, ie whether it is pressed or not. This is useful in something like a text editing program, when you want the Underline toolbar button to appear pressed if the current text is underlined. No doubt you will also want the button to toggle between pressed and not pressed when the user clicks it. This is all done using the Toolbar buttons Selected property.

First, we'll write some code that will set the button called 'Underline' to the correct state. To do this, we need to add some code into the RichTextBox's SelChange event:

Toolbar1.Buttons("Underline").Selected = Abs(RichTextBox1.SelUnderline)

All this code does is checks if the current text is underlined in the RichTextBox, and sets the buttons Selected property accordingly. As the Selected property can be 0 or 1 instead of True or False, where 0 is not selected and 1 is selected, we need to use the Abs function to convert the RichTextBoxes SelUnderline property (that is True or False) to 0 or 1.

Now, to make the button 'toggle' its state when it is clicked all you need to do is change the buttons Style to 1 - tbrChecked. Its that simple!

You might also like...

Comments

About the author

James Crowley

James Crowley United Kingdom

James first started this website when learning Visual Basic back in 1999 whilst studying his GCSEs. The site grew steadily over the years while being run as a hobby - to a regular monthly audien...

Interested in writing for us? Find out more.

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Theory is when you know something, but it doesn't work. Practice is when something works, but you don't know why. Programmers combine theory and practice: Nothing works and they don't know why.”