6
Answers

autocalculation

rashmi kc

rashmi kc

12y
1.7k
1
When i type the value in one text box  automatically it should calculate and display in another textbox

Answers (6)
0
Vulpes
NA 98.3k 1.5m 12y
You'll need to have some way of deciding whether the number selected in the ComboBox should go in the first or the second TextBox. Try this:

        private bool useSecond = false;

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (comboBox1.SelectedIndex == -1) return;
            string selected = comboBox1.SelectedItem.ToString();

            if (!useSecond)
            {
                textBox1.Text = selected;
                textBox2.Text = "";
                textBox3.Text = "";
            }
            else
            {
                int number1 = int.Parse(textBox1.Text);
                int number2 = int.Parse(selected);
                textBox2.Text = number2.ToString();
                textBox3.Text = (number1 * number2).ToString();
            }

            useSecond = !useSecond;
        }
Accepted
0
rashmi kc
NA 113 125.2k 12y
how to make when i select two different number from combobox and this will be displayed in two different textbox, once it displayed  both number should get multiplied and should display in third textbox
0
Vulpes
NA 98.3k 1.5m 12y
In that case you don't need to handle any textbox events at all - you can do everything in the comboBox's SelectedIndexChanged eventhandler and the code is simpler because you know that the selected item will be an integer:

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
    if (comboBox1.SelectedIndex > -1)
    {
       int i = int.Parse(comboBox1.SelectedItem.ToString()); 
       textBox1.Text = i.ToString();
       int j = i * 2; // or whatever
       textBox2.Text = j.ToString();
    }                  
}







0
rashmi kc
NA 113 125.2k 12y
I have a combobox and it contains items like 1,2,3,4,5
when i select the 3 from the combobox that value should display in the textbox1 and its answer display in textbox2



Is this correct??????   
I am selecting the value from combo but not displaying in the textbox1
private void textBox1_Leave(object sender, EventArgs e)
        {
            double d;
            bool isValid = double.TryParse(textBox1.Text, out d);
            if (!isValid)
            {
                MessageBox.Show("Not a valid number. Please amend");
                textBox1.Focus();
                return;
            }
            d = 2 * d; // or any other calculation
            textBox2.Text = d.ToString();
        }
 private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
              this.textBox1.Text = this.comboBox1.Text.ToString();                   
        }
0
Satyapriya Nayak
NA 53k 8m 12y
Hi Rashmi,


Another way


 private void textBox1_TextChanged(object sender, EventArgs e)
        {

            decimal a, b;
            a = Convert.ToDecimal(textBox1.Text);
            b = a  * 2;
            textBox2.Text = b.ToString();
        }


Thanks
0
Vulpes
NA 98.3k 1.5m 12y
Assuming you want to do the calculation after the whole value has been entered in the first textbox (rather than as each character is entered), then I'd handle its Leave event on the following lines:

        private void textBox1_Leave(object sender, EventArgs e)
        {
            double d;
            bool isValid = double.TryParse(textBox1.Text, out d);
            if (!isValid)
            {
                MessageBox.Show("Not a valid number. Please amend");
                textBox1.Focus();
                return;
            }
            d = 2 * d; // or any other calculation
            textBox2.Text = d.ToString();
        }