.NET Framework Developer Center > .NET Development Forums > Windows Presentation Foundation (WPF) > Trying to update a progress bar - bad calculation (math)
Ask a questionAsk a question
 

AnswerTrying to update a progress bar - bad calculation (math)

  • Wednesday, November 04, 2009 6:14 PMB. Clay Shannon Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    Something's wrong with my math: I'm trying to update a ProgressBar, but the math I'm using is not right:

    double gdValPerAnswer = 0.0;
    . . .
    gdValPerAnswer = GetCountOfQuestions() * 0.001;

    ...I know it's wrong, but I don't know what the calculation should be...?


    Writer / Photographer - http://www.feedbooks.com/userbook/3631
    • Changed TypeWang, JieMSFT, ModeratorThursday, November 19, 2009 10:59 AMTo help people with similar question come up with this post easier.
    •  

Answers

  • Wednesday, November 04, 2009 8:05 PMwjousts Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    I tried:

    double dValPerAnswer = 0.0;
    . . .
    dValPerAnswer = 100 / GetCountOfQuestions();

    ...and the value of gdValPerAnswer is 0 (zero)...?
    Writer / Photographer - http://www.feedbooks.com/userbook/3631

    I'm not following exactly what you are trying to do (where's gdValPerAnswer in your last post?) but, assuming GetCountOfQuestions returns an integer, then you are doing integer division. If 100 < GetCountOfQuestions() then the value will be truncated to 0.

    Try this:

    dValPerAnswer = 100.0 / (double)GetCountOfQuestions();
  • Thursday, November 05, 2009 11:57 AMWang, JieMSFT, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer

    Hi Clay,

    Or you can just set the Minimum & Maximum properties before setting the Value property, that will define the possible value range and then you just set the value and don't need to calculate the percentage yourself.

    Regards,
    Jie
    MSDN Subscriber Support in Forum
    If you have any feedback on our support, please contact msdnmg@microsoft.com


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    If you have any feedback, please tell us.

    The CodeFx Project
    My Blog (in Simplified Chinese)
  • Thursday, November 05, 2009 4:13 PMB. Clay Shannon Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    Or you can just set the Minimum & Maximum properties before setting the Value property, that will define the possible value range and then you just set the value and don't need to calculate the percentage yourself.

    Aha!

    This was the best idea. All I had to do was:

          giNumberOfQuestions = GetCountOfQuestions();
          . . .
          progBar.Minimum = 0;
          progBar.Maximum = giNumberOfQuestions;

          . . .
          //After correct answer provided:
          progBar.Value = progBar.Value + 1;


    Writer / Photographer - http://www.feedbooks.com/userbook/3631

All Replies

  • Wednesday, November 04, 2009 6:30 PMHomeroThompson Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Hello,

    if CountOfQuestions is the total count of questions to answer and the gdValPerAnswer is the count of questions answered, then:

    percentage = gdValPerAnswer * 100 /CountOfQuestions;

    Good Luck.
  • Wednesday, November 04, 2009 7:26 PMB. Clay Shannon Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    I tried:

    double dValPerAnswer = 0.0;
    . . .
    dValPerAnswer = 100 / GetCountOfQuestions();

    ...and the value of gdValPerAnswer is 0 (zero)...?
    Writer / Photographer - http://www.feedbooks.com/userbook/3631
  • Wednesday, November 04, 2009 8:05 PMwjousts Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    I tried:

    double dValPerAnswer = 0.0;
    . . .
    dValPerAnswer = 100 / GetCountOfQuestions();

    ...and the value of gdValPerAnswer is 0 (zero)...?
    Writer / Photographer - http://www.feedbooks.com/userbook/3631

    I'm not following exactly what you are trying to do (where's gdValPerAnswer in your last post?) but, assuming GetCountOfQuestions returns an integer, then you are doing integer division. If 100 < GetCountOfQuestions() then the value will be truncated to 0.

    Try this:

    dValPerAnswer = 100.0 / (double)GetCountOfQuestions();
  • Thursday, November 05, 2009 11:57 AMWang, JieMSFT, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer

    Hi Clay,

    Or you can just set the Minimum & Maximum properties before setting the Value property, that will define the possible value range and then you just set the value and don't need to calculate the percentage yourself.

    Regards,
    Jie
    MSDN Subscriber Support in Forum
    If you have any feedback on our support, please contact msdnmg@microsoft.com


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    If you have any feedback, please tell us.

    The CodeFx Project
    My Blog (in Simplified Chinese)
  • Thursday, November 05, 2009 4:13 PMB. Clay Shannon Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    Or you can just set the Minimum & Maximum properties before setting the Value property, that will define the possible value range and then you just set the value and don't need to calculate the percentage yourself.

    Aha!

    This was the best idea. All I had to do was:

          giNumberOfQuestions = GetCountOfQuestions();
          . . .
          progBar.Minimum = 0;
          progBar.Maximum = giNumberOfQuestions;

          . . .
          //After correct answer provided:
          progBar.Value = progBar.Value + 1;


    Writer / Photographer - http://www.feedbooks.com/userbook/3631