Amazinjoey
Amazinjoey

Reputation: 79

Output to textbox and improving the code

So I'm learning C# and just hit the Forms section and it's not going aswell as I hoped and Im feeling as my code isn't properly written. Im also having problems figuring out how to output my calculated varible Im writting in with a button press.

What im asking for : Help with outputing my calculation from CalcFuelConsumptionPerKm() for example too a uneditible textbox by pressing a button

Also thanks for editing and helping out a beginner like me! You guys are awesome!

https://i.sstatic.net/4rL0R.png > how Winform looks

Winform :

    namespace WindowsFormsApplication1
     {
    public partial class Form1 : Form
    {
        double obedomitor, fuel, price , oldvalue;
        Fuel fc;
        public Form1()
        {
            InitializeComponent();
            fc = new Fuel();
        }

        private void GroupBox1_Enter(object sender, EventArgs e)
        {

        }

        private void TextBox1_TextChanged(object sender, EventArgs e)
        {
            if (TextBox1.Text.Trim() != "")
            {
                try
                {
                    obedomitor = Convert.ToDouble(TextBox1.Text.Trim());
                    fc.SetCurrentReading(obedomitor);
                }
                catch
                {
                    MessageBox.Show("Please enter a valid number");
                    TextBox1.Text = "";
                }

            }
        }

        private void TextBox2_TextChanged(object sender, EventArgs e)
        {
            if (TextBox2.Text.Trim() != "")
            {
                try
                {
                    oldvalue = Convert.ToDouble(TextBox1.Text.Trim());
                    fc.setPreviousReading(oldvalue);

                }
                catch
                {
                    MessageBox.Show("Please enter a valid number");
                    TextBox2.Text = "";
                }

            }
        }

        private void Button1_Click(object sender, EventArgs e)
        {

        }

        private void TextBox3_TextChanged(object sender, EventArgs e)
        {
            if (TextBox3.Text.Trim() != "")
            {
                try
                {
                    fuel = Convert.ToDouble(TextBox3.Text.Trim());
                    fc.SetFuelAmount(fuel);
                }
                catch
                {
                    MessageBox.Show("Please enter a valid number");
                    TextBox1.Text = "";
                }

            }
        }

        private void TextBox4_TextChanged(object sender, EventArgs e)
        {
            if (TextBox4.Text.Trim() != "")
            {
                try
                {
                    price = Convert.ToDouble(TextBox4.Text.Trim());
                    fc.setUnitPrice(price);
                }
                catch
                {
                    MessageBox.Show("Please enter a valid number");
                    TextBox1.Text = "";
                }

            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void TextBox5_TextChanged(object sender, EventArgs e)
        {

        }

        private void TextBox7_TextChanged(object sender, EventArgs e)
        {

        }
    }
   }

My other class :

      namespace WindowsFormsApplication1
     {
      class Fuel
    {

        double CurrentCal; 
        double FuelAmount;
        double PreReading;
        double unitPrice;
        double km;
        public  void Fuelcalculator()
        {
            GetPreviousReading();
        }
        public double CalcConsumptionKilometerPerLiter()
        {
            km = CurrentCal - PreReading;
            double litPerKm = FuelAmount / km;
            return litPerKm;
        }
        public double CalcConsumptionPerMetricMile()
        {
            const double kmToMileFactor = 0.621371192;
            double litPerKm = CalcConsumptionKilometerPerLiter();
            double litPerMetricMile = litPerKm / kmToMileFactor;
            return litPerMetricMile;

        }

        public double CalcCostPerKm()
        {
            double cost = (FuelAmount / km) * unitPrice;
            return cost;

        }
        public double CalcFuelConsumptionPerKm()
        {
            double consumption = FuelAmount / km;
            return consumption;

        }
        public double CalcConsumptionKilometerPerSweMil()
        {
            double literPerMil = CalcConsumptionKilometerPerLiter();
            literPerMil = literPerMil*10;
            return literPerMil;

        }
        public double GetCurrentReading()
        {
            return CurrentCal;

        }
        public double GetFuelAmount()
        {
            return FuelAmount;

        }
        public double GetPreviousReading()
        {
            double previous = CurrentCal;
            return previous;

        }
        public double UnitPrice()
        {
            return unitPrice;

        }
        public void SetCurrentReading(double newValue)
        {
            CurrentCal = newValue;
        }
        public void SetFuelAmount(double newValue)
        {
            FuelAmount = newValue;
        }
        public void setPreviousReading(double newValue)
        {
            PreReading = newValue;
        }
        public void setUnitPrice(double newValue)
        {
            unitPrice = newValue;
        }
    }
 }

Upvotes: 1

Views: 98

Answers (1)

BradleyDotNET
BradleyDotNET

Reputation: 61379

If you want everything to update in realtime (which it sounds like you do) I would have a "UpdateResult" method that sets all the "output" text box's text properties:

private void UpdateResult()
{
    TextBox3.Text = fc.CalcConsumptionKilometerPerLiter().ToString();
    //All the others
}

And invoke that once you have validated the user's input in the "TextChanged" events. For example:

    private void TextBox1_TextChanged(object sender, EventArgs e)
    {
        if (TextBox1.Text.Trim() != "")
        {
            try
            {
                obedomitor = Convert.ToDouble(TextBox1.Text.Trim());
                fc.SetCurrentReading(obedomitor);

                UpdateResults();
            }
            catch
            {
                MessageBox.Show("Please enter a valid number");
                TextBox1.Text = "";
            }

        }
    }

A couple quick notes since you are learning:

  • TextBoxX is not a good naming convention. Give your controls descriptive names
  • Since you mentioned it in a comment, double.TryParse is used for converting inputted strings into numbers. It shouldn't be involved in your output
  • Instead of using .Trim != "", you could just use !String.IsWhitespaceOrEmpty. It reads a lot better :)
  • If you expect user error, exceptions are expensive. Use double.TryParse instead of Convert.ToDouble (which really uses double.Parse) so you can have a safer, non-throwing check for user input.

Upvotes: 1

Related Questions