You must Sign In to post a response.
  • Category: ASP.NET

    Multiply two textboxes result in third textbox

    firstbox is txtprice,secondbox is txtquantity and thirdbox is purchaseamount
  • #767988
    Hi Hanish,

    This requirement we can simply do with the basic codings.
    You have not specified with language or technology you want this. But I will post all the languages,

    C#
    ----
     
    public void Multiply()
    {
    int a, b;

    bool isAValid = int.TryParse(textBox1.Text, out a); // First Text Box
    bool isBValid = int.TryParse(textBox2.Text, out b);// Second Text Box

    if (isAValid && isBValid)
    textBox3.Text = (a * b).ToString(); // Third Text Box

    else
    textBox7.Text = "Invalid input";
    }

    Thanks,
    Mani

  • #767989

    Hi Refer the below code.

    namespace aspdemos
    {
    public partial class multiplytextbox : System.Web.UI.Page
    {
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    public void calculate()
    {
    int amount =Convert.ToInt32(TextBox1.Text) * Convert.ToInt32(TextBox2.Text);
    TextBox3.Text =(amount).ToString();
    }
    protected void TextBox1_TextChanged(object sender, EventArgs e)
    {
    try
    {
    calculate();
    }
    catch (Exception ex)
    {
    Response.Write("<script type = 'text/javascript'>alert('pls enter quantity');</script>");
    }

    }

    protected void TextBox2_TextChanged(object sender, EventArgs e)
    {
    try
    {
    calculate();
    }
    catch (Exception ex)
    {
    Response.Write("<script type = 'text/javascript'>alert('pls enter price');</script>");
    }

    }
    }
    }



    Sridhar Thota.
    Editor: DNS Forum.

    Delete Attachment

  • #768001
    Hey Hanish,

    Here i found the shortest code for Multiply The values.
    here you follow,

    private void textBox7_TextChanged(object sender, EventArgs e)
    {

    Int32 val1 = Convert.ToInt32(textBox10.Text);
    Int32 val2 = Convert.ToInt32(textBox8.Text);
    Int32 val3 = val1 * val2;
    textBox7.Text = val3.ToString();

    }

    Hope It Helps,


    Thanks,
    Vaibhav Shah

  • #768004
    hai sridhar
    this is working but small error
    The variable 'exception' is declared but never used and result not shown purcase amount

  • #768013
    Hi Hanish.

    Its not an error, it is warning message it wont stop the application from running. You can even write with out exception variable like below.

    protected void TextBox2_TextChanged(object sender, EventArgs e)
    {
    try
    {
    calculate();
    }
    catch (Exception )// no "ex" variable is used here. It will catch any exception occurred in calling calculate method.
    {
    Response.Write("<script type = 'text/javascript'>alert('please enter both quantity and price');</script>");
    }

    }

    In both alert messages you can specify please enter both quantity and price.
    Still If you face any issue let me know.

    Sridhar Thota.
    Editor: DNS Forum.

  • #768017
    hai sridhar

    i send my source code let me check it.

  • #768416
    Try this

    protected void price_TextChanged(object sender, EventArgs e)
    {
    int amount =Convert.ToInt32(price.Text) * Convert.ToInt32(quantity.Text);
    Total.Text =amount.ToString();
    }

    protected void quantity_TextChanged(object sender, EventArgs e)
    {
    int amount =Convert.ToInt32(price.Text) * Convert.ToInt32(quantity.Text);
    Total.Text =amount.ToString();
    }

    Software Developer
    iFour Technolab Pvt. Ltd.

  • #768425
    You can use int.TryParse to look for valid, numerical input in your test box for better results. Example
     private void textBox8_TextChanged(object sender, EventArgs e)
    {
    Multi();
    }

    private void textBox10_TextChanged(object sender, EventArgs e)
    {
    Multi();
    }

    public void Multi()
    {
    int a, b;

    bool isAValid = int.TryParse(textBox8.Text, out a);
    bool isBValid = int.TryParse(textBox10.Text, out b);

    if (isAValid && isBValid)
    textBox7.Text = (a * b).ToString();

    else
    textBox7.Text = "Invalid input";
    }

    Reference : http://stackoverflow.com/questions/22582983/multiply-2-textbox-values-and-display-in-3rd-textbox
    https://www.codeproject.com/questions/385715/how-to-multiply-value-of-two-textboxes-into-the-th


Sign In to post your comments