VB.NET: Working with ToolStrip Control

ToolStrip Control is an control object that used in the VB.NET windows application to create ToolBar. We can find it on toolbox under Menus & Toolbar. Double click add it into Form.

Control will stick on the top of Form.

8 types of control can be added to ToolStrip.

For initiation, we'll add buttons for CRUD. Buttons are  Save, Edit, Delete, Cancel, and Close. Set focus on toolstrip then click the dropdown and choose "button" as above image. Do it again to get 3 buttons added.  After adding 3 buttons, add a SplitButton, then add 2 buttons again. The result should be as following:


Give focus on first button image, right-click --> DisplayStyle --> ImageAndText. This step will change the button style displaying Image and Text. Do again for 4 other buttons.

Below image shows the result.

Next, we'll change the button text. Give focus on button then go to properties box to change Text. Changes each button's text with Save, Edit, Delete, Cancel, and Close.

Change also the name property with a value representing the function. It will help us when writing code.

The next step is to set an image for each button. Set focus on the button, right-click and click Set Image...

A "Select Source" dialog will be prompted. Choose option "Local resource" then click Import button.

Chose image files that represent the button's function, consider the appropriate size of images. When the image has been chosen click Open...

The image will appear on preview then click OK.

The chosen image will be set as the image of the button. Repeat process for 4 other buttons.

Here is my result.


Code below is a sample to add procedure inside click event of toolbar buttons.
Private Sub tbrSave_Click(ByVal sender As System.Object, _
   
ByVal e As System.EventArgs) Handles tbrSave.Click
    MsgBox("Save Button Clicked")
End Sub

Private Sub tbrEdit_Click(
ByVal sender As System.Object, _
   
ByVal e As System.EventArgs) Handles tbrEdit.Click
    MsgBox("Edit Button Clicked")
End Sub

Private Sub tbrDelete_Click(
ByVal sender As System.Object, _
   
ByVal e As System.EventArgs) Handles tbrDelete.Click
    MsgBox("Delete Button Clicked")
End Sub

Private Sub tbrCancel_Click(
ByVal sender As System.Object, _
   
ByVal e As System.EventArgs) Handles tbrCancel.Click
    MsgBox("Cancel Button Clicked")
End Sub

Private Sub tbrClose_Click(
ByVal sender As System.Object, _
   
ByVal e As System.EventArgs) Handles tbrClose.Click
    Me.Close()
End Sub


Post a Comment

0 Comments