Search code examples
c#textboxmultiplication

Multiplying two boxes while typing the 2nd operand


Guys im currently making a simple Ordering form, i'm almost done i just need to validate some things, i want to make my Price and Qty multiply after i enter/change the value on Qty without pressing any buttons

for example Price = 10 Qty = Total = 10 if i put a 2 the Total should automatically change to 20 without pressing any buttons

private void btnAddOrder_Click(object sender, EventArgs e)
    {
        //Get the txtbox then add to dgv
        int qty = int.Parse(txtPrice.Text);
        int price = int.Parse(txtQty.Text);
        txtTotal.Text = (qty * price).ToString();
        dgvOrder.Rows.Add(txtItemCode.Text, txtDescription.Text,               txtPrice.Text, txtQty.Text,txtTotal.Text);

    }

right now that's my code for those 3 txtbox


Solution

  • You can implement TextBox.TextChanged Event for Quantity textbox.

    In your Constructor:

    MyTextBox.TextChanged += new TextChangedEventHandler( TextChanged );
    

    And Then this Method:

    private void TextChanged(object Sender, TextChangedEventArgs e)
    {
        //Do your stuff here
    }