murderface murderface - 26 days ago 14
C# Question

How to change the value of a progress bar to a double?

I am attempting to have a sound play at specific increments of a progress bar's progress. The progress bar has a max of 80 and is controlled by a timer that has an interval of 100 (thus total duration of 8 seconds). In the first case the sound plays because 80/16 = 5 which is an integer, but nothing else plays. How can I convert the progressbar value to a double? Or is that even the right track? Thank you!

if (checkBox1.Checked && progressBar1.Value == (progressBar1.Maximum * (1/16)))
hiphopkick.Play();
if (checkBox1.Checked && progressBar1.Value == (progressBar1.Maximum * (2 / 16)))
hiphopkick.Play();
if (checkBox1.Checked && progressBar1.Value == (progressBar1.Maximum * (3 / 16)))
hiphopkick.Play();
if (checkBox1.Checked && progressBar1.Value == (progressBar1.Maximum * (4 / 16)))
hiphopkick.Play();
if (checkBox1.Checked && progressBar1.Value == (progressBar1.Maximum * (5 / 16)))
hiphopkick.Play();
if (checkBox1.Checked && progressBar1.Value == (progressBar1.Maximum * (6 / 16)))
hiphopkick.Play();
if (checkBox1.Checked && progressBar1.Value == (progressBar1.Maximum * (7 / 16)))
hiphopkick.Play();
if (checkBox1.Checked && progressBar1.Value == (progressBar1.Maximum * (8 / 16)))
hiphopkick.Play();
if (checkBox1.Checked && progressBar1.Value == (progressBar1.Maximum * (9 / 16)))
hiphopkick.Play();
if (checkBox1.Checked && progressBar1.Value == (progressBar1.Maximum * (10 / 16)))
hiphopkick.Play();
if (checkBox1.Checked && progressBar1.Value == (progressBar1.Maximum * (11 / 16)))
hiphopkick.Play();
if (checkBox1.Checked && progressBar1.Value == (progressBar1.Maximum * (12 / 16)))
hiphopkick.Play();
if (checkBox1.Checked && progressBar1.Value == (progressBar1.Maximum * (13 / 16)))
hiphopkick.Play();
if (checkBox1.Checked && progressBar1.Value == (progressBar1.Maximum * (14 / 16)))
hiphopkick.Play();
if (checkBox1.Checked && progressBar1.Value == (progressBar1.Maximum * (15 / 16)))
hiphopkick.Play();
if (checkBox1.Checked && progressBar1.Value == (progressBar1.Maximum * (16 / 16)))
hiphopkick.Play();

Answer

First use decimal, not double.

Binary floating point and .NET

Second when you divide two ints the result is int. So 12/16 = 0. To make it work correctly you should write it like this:

var decimalRes = progressBar1.Maximum * (13 / (decimal)16);
Comments