Radoslav Dugas
Radoslav Dugas

Reputation: 7

C# -> How to read and print values form Textbox in realtime

how do I get the values from the TextBox and to immediately display the calculated results in the other TextBoxes?

enter image description here

I want to load values and according to formulas to change in other TextBoxes.

This code does not work like that, or the only thing that is calculated is the average ap, but the result will be displayed only after typing any character into the textbox. All other textboxes are non-rewritable 0 or NaN.

using System;
using System.Drawing;
using System.Windows.Forms;

namespace sustruzenie_frezovanie
{
    /// <summary>
    /// Description of sustruzenie.
    /// </summary>
    public partial class sustruzenie : Form
    {   
        double d = 0;
        double dm = 0;
        double vc = 0;
        double f = 0;
        double n = 0;
        double ap = 0;
        double vf = 0;
        public const double PI = 3.1415926535897931;
        public sustruzenie()
        {
            InitializeComponent();
        }
        void Label1Click(object sender, EventArgs e)
        {
            
        }
        void TextBox1TextChanged(object sender, EventArgs e)
        {
            double.TryParse(textBox1.Text, out d);
            d = double.Parse(textBox1.Text);
        }
        void TextBox2TextChanged(object sender, EventArgs e)
        {
            double.TryParse(textBox2.Text, out dm);
            dm = double.Parse(textBox2.Text);
        }
        void TextBox3TextChanged(object sender, EventArgs e)
        {
            double.TryParse(textBox3.Text, out vc);
            vc = double.Parse(textBox3.Text);
            if(d >=0 && n >= 0)
            {
                VypocetVc();
            }
        }
        void TextBox4TextChanged(object sender, EventArgs e)
        {
            double.TryParse(textBox4.Text, out vf);
            vf = double.Parse(textBox4.Text);
            if(f >=0 && n >= 0)
            {
                VypocetVf();
            }
        }
        void TextBox5TextChanged(object sender, EventArgs e)
        {
            double.TryParse(textBox5.Text, out n);
            n = double.Parse(textBox5.Text);
            if(vc >=0 && d >= 0)
            {
                VypocetN();
            }
        }
        void TextBox6TextChanged(object sender, EventArgs e)
        {
            double.TryParse(textBox6.Text, out f);
            f = double.Parse(textBox6.Text);
            if(vf >=0 && n >= 0)
            {
                VypocetF();
            }
        }
        void TextBox7TextChanged(object sender, EventArgs e)
        {
            double.TryParse(textBox7.Text, out ap);
            ap = double.Parse(textBox7.Text);
            if(d >=0 && dm >= 0)
            {
                VypocetAp();
            }
        }
        void SustruzenieTextChanged(object sender, EventArgs e)
        {
    
        }
        private void VypocetVc()
        {
            textBox3.Text = ((PI * d * n)/100).ToString();
        }
        private void VypocetVf()
        {
            textBox4.Text = (f * n).ToString();
        }
        private void VypocetN()
        {
            textBox5.Text = ((vc * 1000)/(PI * d)).ToString();
        }
        private void VypocetF()
        {
            textBox6.Text = (vf/n).ToString();
        }
        private void VypocetAp()
        {
            textBox7.Text = ((d - dm)/2).ToString();
        }
    }
}

Upvotes: 0

Views: 792

Answers (2)

Salar Askar
Salar Askar

Reputation: 126

Ok, if you wanna do it in realtime then you need TextChanged event associated to your textBoxes.

Let suppose if you want to add up the values from two textBoxes and immediately display the result in the third one. You can do it like,

public partial class Form1 : Form
    {
        double value1 = 0; // variables
        double value2 = 0; // variables

        public Form1()

        {
            InitializeComponent();
        }
// TextChanged associated with textBox1
 private void textBox1_TextChanged(object sender, EventArgs e)
  {
// Use TryParse instead of Prase. TryParse will not read if textBox is empty
      double.TryParse(textBox1.Text, out value1);
           
     if(value1 >=0 && value2 >= 0)
        {
            AddUp();// calling the metod with addition formula
        }
 }
// TextChanged associated with textBox2
 private void textBox2_TextChanged(object sender, EventArgs e)
   {
// Use TryParse instead of Prase. TryParse will not read if textBox is empty
    double.TryParse(textBox2.Text, out value2); 
 
    if (value1 >= 0 && value2 >= 0)
      {
        AddUp();// calling the metod with addition formula
      }
}

   private void AddUp()
        {
            textBox3.Text = (value1+value2).ToString();
        }

I hope this answer addresses your question. Otherwirse write in the comment section.

Upvotes: 1

Nima Owji
Nima Owji

Reputation: 665

you can use text change event for each textbox

Upvotes: 0

Related Questions