-3

I'm completely new to C# programming, and I'm trying to make a custom calculator using Windows Forms.

Three text boxes txtMinSkill, txtMaxSkill, txtCooldown should be keyed values into, and clicking a button buttonCalculate should do some calculations and present the result in a label resultLabel.

I have managed to get everything working down to the skill.Display function, but I have no idea how to display the variables hours, minutes, seconds in the label. When I try to access them from within the button event, I just get a message that it does not exist in the current context. And I can't access the label from the Display method.

Can anyone assist? Thanks!

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void buttonCalculate_Click(object sender, EventArgs e)
    {
        double minSkill;
        double maxSkill;
        double coolDown;
        minSkill = float.Parse(txtMinSkill.Text) * 10;
        maxSkill = float.Parse(txtMaxSkill.Text) * 10;
        coolDown = float.Parse(txtCooldown.Text);
        SkillGainCalculator skill = new SkillGainCalculator();
        skill.IntegerMax(maxSkill);
        skill.RemainderMax(maxSkill);
        skill.RemainderMin(minSkill);
        skill.IntegerMin(minSkill);
        skill.Calculate(minSkill,maxSkill);
        skill.Display(coolDown);
    }

}
class SkillGainCalculator
{
    //member variables
    private int integerMax;
    private int remainderMax;
    private int integerMin;
    private int remainderMin;

    private double counter;
    const int constant = 6480;
    private int i;

    private double totalTime;
    private int hours;
    private int minutes;
    private int seconds;

    public double IntegerMax(double intMax)
    {
        integerMax = (int)((1000 - intMax) / 50);
        return integerMax;
    }

    public int RemainderMax(double intMax)
    {
        remainderMax = (int)((1000 - intMax) % 50);
        return remainderMax;
    }

    public int RemainderMin(double intMin)
    {
        remainderMin = (int)((1000 - intMin) % 50);
        return remainderMin;
    }

    public int IntegerMin(double intMin)
    {
        if (remainderMin == 0)
        {
            integerMin = (int)((1000 - intMin) / 50) - 1;
            return integerMin;
        }
        else
        {
            integerMin = (int)((1000 - intMin) / 50);
            return integerMin;
        }
    }

    public double Calculate(double intMax, double intMin)
    {
        for (i = integerMax; i <= integerMin; i++)
        {
            if (i == integerMax && remainderMax != 0)
            {
                if (intMax <= 700)
                {
                    counter = counter + constant * Math.Pow(0.8, i) * (50 - remainderMax) / 50;
                }
                else
                {
                    counter = counter + constant * Math.Pow(0.8, i) * (50 - remainderMax) / 50;
                }
            }
            else if (i == integerMin && remainderMin != 0)
            {
                if (intMin < 700)
                {
                    counter = counter + constant * Math.Pow(0.8, i) * remainderMin / 50;
                }
                else
                {
                    counter = counter + constant * Math.Pow(0.8, i) * remainderMin / 50;
                }
            }
            else if (i >= 6)
            {
                counter = counter + constant * Math.Pow(0.8, i);
            }
            else
            {
                counter = counter + constant * Math.Pow(0.8, i);
            }
        }
        return counter;
    }

    public void Display(double clD)
    {
        totalTime = counter * clD / 3600;
        hours = (int)(counter * clD / 3600);
        minutes = (int)((totalTime - hours) * 3600 / 60);
        seconds = (int)((totalTime - hours) * 3600 % 60);
    }

}
farosch
  • 210
  • 4
  • 16
  • When you have an error message please include it in its entirety, paraphrasing it can lead to inconsistencies. Also include the line number if you can. – Steve Apr 04 '18 at 20:54
  • Possible duplicate of [How to access Winform textbox control from another class?](https://stackoverflow.com/questions/5646954/how-to-access-winform-textbox-control-from-another-class) – Broots Waymb Apr 04 '18 at 20:58
  • I'll admit that's probably not the best duplicate flag I've ever submitted, but if you search something like "c# access control from another class", you'll get tons of help. – Broots Waymb Apr 04 '18 at 20:59

1 Answers1

0

I have no idea, what your code does and as @Steve already said your question misses some key infos. Nevertheless try changing your two methods Display and buttonCalculate_Click like this:

public string Display(double clD)
{
    totalTime = counter * clD / 3600;
    hours = (int)(counter * clD / 3600);
    minutes = (int)((totalTime - hours) * 3600 / 60);
    seconds = (int)((totalTime - hours) * 3600 % 60);

    return "Hours: " + hours + ", Minutes: " + minutes + ", Seconds: " + seconds;
}

private void buttonCalculate_Click(object sender, EventArgs e)
{
    double minSkill;
    double maxSkill;
    double coolDown;
    minSkill = float.Parse(txtMinSkill.Text) * 10;
    maxSkill = float.Parse(txtMaxSkill.Text) * 10;
    coolDown = float.Parse(txtCooldown.Text);
    SkillGainCalculator skill = new SkillGainCalculator();
    skill.IntegerMax(maxSkill);
    skill.RemainderMax(maxSkill);
    skill.RemainderMin(minSkill);
    skill.IntegerMin(minSkill);
    skill.Calculate(minSkill, maxSkill);
    resultLabel.Text = skill.Display(coolDown);
}

The method Display now generates the string you want to display and returns it when called so you can set resultLabel.Text from you calling method:

enter image description here

farosch
  • 210
  • 4
  • 16