vb.net中的statusstrip控件

问题描述:

如何在vb.net 2008中的状态栏中显示时间,日期,大写锁定和numlock状态?

How to show time, date, capslock and numlock state in status bar in vb.net 2008?

请参阅下面的演示以帮助您前进。您从未说过您使用的是哪个版本的dot net,请参阅.Net 3.5及以上版本的tick事件



See the demo below to get you going. You never said which version of dot net you were using, see the tick event for info on .Net 3.5 and above

Public Class Form1

    Public Declare Function GetKeyState Lib "user32" (ByVal nVirtKey As Long) As Integer

    Private Sub Form1_Load(sender As Object, e As System.EventArgs) Handles Me.Load

        'Create a StatusBar
        Dim statusBarMain As New StatusBar

        statusBarMain.Name = "StatusBar"
        statusBarMain.ShowPanels = True

        'Create the panels
        Dim statusBarDate = New StatusBarPanel
        statusBarDate.Name = "StatusBarDate"
        statusBarDate.Text = FormatDateTime(Now(), DateFormat.ShortDate)
        statusBarDate.AutoSize = StatusBarPanelAutoSize.Contents
        statusBarMain.Panels.Add(statusBarDate)

        Dim statusBarTime = New StatusBarPanel
        statusBarTime.Name = "StatusBarTime"
        statusBarTime.Text = FormatDateTime(Now(), DateFormat.LongTime)
        statusBarTime.AutoSize = StatusBarPanelAutoSize.Contents
        statusBarMain.Panels.Add(statusBarTime)

        Dim statusBarCAPS = New StatusBarPanel
        statusBarCAPS.Name = "StatusBarCAPS"

        If GetKeyState(Keys.CapsLock) = 1 Then
            statusBarCAPS.Text = "CAPS ON"
        Else
            statusBarCAPS.Text = "CAPS OFF"
        End If

        statusBarCAPS.AutoSize = StatusBarPanelAutoSize.Contents
        statusBarMain.Panels.Add(statusBarCAPS)

        Dim statusBarNUMS = New StatusBarPanel
        statusBarNUMS.Name = "StatusBarNUMS"

        If GetKeyState(Keys.NumLock) = 1 Then
            statusBarNUMS.Text = "NumLock ON"
        Else
            statusBarNUMS.Text = "NumLock OFF"
        End If

        statusBarNUMS.AutoSize = StatusBarPanelAutoSize.Contents
        statusBarMain.Panels.Add(statusBarNUMS)

        'Add all teh controls to the form
        Me.Controls.Add(statusBarMain)

        'Set up a refresh timer
        Dim timer As New Timer
        timer.Interval = 1000
        timer.Start()
        AddHandler timer.Tick, AddressOf timer_Tick


    End Sub

        Private Sub timer_Tick()

        Dim status As StatusBar = CType(Me.Controls.Find("statusBar", True)(0), StatusBar)

        status.Panels("statusBarDate").Text = FormatDateTime(Now(), DateFormat.ShortDate)
        status.Panels("statusBarTime").Text = FormatDateTime(Now(), DateFormat.LongTime)

        'Rather than set up message listeners etc, are you really going to notice much if the caps lock/nums lock is
        'upto 1 seconds out from true state?? doubt it much. (you decrease the timer tick of course.
        If GetKeyState(Keys.NumLock) = 1 Then
            status.Panels("statusBarNUMS").Text = "NumLock ON"
        Else
            status.Panels("statusBarNUMS").Text = "NumLock OFF"
        End If

        If GetKeyState(Keys.CapsLock) = 1 Then
            status.Panels("statusBarCAPS").Text = "CAPS ON"
        Else
            status.Panels("statusBarCAPS").Text = "CAPS OFF"
        End If

        'NOTE: IN .Net Version 3.5 and above you can use the;
        '        My.Computer.Keyboard.CapsLock
        '        My.Computer.Keyboard.NumLock
        ' to read the status without an unmanaged call

        End Sub
End Class


创建各个状态条项目,然后通过代码将文本和值分配给这些项目。
Create the individual status strip items and then assign the text and values through code to these items.