I wanted my program to show a simple dialog form when the battery percentage of my laptop reaches 80%. I've used SystemInformation.PowerStatus.BatteryLifePercent
to achieve the same and have used a timer event to monitor the battery percentage changing while charging or discharging and check for the battery to reach 80% charge using the above mentioned method. Below is my code.
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Timer1.Enabled = True
TimerChargeMonitor.Interval = 100
TimerChargeMonitor.Enabled = True
End Sub
Private Sub TimerChargeMonitor_Tick(sender As Object, e As EventArgs) Handles TimerChargeMonitor.Tick
If SystemInformation.PowerStatus.BatteryLifePercent = 0.8 Then
NotifBox.Show()
TimerChargeMonitor.Enabled = False
End If
End Sub
The problem is it doesnt work. The dialog form doesnt show up when the battery percentage reaches 80% or any other number.
Your code needs some adjustments:
SystemInformation.PowerStatus.BatteryLifePercent
returns a single.
It's better to test it with >=
because its value might be slightly different from what you are expecting here.
Then, you have to stop your timer before showing a MessageBox (if that is what NotifBox.Show()
is).
So your code would be:
If SystemInformation.PowerStatus.BatteryLifePercent >= 0.8 Then
TimerChargeMonitor.Stop()
NotifBox.Show()
End If
As a note, the tick interval seems way too low for this application.
Maybe set it to TimerChargeMonitor.Interval = 5000
(I don't know what the other timer is for, here).