Click here to Skip to main content
15,896,348 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi all,

actually i am a newbie, i think i get wrong when i try to place the if statement, i wish my application send me an alert music when the status of battery more than 90% or less than 20%,

but the result is the application is running well and the music alert is bad to listen.


this my code:

C#
namespace batrei_laptop_rev_2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            timer1.Enabled = true;
        }
        string musik;
        private void timer1_Tick(object sender, EventArgs e)
        {
            PowerStatus status = SystemInformation.PowerStatus;
            progressBarStatus.Value = (int)(status.BatteryLifePercent * 100);
            lblBatterLevel.Text = String.Format("{0}%", (status.BatteryLifePercent * 100));
            if ((status.BatteryLifePercent * 100) > 90)
            {
                StatusText.Text = "SEGERA CABUT CHARGE'NYA";
                axWindowsMediaPlayer1.URL = musik;
            }
            else if ((status.BatteryLifePercent * 100) < 60)
            {
                StatusText.Text = "LAPTOP PERLU DI CHARGE";
                axWindowsMediaPlayer1.URL = musik;
            }
        }
        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog open = new OpenFileDialog();
            if(open.ShowDialog() == DialogResult.OK)
            {
                musik = open.FileName;
                textBox1.Text = open.SafeFileName;
            }
        }
    }
}


somebody help me please....
Posted
Updated 24-Sep-14 19:44pm
v2
Comments
MuhammadUSman1 25-Sep-14 1:58am    
Check Solution 1
I have tested it is working fine for me.

1 solution

C#
namespace batrei_laptop_rev_2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
 
        private void Form1_Load(object sender, EventArgs e)
        {
            timer1.Enabled = true;
        }
        string musik;
        private void timer1_Tick(object sender, EventArgs e)
        {
            PowerStatus status = SystemInformation.PowerStatus;
            progressBarStatus.Value = (int)(status.BatteryLifePercent * 100);
            lblBatterLevel.Text = String.Format("{0}%", (status.BatteryLifePercent * 100));
            if ((status.BatteryLifePercent * 100) > 90)
            {
                StatusText.Text = "SEGERA CABUT CHARGE'NYA";
                axWindowsMediaPlayer1.URL = musik;
            }
            else if ((status.BatteryLifePercent * 100) < 60)
            {
                StatusText.Text = "LAPTOP PERLU DI CHARGE";
                axWindowsMediaPlayer1.URL = musik;
            }
            axWindowsMediaPlayer1.openPlayer(musik);//add this line hope so it will work.

        }
        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog open = new OpenFileDialog();
            if(open.ShowDialog() == DialogResult.OK)
            {
                musik = open.FileName;
                textBox1.Text = open.SafeFileName;
            }
        }
    }
}


if any issue then let me know.

-> M.U
 
Share this answer
 
Comments
mis sutiawan 25-Sep-14 2:14am    
thanks mr. MuhammadUsman1, i have an error when i add that line.
MuhammadUSman1 25-Sep-14 2:42am    
You welcome and Which error?
MuhammadUSman1 25-Sep-14 2:46am    
Please paste your error message.
mis sutiawan 25-Sep-14 3:12am    
NullRefeenceException was unhandled
Object reference not set to an instance of an object.
MuhammadUSman1 25-Sep-14 3:39am    
Please debug and trace on which line you facing error.
This error is because you are using any object which is initialized with null value.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900