Members

Technology Zones

IBM Learning Center

Articles

Hosted By

MaximumASP

Info

Rated
Read 61,775 times

Contents

Related Categories

Toolbar Control - Toggling Buttons

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!

James first started writing tutorials on Visual Basic in 1999 whilst starting this website (then known as VB Web). Since then, the site has grown rapidly, and James has written numerous tutorials, articles and reviews on VB, PHP, ASP and C#. In October 2003, James formed the company Developer Fusion Ltd, which owns this website, and also offers various development services. In his spare time, he's a 3rd year undergraduate studying Computer Science in the UK. He's also a Visual Basic MVP.

Comments

  • Re: [2] Toolbar Control

    Posted by vblife2003 on 19 Jan 2007

    Hi,


    I am trying to create keyboard shortcuts for toolbar buttons in VB.NET. Eg. For a button titled Manual I am setting the Text property to '&Manual'. In run mode when I press Alt...

  • Re: [2] Toolbar Control

    Posted by 3nter on 10 Jul 2006

    How would i resize the button if i had the text align to the right.


    for some reason when i try to change the width it switches right back to the width of whichever button that wou...

  • Problem with diabling images in the tool bar

    Posted by skomali on 05 Apr 2005

    Hi!
    I am using a imagelist with a toolbar and when I am trying to disable an image in the tool bar the image is getting completely faded (no image is being displayed but simply a complete gray sectio...

  • TBPaint

    Posted by donkey on 03 Oct 2003

    FYI, if you want to do your own Toolbar graphics I have a freeware toolbar paint program that allows you to create imagelists quickly and easily. You can import icons, individual images from other ima...

  • toolbar code

    Posted by ocanse on 05 Sep 2003

    HI all

    this is my code en c# ,
    I have six button but they no working .

    private void toolBar1_ButtonClick(object sender, System.Windows.Forms.ToolBarButtonClickEventArgs e)
    {

    }

    who can he...