Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All

Pushing Buttons: Adding Toolbars and Buttons

In this lesson, you will learn how to add a toolbar to an application, how to add toolbar buttons, and how to respond to the click events of the buttons.

There is typically a toolbar at the top of any standard Windows-based application. The toolbar provides direct access to functionality in the application at the click of a button. You can add a ToolStrip control to an application, and then add buttons to the toolbar. You can add individual ToolStripButton controls, or you can choose from a selection of other controls, such as the ToolStripTextBox control, the ToolStripDropDownButton control, and more.

To add a toolbar and toolbar button to a Windows Form

  1. On the File menu, click New Project.

    The New Project dialog box appears.

  2. Click Windows Forms Application and then click OK.

  3. Drag a ToolStrip control from the Toolbox to the form.

    ToolStrip1 appears in the component tray, and a toolbar that contains a drop-down arrow is added to the top of the form.

  4. Click the drop-down arrow, and then click Button to add a button to the toolbar, as shown in the following illustration.

    Adding a button to the ToolStrip control

    Toolstrip control

    ToolStripButton1 appears on the toolbar.

  5. Click ToolstripButton1 on the form, and then change the following properties in the Properties window.

    Property

    Value

    Name

    Cut

    Text

    Cut

    DisplayStyle

    Text

    NoteNote

    In this example, you display only text on the button, but you could also display an image by setting the DisplayStyle property to ImageAndText.

  6. Add a TextBox control to the form, leaving the default name of TextBox1.

  7. Click the TextBox control, and then change the following properties in the Properties window:

    Property

    Value

    Size

    260, 20

    Text

    This is a simple test

  8. Double-click the Cut button to enter the default Click event handler.

  9. Add the following code to the Cut_Click event handler. This code cuts the selected text in TextBox1 and adds it to the Clipboard.

    
    Me.TextBox1.Cut()
    
    
    
  10. Press F5 to run the code.

  11. In the text box, select the word simple, and then click Cut.

    The word simple is deleted from the text box.

  12. Close the application.

As a shortcut, you can add several standard buttons all at once. The ToolStrip control has a ToolStrip Tasks pane that you can access by clicking the smart task arrow at the upper-right corner of the control. This task pane lets you insert seven standard buttons onto the toolbar, as shown in the following illustration.

Accessing the ToolStrip Tasks pane

Toolstrip Tasks

To add a set of standard toolbar buttons to a Windows Form

  1. Select the ToolStrip control and click the smart task arrow at the upper-right corner of the control. Then click Insert Standard Items.

    Seven standard buttons (New, Open, Save, Print, Cut, Copy, Paste, and Help) become visible on the toolbar.

  2. Double-click the CutToolStripButton control to enter the default Click event handler, and add the following code. This code cuts the selected text in TextBox1 and copies it to the Clipboard.

    
    Me.TextBox1.Cut()
    
    
    
  3. Add the following code underneath the CutToolStripButton_Click procedure. This code copies selected text in TextBox1 to the Clipboard when the user clicks the Copy button. It pastes the contents of the Clipboard to the selection in TextBox1 when the user clicks the Paste button.

    
    Private Sub CopyToolStripButton_Click() Handles CopyToolStripButton.Click
        Me.TextBox1.Copy()
    End Sub
    
    Private Sub PasteToolStripButton_Click() Handles PasteToolStripButton.Click
        Me.TextBox1.Paste()
    End Sub
    
    
    
  4. Press F5 to run the code.

  5. When the form appears, click the Copy button.

  6. Move the insertion point to the end of the sentence, press the SPACEBAR, and then click the Paste button.

  7. Select the word simple in the second sentence, and click the Cut button that has the scissor icon.

  8. Close the application.

In this lesson, you learned how to add a ToolStrip control to an application. You then learned how to add a button to the toolbar and write code in the Click event handler of the button. You also learned how to add a set of standard buttons to the toolbar.

In the next lesson, you'll learn how to make your application look like Windows Explorer by using a TreeView control.

Next Lesson: Providing Your Own Explorer: Working with TreeView Controls

Show:
© 2015 Microsoft