vb.net中的statusstrip控件 [英] statusstrip control in vb.net

查看:440
本文介绍了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.


这篇关于vb.net中的statusstrip控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆