通过上一节的学习,大家对StatusStrip控件有了初步了解,
本节主要讲解如何通过代码操作StatusStrip,在上一节窗体基础上增加两个按钮:
'修改状态栏标签 Private Sub Button1_Click(sender As Object,e As EventArgs) Handles Button1.Click ToolStripStatusLabel1.Text = "这是状态栏" ToolStripStatusLabel1.Image = Nothing End Sub
按下后:
按下“启动进度条”按钮:
'启动进度条 Private Sub Button2_Click(sender As Object,e As EventArgs) Handles Button2.Click For i As Integer = 0 To 100 ToolStripProgressBar1.Value = i Threading.Thread.Sleep(100) Next End Sub按下后:
在窗体编辑器上双击粗体、斜体或者下划线按钮添加Click事件,
使标签上的图片变化,同时使ToolStripDropDownButton1上的图片也跟着变化:
'粗体 Private Sub 粗体ToolStripMenuItem3_Click(sender As Object,e As EventArgs) Handles 粗体ToolStripMenuItem3.Click ToolStripStatusLabel1.Image = 粗体ToolStripMenuItem3.Image ToolStripStatusLabel1.Text = "" ToolStripDropDownButton1.Image = 粗体ToolStripMenuItem3.Image End Sub '斜体 Private Sub 斜体ToolStripMenuItem_Click(sender As Object,e As EventArgs) Handles 斜体ToolStripMenuItem.Click ToolStripStatusLabel1.Image = 斜体ToolStripMenuItem.Image ToolStripStatusLabel1.Text = "" ToolStripDropDownButton1.Image = 斜体ToolStripMenuItem.Image End Sub '下划线 Private Sub 下划线ToolStripMenuItem_Click(sender As Object,e As EventArgs) Handles 下划线ToolStripMenuItem.Click ToolStripStatusLabel1.Image = 下划线ToolStripMenuItem.Image ToolStripStatusLabel1.Text = "" ToolStripDropDownButton1.Image = 下划线ToolStripMenuItem.Image End Sub
运行时如图:
在窗体编辑器上双击左对齐、右对齐或者ToolStripSplitButton1添加Click事件:
'左对齐 Private Sub 左对齐ToolStripMenuItem_Click(sender As Object,e As EventArgs) Handles 左对齐ToolStripMenuItem.Click ToolStripStatusLabel1.Image = Nothing ToolStripStatusLabel1.Text = "这是状态栏说明" ToolStripStatusLabel1.TextAlign = ContentAlignment.MiddleLeft End Sub '右对齐 Private Sub 右对齐ToolStripMenuItem_Click(sender As Object,e As EventArgs) Handles 右对齐ToolStripMenuItem.Click ToolStripStatusLabel1.Image = Nothing ToolStripStatusLabel1.Text = "这是状态栏说明" ToolStripStatusLabel1.TextAlign = ContentAlignment.MiddleRight End Sub '直接按下ToolStripSplitButton1 Private Sub ToolStripSplitButton1_ButtonClick(sender As Object,e As EventArgs) Handles ToolStripSplitButton1.ButtonClick ToolStripStatusLabel1.Image = Nothing ToolStripStatusLabel1.Text = "这是状态栏说明" ToolStripStatusLabel1.TextAlign = ContentAlignment.MiddleCenter End Sub
运行时如图:
需要注意的是,虽然按下左对齐、右对齐后改变了文字对齐方式,但是由于ToolStripStatusLabel1的ImageAlign属性设置为了MiddleCenter
所以按下ToolStripDropDownButton上的粗体、斜体、下划线按钮后,显示的图片的对齐方式不受文字对齐方式的影响。
学习更多vb.net知识,请参看 vb.net 教程 目录
原文链接:https://www.f2er.com/vb/256592.html