반응형

* VBNET 노트북 배터리 정보 예제...

 

Main

 

- 사용한 컨트롤 : Button 2개, TextBox 2개, Label 3개, 프로그래스바 1개, Timer 1개

 

전체 소스 코드

Form1.vb

 

Public Class Form1

    Protected Overrides Sub OnLoad(ByVal e As System.EventArgs)
        MyBase.OnLoad(e)

        timer1.Interval = 1000 '1초마다...
    End Sub

    Protected Overrides Sub OnClosed(ByVal e As System.EventArgs)
        MyBase.OnClosed(e)
        timer1.Stop()
    End Sub


    Private Sub button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button1.Click
        timer1.Start()
    End Sub

    Private Sub button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button2.Click
        timer1.Stop()
    End Sub

    Private Sub timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles timer1.Tick
        Dim psStatus As PowerStatus = SystemInformation.PowerStatus

        '충전 상태
        txtChargeStatus.Text = psStatus.BatteryChargeStatus.ToString()

        '전원 상태
        txtPoerStatus.Text = psStatus.PowerLineStatus.ToString()

        '충전 비율
        If psStatus.BatteryLifePercent <> 255 Then
            pbCharge.Value = Convert.ToInt32(psStatus.BatteryLifePercent * 100)
        Else
            pbCharge.Value = 0
        End If

        '잔여 사용 시간
        If psStatus.BatteryLifeRemaining <> -1 Then
            textBox1.Text = TimeSpan.FromSeconds(psStatus.BatteryLifeRemaining).ToString()
        Else
            textBox1.Text = "-------"
        End If
        '완충시 사용 시간
        If psStatus.BatteryFullLifetime <> -1 Then
            textBox2.Text = psStatus.BatteryFullLifetime.ToString()
        Else
            textBox2.Text = "-------"
        End If


    End Sub

End Class

 

 

*예제 결과

 

 

반응형

+ Recent posts