Export (0) Print
Expand All

PowerStatus Class

Indicates current system power status information.

System.Object
  System.Windows.Forms.PowerStatus

Namespace:  System.Windows.Forms
Assembly:  System.Windows.Forms (in System.Windows.Forms.dll)

'Declaration
Public Class PowerStatus

The PowerStatus type exposes the following members.

  NameDescription
Public propertyBatteryChargeStatusGets the current battery charge status.
Public propertyBatteryFullLifetimeGets the reported full charge lifetime of the primary battery power source in seconds.
Public propertyBatteryLifePercentGets the approximate amount of full battery charge remaining.
Public propertyBatteryLifeRemainingGets the approximate number of seconds of battery time remaining.
Public propertyPowerLineStatusGets the current system power status.
Top

  NameDescription
Public methodEquals(Object)Determines whether the specified object is equal to the current object. (Inherited from Object.)
Protected methodFinalizeAllows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Public methodGetHashCodeServes as the default hash function. (Inherited from Object.)
Public methodGetTypeGets the Type of the current instance. (Inherited from Object.)
Protected methodMemberwiseCloneCreates a shallow copy of the current Object. (Inherited from Object.)
Public methodToStringReturns a string that represents the current object. (Inherited from Object.)
Top

The PowerStatus class represents information about the current AC line power status, battery charging status, and battery charge status.

This class is used by the PowerStatus property of the SystemInformation class to indicate current system power information.

The following example lists all properties of the PowerStatus class in a ListBox control and displays the current value of the associated property in a TextBox control when a list item selected.

Imports Microsoft.VisualBasic
Imports System
Imports System.Collections
Imports System.ComponentModel
Imports System.Drawing
Imports System.Reflection
Imports System.Windows.Forms

Public Class PowerStatusBrowserForm
    Inherits System.Windows.Forms.Form
    Private listBox1 As System.Windows.Forms.ListBox
    Private textBox1 As System.Windows.Forms.TextBox  

    Public Sub New()
        Me.SuspendLayout()
        InitForm()

        'Add each property of the PowerStatus class to the list box. 
        Dim t As Type = GetType(System.Windows.Forms.PowerStatus)
        Dim pi As PropertyInfo() = t.GetProperties()
        Dim i As Integer 
        For i = 0 To pi.Length - 1
            listBox1.Items.Add(pi(i).Name)
        Next i
        textBox1.Text = "The PowerStatus class has " + pi.Length.ToString() + " properties." + ControlChars.CrLf

        ' Configure the list item selected handler for the list box to invoke a  
        ' method that displays the value of each property.            
        AddHandler listBox1.SelectedIndexChanged, AddressOf listBox1_SelectedIndexChanged
        Me.ResumeLayout(False)
    End Sub 

    Private Sub listBox1_SelectedIndexChanged(sender As Object, e As EventArgs)
        ' Return if no item is selected. 
        If listBox1.SelectedIndex = - 1 Then 
            Return 
        End If  
        ' Get the property name from the list item 
        Dim propname As String = listBox1.Text

        ' Display the value of the selected property of the PowerStatus type. 
        Dim t As Type = GetType(System.Windows.Forms.PowerStatus)
        Dim pi As PropertyInfo() = t.GetProperties()
        Dim prop As PropertyInfo = Nothing 
        Dim i As Integer 
        For i = 0 To pi.Length - 1
            If pi(i).Name = propname Then
                prop = pi(i)
                Exit For 
            End If 
        Next i 
        Dim propval As Object = prop.GetValue(SystemInformation.PowerStatus, Nothing)
        textBox1.Text += ControlChars.CrLf + "The value of the " + propname + " property is: " + propval.ToString()
    End Sub 

    Private Sub InitForm()
        ' Initialize the form settings 
        Me.listBox1 = New System.Windows.Forms.ListBox()
        Me.textBox1 = New System.Windows.Forms.TextBox()
        Me.listBox1.Anchor = CType(System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Left Or System.Windows.Forms.AnchorStyles.Right, System.Windows.Forms.AnchorStyles)
        Me.listBox1.Location = New System.Drawing.Point(8, 16)
        Me.listBox1.Size = New System.Drawing.Size(172, 496)
        Me.listBox1.TabIndex = 0
        Me.textBox1.Anchor = CType(System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Right, System.Windows.Forms.AnchorStyles)
        Me.textBox1.Location = New System.Drawing.Point(188, 16)
        Me.textBox1.Multiline = True 
        Me.textBox1.ScrollBars = System.Windows.Forms.ScrollBars.Vertical
        Me.textBox1.Size = New System.Drawing.Size(420, 496)
        Me.textBox1.TabIndex = 1
        Me.ClientSize = New System.Drawing.Size(616, 525)
        Me.Controls.Add(Me.textBox1)
        Me.Controls.Add(Me.listBox1)
        Me.Text = "Select a PowerStatus property to get the value of" 
    End Sub    

    <STAThread()>  _
    Shared Sub Main()
        Application.Run(New PowerStatusBrowserForm())
    End Sub 

End Class

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Show:
© 2014 Microsoft