Microsoft Developer Network > Página principal de foros > Windows Presentation Foundation (WPF) > Trying to update a progress bar - bad calculation (math)
Formular una preguntaFormular una pregunta
 

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

  • miércoles, 04 de noviembre de 2009 18:14B. Clay Shannon Medallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuario
     

    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
    • Tipo cambiadoWang, JieMSFT, Moderadorjueves, 19 de noviembre de 2009 10:59To help people with similar question come up with this post easier.
    •  

Respuestas

  • miércoles, 04 de noviembre de 2009 20:05wjousts Medallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuario
     Respondida
    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();
  • jueves, 05 de noviembre de 2009 11:57Wang, JieMSFT, ModeradorMedallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuario
     Respondida

    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)
  • jueves, 05 de noviembre de 2009 16:13B. Clay Shannon Medallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuario
     Respondida
    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

Todas las respuestas

  • miércoles, 04 de noviembre de 2009 18:30HomeroThompson Medallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuario
     
    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.
  • miércoles, 04 de noviembre de 2009 19:26B. Clay Shannon Medallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuario
     
    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
  • miércoles, 04 de noviembre de 2009 20:05wjousts Medallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuario
     Respondida
    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();
  • jueves, 05 de noviembre de 2009 11:57Wang, JieMSFT, ModeradorMedallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuario
     Respondida

    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)
  • jueves, 05 de noviembre de 2009 16:13B. Clay Shannon Medallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuario
     Respondida
    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