By its self this code is rather simple and meaningless but useful when adding to scheduled tasks or applications that take a long time to run and have the potential of running on a laptop.
Before using the code below in Design View you’ll need these:
- 1 Progress Bar
- 2 Labels
- 1 Timer
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Timer1.Enabled = True
Dim psBattery As PowerStatus = SystemInformation.PowerStatus
Dim perFull As Single = psBattery.BatteryLifePercent
If psBattery.PowerLineStatus = PowerLineStatus.Offline Then
Me.Text = "Battery Indicator - " & perFull * 100 & "% Battery Power"
End If
If perFull * 100 < 10 Then
MsgBox("Your battery is running really low you may want to plug in.")
Application.Exit()
End If
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
'Objects for retrieving the information
Dim psBattery As PowerStatus = SystemInformation.PowerStatus
Dim perFull As Single = psBattery.BatteryLifePercent
'Show the remaining battery power in percentages
ProgressBar1.Value = perFull * 100
Label1.Text = "Total battery power remaining: " & perFull * 100 & "%"
Me.Text = "Battery Indicator - " & perFull * 100 & "% Battery Power"
'Is the battery charging?
If psBattery.PowerLineStatus = PowerLineStatus.Online Then
Label2.Text = "AC status: Plugged In / Charging"
ElseIf psBattery.PowerLineStatus = PowerLineStatus.Offline Then
Label2.Text = "AC status: Not Plugged In / Not charging"
End If
End Sub
End Class
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Diagnostics;
using System.Windows.Forms;
namespace BatteryIndicatorC
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
timer1.Enabled = true;
PowerStatus psBattery = SystemInformation.PowerStatus;
float perFull = psBattery.BatteryLifePercent;
if (psBattery.PowerLineStatus == PowerLineStatus.Offline)
{
this.Text = "Battery Indicator - " + perFull * 100 + "% Battery Power";
}
if (perFull * 100 < 10)
{
MessageBox.Show("Your battery is running really low you may want to plug in.");
Application.Exit();
}
}
private void timer1_Tick(object sender, EventArgs e)
{
//Objects for retrieving the information
PowerStatus psBattery = SystemInformation.PowerStatus;
float perFull = psBattery.BatteryLifePercent;
//Show the remaining battery power in percentages
progressBar1.Value = (int)perFull * 100;
label1.Text = "Total battery power remaining: " + perFull * 100 + "%";
this.Text = "Battery Indicator - " + perFull * 100 + "% Battery Power";
//Is the battery charging?
if (psBattery.PowerLineStatus == PowerLineStatus.Online)
{
label2.Text = "AC status: Plugged In / Charging";
}
else if (psBattery.PowerLineStatus == PowerLineStatus.Offline)
{
label2.Text = "AC status: Not Plugged In / Not charging";
}
}
}
}
Originally Posted on May 3, 2013
Last Updated on February 8, 2016
Last Updated on February 8, 2016
All information on this site is shared with the intention to help. Before any source code or program is ran on a production (non-development) system it is suggested you test it and fully understand what it is doing not just what it appears it is doing. I accept no responsibility for any damage you may do with this code.

You must be logged in to post a comment.