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!
Comments