Microsoft Developer Network > Forenhomepage > Windows Presentation Foundation (WPF) > Trying to update a progress bar - bad calculation (math)
Stellen Sie eine FrageStellen Sie eine Frage
 

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

  • Mittwoch, 4. November 2009 18:14B. Clay Shannon TeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillen
     

    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
    • Typ geändertWang, JieMSFT, ModeratorDonnerstag, 19. November 2009 10:59To help people with similar question come up with this post easier.
    •  

Antworten

  • Mittwoch, 4. November 2009 20:05wjousts TeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillen
     Beantwortet
    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();
  • Donnerstag, 5. November 2009 11:57Wang, JieMSFT, ModeratorTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillen
     Beantwortet

    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)
  • Donnerstag, 5. November 2009 16:13B. Clay Shannon TeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillen
     Beantwortet
    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

Alle Antworten

  • Mittwoch, 4. November 2009 18:30HomeroThompson TeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillen
     
    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.
  • Mittwoch, 4. November 2009 19:26B. Clay Shannon TeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillen
     
    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
  • Mittwoch, 4. November 2009 20:05wjousts TeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillen
     Beantwortet
    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();
  • Donnerstag, 5. November 2009 11:57Wang, JieMSFT, ModeratorTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillen
     Beantwortet

    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)
  • Donnerstag, 5. November 2009 16:13B. Clay Shannon TeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillen
     Beantwortet
    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