VB.Net Tutorial/GUI/ToolBar
Add action listener to buttons on a ToolBar
<source lang="vbnet">Imports System.Windows.Forms public class ToolBarMenu
public Shared Sub Main Application.Run(New Form1) End Sub
End class Public Class Form1
Inherits System.Windows.Forms.Form
- Region " Windows Form Designer generated code "
Public Sub New() MyBase.New() "This call is required by the Windows Form Designer. InitializeComponent() "Add any initialization after the InitializeComponent() call End Sub "Form overrides dispose to clean up the component list. Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean) If disposing Then If Not (components Is Nothing) Then components.Dispose() End If End If MyBase.Dispose(disposing) End Sub "Required by the Windows Form Designer Private components As System.ruponentModel.IContainer "NOTE: The following procedure is required by the Windows Form Designer "It can be modified using the Windows Form Designer. "Do not modify it using the code editor. Friend WithEvents MainMenu1 As System.Windows.Forms.MainMenu Friend WithEvents mnuEdit As System.Windows.Forms.MenuItem Friend WithEvents mnuEditCut As System.Windows.Forms.MenuItem Friend WithEvents mnuEditCopy As System.Windows.Forms.MenuItem Friend WithEvents mnuEditPaste As System.Windows.Forms.MenuItem Friend WithEvents txtEdit As System.Windows.Forms.TextBox Friend WithEvents ContextMenu1 As System.Windows.Forms.ContextMenu Friend WithEvents MenuItem7 As System.Windows.Forms.MenuItem Friend WithEvents ImageList1 As System.Windows.Forms.ImageList Friend WithEvents ToolBarButton1 As System.Windows.Forms.ToolBarButton Friend WithEvents ToolBarButton2 As System.Windows.Forms.ToolBarButton Friend WithEvents ToolBarButton3 As System.Windows.Forms.ToolBarButton Friend WithEvents ToolBar1 As System.Windows.Forms.ToolBar Friend WithEvents cmnuCut As System.Windows.Forms.MenuItem Friend WithEvents cmnuCopy As System.Windows.Forms.MenuItem Friend WithEvents cmnuPaste As System.Windows.Forms.MenuItem <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent() Me.ruponents = New System.ruponentModel.Container() Me.MainMenu1 = New System.Windows.Forms.MainMenu() Me.mnuEdit = New System.Windows.Forms.MenuItem() Me.mnuEditCut = New System.Windows.Forms.MenuItem() Me.mnuEditCopy = New System.Windows.Forms.MenuItem() Me.mnuEditPaste = New System.Windows.Forms.MenuItem() Me.txtEdit = New System.Windows.Forms.TextBox() Me.ContextMenu1 = New System.Windows.Forms.ContextMenu() Me.cmnuCut = New System.Windows.Forms.MenuItem() Me.cmnuCopy = New System.Windows.Forms.MenuItem() Me.cmnuPaste = New System.Windows.Forms.MenuItem() Me.MenuItem7 = New System.Windows.Forms.MenuItem() Me.ImageList1 = New System.Windows.Forms.ImageList(Me.ruponents) Me.ToolBarButton1 = New System.Windows.Forms.ToolBarButton() Me.ToolBarButton2 = New System.Windows.Forms.ToolBarButton() Me.ToolBarButton3 = New System.Windows.Forms.ToolBarButton() Me.ToolBar1 = New System.Windows.Forms.ToolBar() Me.SuspendLayout() " "MainMenu1 " Me.MainMenu1.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.mnuEdit}) " "mnuEdit " Me.mnuEdit.Index = 0 Me.mnuEdit.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.mnuEditCut, Me.mnuEditCopy, Me.mnuEditPaste}) Me.mnuEdit.Text = "&Edit" " "mnuEditCut " Me.mnuEditCut.Index = 0 Me.mnuEditCut.Shortcut = System.Windows.Forms.Shortcut.CtrlX Me.mnuEditCut.Text = "Cu&t" " "mnuEditCopy " Me.mnuEditCopy.Index = 1 Me.mnuEditCopy.Shortcut = System.Windows.Forms.Shortcut.CtrlC Me.mnuEditCopy.Text = "&Copy" " "mnuEditPaste " Me.mnuEditPaste.Index = 2 Me.mnuEditPaste.Shortcut = System.Windows.Forms.Shortcut.CtrlV Me.mnuEditPaste.Text = "&Paste" " "txtEdit " Me.txtEdit.ContextMenu = Me.ContextMenu1 Me.txtEdit.Location = New System.Drawing.Point(0, 24) Me.txtEdit.Multiline = True Me.txtEdit.Name = "txtEdit" Me.txtEdit.ScrollBars = System.Windows.Forms.ScrollBars.Both Me.txtEdit.Size = New System.Drawing.Size(248, 176) Me.txtEdit.TabIndex = 0 Me.txtEdit.Text = "" " "ContextMenu1 " Me.ContextMenu1.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.cmnuCut, Me.cmnuCopy, Me.cmnuPaste}) " "cmnuCut " Me.cmnuCut.Index = 0 Me.cmnuCut.Text = "Cut" " "cmnuCopy " Me.cmnuCopy.Index = 1 Me.cmnuCopy.Text = "Copy" " "cmnuPaste " Me.cmnuPaste.Index = 2 Me.cmnuPaste.Text = "Paste" " "MenuItem7 " Me.MenuItem7.Index = -1 Me.MenuItem7.Text = "" " "ToolBarButton1 " Me.ToolBarButton1.ImageIndex = 0 " "ToolBarButton2 " Me.ToolBarButton2.ImageIndex = 1 " "ToolBarButton3 " Me.ToolBarButton3.ImageIndex = 2 " "ToolBar1 " Me.ToolBar1.Buttons.AddRange(New System.Windows.Forms.ToolBarButton() {Me.ToolBarButton1, Me.ToolBarButton2, Me.ToolBarButton3}) Me.ToolBar1.ButtonSize = New System.Drawing.Size(23, 22) Me.ToolBar1.DropDownArrows = True Me.ToolBar1.Name = "ToolBar1" Me.ToolBar1.ShowToolTips = True Me.ToolBar1.Size = New System.Drawing.Size(248, 25) Me.ToolBar1.TabIndex = 1 " "Form1 " Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13) Me.ClientSize = New System.Drawing.Size(248, 201) Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.ToolBar1, Me.txtEdit}) Me.Menu = Me.MainMenu1 Me.Name = "Form1" Me.Text = "Form1" Me.ResumeLayout(False) End Sub
- End Region
Private Sub ToolBar1_ButtonClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.ToolBarButtonClickEventArgs) Handles ToolBar1.ButtonClick Select Case ToolBar1.Buttons.IndexOf(e.Button) Case 0 mnuEditCut_Click(sender, e) Case 1 mnuEditCopy_Click(sender, e) Case 2 mnuEditPaste_Click(sender, e) End Select End Sub Private Sub mnuEditCut_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles mnuEditCut.Click, cmnuCut.Click txtEdit.Cut() End Sub Private Sub mnuEditCopy_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles mnuEditCopy.Click, cmnuCopy.Click txtEdit.Copy() End Sub
Private Sub mnuEditPaste_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles mnuEditPaste.Click, cmnuPaste.Click txtEdit.Paste() End Sub
End Class</source>
Button, Menu, ComboBox on ToolBar
<source lang="vbnet">Imports System.Drawing Imports System.Drawing.Drawing2D Imports System.Windows.Forms public class ToolBarButtonComboBox
public Shared Sub Main Application.Run(New Form1) End Sub
End class Public Class Form1
Inherits System.Windows.Forms.Form
- Region " Windows Form Designer generated code "
Public Sub New() MyBase.New() "This call is required by the Windows Form Designer. InitializeComponent() "Add any initialization after the InitializeComponent() call End Sub "Form overrides dispose to clean up the component list. Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean) If disposing Then If Not (components Is Nothing) Then components.Dispose() End If End If MyBase.Dispose(disposing) End Sub "Required by the Windows Form Designer Private components As System.ruponentModel.IContainer "NOTE: The following procedure is required by the Windows Form Designer "It can be modified using the Windows Form Designer. "Do not modify it using the code editor. Friend WithEvents ToolBar1 As System.Windows.Forms.ToolBar Friend WithEvents Label1 As System.Windows.Forms.Label Friend WithEvents TextBox1 As System.Windows.Forms.TextBox Friend WithEvents ToolBarButton1 As System.Windows.Forms.ToolBarButton Friend WithEvents ToolBarButton2 As System.Windows.Forms.ToolBarButton Friend WithEvents ToolBarButton3 As System.Windows.Forms.ToolBarButton Friend WithEvents ContextMenu1 As System.Windows.Forms.ContextMenu Friend WithEvents MenuItem1 As System.Windows.Forms.MenuItem Friend WithEvents MenuItem2 As System.Windows.Forms.MenuItem Friend WithEvents MenuItem3 As System.Windows.Forms.MenuItem Friend WithEvents ToolBarButton4 As System.Windows.Forms.ToolBarButton Friend WithEvents ToolBarButton5 As System.Windows.Forms.ToolBarButton Friend WithEvents ToolBarButton6 As System.Windows.Forms.ToolBarButton Friend WithEvents ComboBox1 As System.Windows.Forms.ruboBox <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent() Me.ToolBar1 = New System.Windows.Forms.ToolBar Me.Label1 = New System.Windows.Forms.Label Me.TextBox1 = New System.Windows.Forms.TextBox Me.ToolBarButton1 = New System.Windows.Forms.ToolBarButton Me.ToolBarButton2 = New System.Windows.Forms.ToolBarButton Me.ToolBarButton3 = New System.Windows.Forms.ToolBarButton Me.ContextMenu1 = New System.Windows.Forms.ContextMenu Me.MenuItem1 = New System.Windows.Forms.MenuItem Me.MenuItem2 = New System.Windows.Forms.MenuItem Me.MenuItem3 = New System.Windows.Forms.MenuItem Me.ToolBarButton4 = New System.Windows.Forms.ToolBarButton Me.ToolBarButton5 = New System.Windows.Forms.ToolBarButton Me.ToolBarButton6 = New System.Windows.Forms.ToolBarButton Me.ruboBox1 = New System.Windows.Forms.ruboBox Me.SuspendLayout() " "ToolBar1 " Me.ToolBar1.Buttons.AddRange(New System.Windows.Forms.ToolBarButton() {Me.ToolBarButton1, Me.ToolBarButton2, Me.ToolBarButton3, Me.ToolBarButton4, Me.ToolBarButton5, Me.ToolBarButton6}) Me.ToolBar1.DropDownArrows = True Me.ToolBar1.Location = New System.Drawing.Point(0, 0) Me.ToolBar1.Name = "ToolBar1" Me.ToolBar1.ShowToolTips = True Me.ToolBar1.Size = New System.Drawing.Size(368, 42) Me.ToolBar1.TabIndex = 0 " "Label1 " Me.Label1.Font = New System.Drawing.Font("Microsoft Sans Serif", 24.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte)) Me.Label1.Location = New System.Drawing.Point(0, 48) Me.Label1.Name = "Label1" Me.Label1.Size = New System.Drawing.Size(160, 40) Me.Label1.TabIndex = 1 Me.Label1.Text = "Toolbars" " "TextBox1 " Me.TextBox1.Location = New System.Drawing.Point(72, 136) Me.TextBox1.Name = "TextBox1" Me.TextBox1.Size = New System.Drawing.Size(152, 20) Me.TextBox1.TabIndex = 2 Me.TextBox1.Text = "" " "ToolBarButton1 " Me.ToolBarButton1.Text = "Button 1" " "ToolBarButton2 " Me.ToolBarButton2.Text = "Button 2" " "ToolBarButton3 " Me.ToolBarButton3.Style = System.Windows.Forms.ToolBarButtonStyle.ToggleButton Me.ToolBarButton3.Text = "Toggle Button " " "ContextMenu1 " Me.ContextMenu1.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.MenuItem1, Me.MenuItem2, Me.MenuItem3}) " "MenuItem1 " Me.MenuItem1.Index = 0 Me.MenuItem1.Text = "Red" " "MenuItem2 " Me.MenuItem2.Index = 1 Me.MenuItem2.Text = "Green" " "MenuItem3 " Me.MenuItem3.Index = 2 Me.MenuItem3.Text = "Blue" " "ToolBarButton4 " Me.ToolBarButton4.DropDownMenu = Me.ContextMenu1 Me.ToolBarButton4.Style = System.Windows.Forms.ToolBarButtonStyle.DropDownButton Me.ToolBarButton4.Text = "Button 4" " "ToolBarButton5 " Me.ToolBarButton5.Style = System.Windows.Forms.ToolBarButtonStyle.Separator " "ToolBarButton6 " Me.ToolBarButton6.Style = System.Windows.Forms.ToolBarButtonStyle.Separator " "ComboBox1 " Me.ruboBox1.Items.AddRange(New Object() {"Item 0", "Item 1", "Item 2"}) Me.ruboBox1.Location = New System.Drawing.Point(264, 8) Me.ruboBox1.Name = "ComboBox1" Me.ruboBox1.Size = New System.Drawing.Size(88, 21) Me.ruboBox1.TabIndex = 3 " "Form1 " Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13) Me.ClientSize = New System.Drawing.Size(368, 273) Me.Controls.Add(Me.ruboBox1) Me.Controls.Add(Me.TextBox1) Me.Controls.Add(Me.Label1) Me.Controls.Add(Me.ToolBar1) Me.Name = "Form1" Me.Text = "Form1" Me.ResumeLayout(False) End Sub
- End Region
Private Sub ToolBar1_ButtonClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.ToolBarButtonClickEventArgs) Handles ToolBar1.ButtonClick TextBox1.Text = "You clicked " & e.Button.Text End Sub Private Sub MenuItem1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem1.Click TextBox1.Text = "You clicked the Red item." End Sub Private Sub MenuItem2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem2.Click TextBox1.Text = "You clicked the Green item." End Sub Private Sub MenuItem3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem3.Click TextBox1.Text = "You clicked the Blue item." End Sub Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged TextBox1.Text = "You selected item " & ComboBox1.SelectedIndex End Sub
End Class</source>