Skip to content
David Kittell
David Kittell

Application & System: Development / Integration / Orchestration

  • Services
    • Application Development
    • Online Application Integration
  • Code
  • Online Tools
  • Tech Support
David Kittell

Application & System: Development / Integration / Orchestration

Battery Life

Posted on May 3, 2013February 8, 2016 By David Kittell

BatteryLife

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
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.

Related

Code CSharp VB.NET

Post navigation

Previous post
Next post

Related Posts

Mac OSX Terminal – Convert RAW Photos to JPG

Posted on September 22, 2016

If you’ve ever been stuck with a directory of .DNG or .RAW files this script will quickly convert the images for you. Adobe Products may give a better result but seem to be one at a time still… for i in *.dng; do sips -s format jpeg $i –out "${i%.*}.jpg";…

Read More

Embarcadero Delphi – Battery Indicator/Check

Posted on January 5, 2017

If your application runs on a computer or device that has a battery it’s helpful to know the battery level. Setup your application canvas with 3 labels, 1 Gauge (progress bar) and 1 timer. To keep it simple I’m not changing the names of the elements so you should have:…

Read More

PowerShell – Azure – Create UNIX VM

Posted on April 8, 2016

If you have a need to create a few VMs it can get a bit tedious to build them in one of the various portals. Set the variables then let this script run. At some point I will create a function around this but for now the script works as…

Read More

Code

Top Posts & Pages

  • PowerShell - Rename Pictures to Image Taken
  • Front Page
  • C# - Start/Stop/Restart Services
  • MacPorts / HomeBrew - Rip CD tracks from terminal
  • PowerShell - Show File Extensions

Recent Posts

  • Javascript – Digital Clock with Style
  • BASH – Web Ping Log
  • BASH – Picture / Video File Name Manipulation
  • Mac OSX Terminal – Create SSH Key
  • Bash – Rename Picture

Top Posts

  • PowerShell - Rename Pictures to Image Taken
  • C# - Start/Stop/Restart Services
  • MacPorts / HomeBrew - Rip CD tracks from terminal
  • PowerShell - Show File Extensions
  • Open On Screen Keyboard (OSK)
  • SQLite - Auto-Increment / Auto Generate GUID
©2025 David Kittell | WordPress Theme by SuperbThemes
 

Loading Comments...
 

You must be logged in to post a comment.