StatusStrip

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

Rate: /

/

Solution 2

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

HideExpandCopy Code

PublicClass Form1

PublicDeclareFunctionGetKeyStateLib"user32" (ByValnVirtKeyAsLong) AsInteger

PrivateSub Form1_Load(sender AsObject, e AsSystem.EventArgs) HandlesMe.Load

'Create a StatusBar

DimstatusBarMainAsNewStatusBar

statusBarMain.Name = "StatusBar"

statusBarMain.ShowPanels = True

'Create the panels

DimstatusBarDate = NewStatusBarPanel

statusBarDate.Name = "StatusBarDate"

statusBarDate.Text = FormatDateTime(Now(), DateFormat.ShortDate)

statusBarDate.AutoSize = StatusBarPanelAutoSize.Contents

statusBarMain.Panels.Add(statusBarDate)

DimstatusBarTime = NewStatusBarPanel

statusBarTime.Name = "StatusBarTime"

statusBarTime.Text = FormatDateTime(Now(), DateFormat.LongTime)

statusBarTime.AutoSize = StatusBarPanelAutoSize.Contents

statusBarMain.Panels.Add(statusBarTime)

DimstatusBarCAPS = NewStatusBarPanel

statusBarCAPS.Name = "StatusBarCAPS"

IfGetKeyState(Keys.CapsLock) = 1Then

statusBarCAPS.Text = "CAPS ON"

Else

statusBarCAPS.Text = "CAPS OFF"

EndIf

statusBarCAPS.AutoSize = StatusBarPanelAutoSize.Contents

statusBarMain.Panels.Add(statusBarCAPS)

DimstatusBarNUMS = NewStatusBarPanel

statusBarNUMS.Name = "StatusBarNUMS"

IfGetKeyState(Keys.NumLock) = 1Then

statusBarNUMS.Text = "NumLock ON"

Else

statusBarNUMS.Text = "NumLock OFF"

EndIf

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 AsNew Timer

timer.Interval = 1000

timer.Start()

AddHandlertimer.Tick, AddressOftimer_Tick

EndSub

PrivateSubtimer_Tick()

Dim status AsStatusBar = 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.

IfGetKeyState(Keys.NumLock) = 1Then

status.Panels("statusBarNUMS").Text = "NumLock ON"

Else

status.Panels("statusBarNUMS").Text = "NumLock OFF"

EndIf

IfGetKeyState(Keys.CapsLock) = 1Then

status.Panels("statusBarCAPS").Text = "CAPS ON"

Else

status.Panels("statusBarCAPS").Text = "CAPS OFF"

EndIf

'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

EndSub

EndClass