Adding equation formula to textbox.

Joined
Nov 8, 2020
Messages
8
Programming Experience
Beginner
Good evening all,

First of all, thank you in advance for taking the time to read my request. I am really struggling with the following issue.

I have created a windows form using c#. The form consists of a number of text boxes that my users will insert numbers into. Eg: -200, 100 and so on.
TextBox9 is a total column. I need to take TextBox1 value and do an equation and the answer be in TextBox9. For example, the user puts -200 into TextBox1 and I want to do -200(/ 1000 * 3.18141 * 2.3) * 1.19 and the answer be in the Textbox9.
In VBA its so simple, but this seems to be challenging my skill set.

Thank you so much for your help.

Best regards,
 
Show us your current code. We can't advise you on what is wrong if we can't see your code.

You would do the same thing you did in VBA: take the value from textbox1, parse into a numeric value, compute the output value, convert the output value into a string, and then finally store that string in textbox9.
 
Show us your current code. We can't advise you on what is wrong if we can't see your code.

You would do the same thing you did in VBA: take the value from textbox1, parse into a numeric value, compute the output value, convert the output value into a string, and then finally store that string in textbox9.
Hey Skydiver, thank you for getting back to me. My apologies for not providing the code, I have done below. LOL, you make it sound so simple, but for life of me, I cannot seem to do it.
Just to confirm, textBox8 needs to have the value from textBox5 \ (1000 * 3.18141 * 2.3) * 1.19

As always, I appreciate your time and expertise.

Best regards,

C#:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using Excel = Microsoft.Office.Interop.Excel;
using Microsoft.Office.Interop.Excel;
using System.Runtime.ExceptionServices;

namespace Navigations_delivery_report
{
    public partial class Form1 : Form
    {
        Excel.Application xlap = new Excel.Application();
        public Form1()
        {
            InitializeComponent();
            textBox1.Text = Environment.UserName;
        }
        private void label1_Click(object sender, EventArgs e)
        {

        }
        private void label5_Click(object sender, EventArgs e)
        {   }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {  }

        private void button1_Click(object sender, EventArgs e)

        {
            string result = "Please Enter ";
            
            if (textBox1.Text == string.Empty)
            {
                result += " Name,";
            }
            
            if (textBox2.Text == string.Empty)
            {
                result += " CallSign,";
            }
            
            if (textBox3.Text == string.Empty)
            {
                result += " Departure,";
            }
            
            if (textBox4.Text == string.Empty)
            {
                result += " Arrival,";
            }

            if (textBox5.Text == string.Empty)
            {
                result += " Fuel,";
            }

            if (textBox6.Text == string.Empty)
            {
                result += " Cost,";
            }

            if (textBox7.Text == string.Empty)
            {
                result += " Time,";
            }

            if (comboBox1.SelectedItem == null)
            {
                result += " Type Of Route,";
            }

            if (comboBox2.SelectedItem == null)
            {
                result += " Route Method,";
            }

            if (checkBox1.Checked == false)
            {
                result += " Confirm OFP Matches ATC Plan,";
            }
            else
            {
                if (result.Contains(','))
                {
                    result = result.Remove(result.LastIndexOf(','));
                }
            }
            
            if (result != "Please Enter ")
            {
                MessageBox.Show(result);
            }
            else
            {
                Excel.Workbook xlworkbook;
                Excel.Worksheet xlworksheet;
                
                object misValue = System.Reflection.Missing.Value;
                
                xlworkbook = xlap.Workbooks.Add(misValue);
                xlworksheet = (Excel.Worksheet)xlworkbook.Worksheets.get_Item(1);
                
                xlworksheet.Cells[1, 1] = textBox1.Text;
                xlworksheet.Cells[1, 2] = dateTimePicker1.Value;
                xlworksheet.Cells[1, 3] = textBox2.Text;
                xlworksheet.Cells[1, 4] = textBox3.Text;
                xlworksheet.Cells[1, 5] = textBox4.Text;
                xlworksheet.Cells[1, 6] = textBox5.Text;
                xlworksheet.Cells[1, 7] = textBox6.Text;
                xlworksheet.Cells[1, 8] = textBox7.Text;
                xlworksheet.Cells[1, 9] = textBox8.Text;
                xlworksheet.Cells[1, 10] = textBox9.Text;
                xlworksheet.Cells[1, 11] = comboBox1.Text;
                xlworksheet.Cells[1, 12] = comboBox2.Text;

                xlworkbook.SaveAs(@"C:\demo\savings_" + DateTime.Now.ToString("yyyyMMdd_hmmss") + ".csv");
                xlworkbook.Close(true);
                xlap.Quit();

                textBox2.Clear();
                textBox3.Clear();
                textBox4.Clear();
                textBox5.Clear();
                textBox6.Clear();
                textBox7.Clear();
                comboBox1.SelectedIndex = -1;
                comboBox2.SelectedIndex = -1;
                checkBox1.Checked = false;
                textBox2.Focus();
            }
        }

        private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void button2_Click(object sender, EventArgs e)
        {
            Close();
        }

        public void Button3_Click(object sender, EventArgs e)
        {
            textBox2.Clear();
            textBox3.Clear();
            textBox4.Clear();
            textBox5.Clear();
            textBox6.Clear();
            textBox7.Clear();
            comboBox1.SelectedIndex = -1;
            comboBox2.SelectedIndex = -1;
            checkBox1.Checked = false;
            textBox2.Focus();
        }

        private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
        {
            dateTimePicker1.CustomFormat = "yyyy-mm-dd";
        }

        private void textBox8_TextChanged(object sender, EventArgs e)
        {

        }

        private void textBox9_TextChanged(object sender, EventArgs e)
        {


        }

        private void button4_Click(object sender, EventArgs e)
        {


        }

        private void textBox6_TextChanged(object sender, EventArgs e)
        {

        }

        private void textBox5_TextChanged(object sender, EventArgs e)
        {

        }
    }
}
 
Last edited by a moderator:
Please avoid indiscriminate code dumps. Post ONLY the relevant code. You have numerous empty methods in there so we obviously don't need to see them. We don't need to see all the rest of the code either; only what's specifically relevant to the issue at hand. If you don't know what's relevant then you haven't really considered the problem enough yet.
 
Please avoid indiscriminate code dumps. Post ONLY the relevant code. You have numerous empty methods in there so we obviously don't need to see them. We don't need to see all the rest of the code either; only what's specifically relevant to the issue at hand. If you don't know what's relevant then you haven't really considered the problem enough yet.
Hi,

Thanks for the tip, I will make sure to post only relevant info from now on. That being said, I know what the problem is, I am simply finding it difficult to resolve.
Best regards
 
What do you see as the problem?
 
What do you see as the problem?
Hi Skydiver,

I have the code working here, but only in a MessageBox. I dont know how to code it so it populates in TextBox8 when the user puts the number in Textbox 5.
I have created a variable here, but textBox8.Text(answer.ToString()); does not work.
Thats pretty much it now.

Thank you so much...
 

Attachments

  • 1604944135071.png
    1604944135071.png
    9.8 KB · Views: 56
In the future please post your code as text in code tags, not as a screenshot. Screenshots are hard to read on a small screen device like a phone.
 
Anyway, you assign values to the Text property of TextBox. The error you would have gotten would have told you that Text is a property, not a method, so you can't try to set the value by trying to call it like a method.

One big aside. Why are you using the Excel object model in some of your code above? Although your specific question is about how to compute a value and display it, I can't shake the feeling that you are going about things the hard way after seeing some of that object model code. What is your bigger objective with your code?
 
Anyway, you assign values to the Text property of TextBox. The error you would have gotten would have told you that Text is a property, not a method, so you can't try to set the value by trying to call it like a method.

One big aside. Why are you using the Excel object model in some of your code above? Although your specific question is about how to compute a value and display it, I can't shake the feeling that you are going about things the hard way after seeing some of that object model code. What is your bigger objective with your code?
Hey,

Thank you for getting back to me. This is my first attempt at C# and a majority of the code is from the standard setup after creating the project. So any comments or feedback you provide is very welcomed..
I have actually managed to solve the problem I had by doing the following..
This works as required however, if the user puts in a negative value such as -200, then it does not read it. I am guessing double or int do not allow character. This is my next obstacle, if thats something you are able to explain briefly?
If you are interested in what I am doing, I work in the airline industry and this calculation is the carbon offset charge per kilo of fuel in Euros..

Once again, thank you so much for you're help.
double firstbox;
double answer;

firstbox = double.Parse(textBox5.Text);
answer = firstbox / 1000 * 3.18141 * 2.3 * 1.19;


textBox8.Text = (answer.ToString());
 
Parse() for int and double should be able to handle negative numbers.

What do mean exactly by "it does not read it"?

Are you getting an exception? If so what does it say?
 

Latest posts

Back
Top Bottom