Compare numeric Textbox with another.

Abe

Member
Joined
Mar 16, 2017
Messages
20
Programming Experience
Beginner
I am working on a game WPF/C#.

So I have 2 text boxes. HP and Currenthp. I also have 10 images of hearts, each image the heart starts to break till it is dead.

HP.text is the max amount of hit points a character has.

Currenthp.text is the current amount of Hit points a character has. With a +/- button to alter the value.

What I want to do is compare HP and Current HP by %.

So if you have 100 HP and are at 90 currenthp, image 1 will show. If you are at 80 currenthp image 2 will show. I know I will need an IF or switch statement, but trying to figure out how to do the math in %. 10% per case. (HP will not always be 100.)

EDIT* So I am looking for current/total = percentage but not sure how to do this with a switch or if statement.

Thanks in advance!
-Abe
 
Last edited:
So I got it half working with the code below. The issue I am having now is the numbers are not lining up. It I set Case 10: I need it to be 10% but it does not seem to do that. It is making images appear just at the wrong percent.

Based on a 100HP Total I need

Case 10: Show image at 10% (It is showing at CurrentHP value 10)
Case 20: Show image at 20% (It is showing at CurrentHP value 5)


C#:
private void curhp_TextChanged(object sender, TextChangedEventArgs e)
        {
            int thp = 100;
            int currenthp = Convert.ToInt32(curhp.Text);
            int totalhp = Convert.ToInt32(thp);
            




            int percenthp = (totalhp / currenthp);






            switch (percenthp)
            {




                case 10:
                    heart1.Visibility = Visibility.Visible;
                    break;
                case 20:
                    heart7.Visibility = Visibility.Visible;
                    break;


            }
 
Fixed it, needed Double and a couple Tweeks.
C#:
double thp = 100;
            double currenthp = Convert.ToInt32(chp.Text);
            double totalhp = Convert.ToInt32(thp);
            double percenthp = (Math.Round((double)((currenthp / totalhp) * (100))));




            switch (percenthp)
 
Back
Top Bottom