来看一个列子:
在ToolStrip上添加按钮、选择框、进度条
选择框的值有100、200、500。
当按钮按下时,进度条依据设置的值不断增加。
Private Sub ToolStripButton1_Click(sender As Object,e As EventArgs) Handles ToolStripButton1.Click Dim i As Integer = Integer.Parse(ToolStripComboBox1.Text) ToolStripProgressBar1.Maximum = i For j As Integer = 0 To i ToolStripProgressBar1.Value = j Next End Sub
获得工具栏中工具数量:
Private Sub Button2_Click(sender As Object,e As EventArgs) Handles Button2.Click MessageBox.Show(ToolStrip1.Items.Count) End Sub
移除工具栏中最后一个工具:
Private Sub Button3_Click(sender As Object,e As EventArgs) Handles Button3.Click Dim itemCount As Integer = ToolStrip1.Items.Count ToolStrip1.Items.RemoveAt(itemCount - 1) End Sub
Private Sub Button1_Click(sender As Object,e As EventArgs) Handles Button1.Click Dim tsb As New ToolStripButton() tsb.Text = "增加" tsb.DisplayStyle = ToolStripItemDisplayStyle.Image tsb.Image = Image.FromFile("D:\BoldB.png") ToolStrip1.Items.Add(tsb) AddHandler tsb.Click,AddressOf tsb_Click End Sub
Private Sub tsb_Click(sender As Object,e As EventArgs) MessageBox.Show("按下了新增加的按钮") End Sub
运行效果如下:
需要注意的是,如果移除工具后,又需要调用该工具,那么将会引发错误,例如我将ToolStripComboBox1移除后,又按下了ToolStripButton1:
由于.net平台下C#和vb.NET很相似,本文也可以为C#爱好者提供参考。
学习更多vb.net知识,请参看vb.net 教程 目录