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

  • #768501
    Hi,

    Refer below

    firstbox is txtprice,secondbox is txtquantity and thirdbox is purchaseamount
    txtprice_textchanged event
    {
    int PurchaseAmountRes=Convert.ToInt32(txtprice.Text)*Convert.ToInt32(txtquantity.Text);
    purchaseamount.Text=PurchaseAmountRes.Tostring();
    }
    txtquantity_textchanged event
    {
    int PurchaseAmountRes=Convert.ToInt32(txtprice.Text)*Convert.ToInt32(txtquantity.Text);
    purchaseamount.Text=PurchaseAmountRes.Tostring();
    }



    Hope this will help you

    Regards,
    SonyShiva
    Never lose hope..You never know what tomorrow will bring

  • #768507
    try{
    int firstTxtValue= Convert.ToInt(txtFirst.Text);
    int firstSecondValue= Convert.ToInt(txtSecondText.Text);
    int finalValue= firstTxtValue* firstSecondValue;
    txtFinalText.Text = finalValue.ToString();
    }catch(exception ex){}

    Debasmit Samal

  • #768518
    Hello,

    Sending the solution in three different ways :-

    1. By Keeping Three TextBox Controls and One Button and on Button Click the resultant value will be display.
    2. By Keeping Three TextBox Controls and the calculation event will fired TextChanged event.
    3. By using Javascript.


    1. On Button Click Event
    ------------------------------

    HTML PART

    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
    <title>Untitled Page</title>
    </head>
    <body>
    <form id="formCalculate" runat="server">
    <div>
    <table>
    <tr>
    <td>
    Price :
    </td>
    <td>
    <asp:TextBox ID="txtPrice" runat="server"></asp:TextBox>
    <asp:RequiredFieldValidator ID="rqdPrice" runat="server" ControlToValidate="txtPrice"
    ErrorMessage="Please enter price" ValidationGroup="calculate" Display="None"></asp:RequiredFieldValidator>
    </td>
    </tr>
    <tr>
    <td>
    Quantity :
    </td>
    <td>
    <asp:TextBox ID="txtQuantity" runat="server"></asp:TextBox>
    <asp:RequiredFieldValidator ID="rqdQuantity" runat="server" ErrorMessage="Please enter quantity"
    ValidationGroup="calculate" ControlToValidate="txtQuantity" Display="None"></asp:RequiredFieldValidator>
    </td>
    </tr>
    <tr>
    <td colspan="2">
    <asp:Button ID="btnResult" runat="server" Text="Calculate" OnClick="btnResult_Click" ValidationGroup="calculate" />
    </td>
    </tr>
    <tr>
    <td>
    Purchase Amount :
    </td>
    <td>
    <asp:TextBox ID="txtAmount" runat="server"></asp:TextBox>
    </td>
    </tr>
    <tr>
    <td colspan="2">
    <asp:ValidationSummary ID="ValidationSummary1" runat="server" ShowMessageBox="True"
    ShowSummary="False" ValidationGroup="calculate" />
    </td>
    </tr>
    </table>
    </div>
    </form>
    </body>
    </html>

    Code Behind
    ----------------

    protected void btnResult_Click(object sender, EventArgs e)
    {
    try
    {
    decimal result = Convert.ToDecimal(Convert.ToDecimal(txtPrice.Text) * Convert.ToInt32(txtQuantity.Text));
    txtAmount.Text = Convert.ToString(result);
    }
    catch(Exception ex)
    {
    Page.ClientScript.RegisterStartupScript(this.GetType(), "message", string.Format("alert('Exception occur due to : {0}');", ex.Message), true);
    }

    }


    2. On TextChanged Event
    ----------------------------

    HTML Part
    --------------
    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Copy of Default.aspx.cs" Inherits="_Default" %>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
    <title>Untitled Page</title>
    </head>
    <body>
    <form id="formCalculate" runat="server">
    <div>
    <table>
    <tr>
    <td style="height: 26px">
    Price :
    </td>
    <td style="height: 26px">
    <asp:TextBox ID="txtPrice" runat="server" AutoPostBack="True" OnTextChanged="txtPrice_TextChanged" ValidationGroup="calculate"></asp:TextBox>
    </td>
    </tr>
    <tr>
    <td>
    Quantity :
    </td>
    <td>
    <asp:TextBox ID="txtQuantity" runat="server" AutoPostBack="True" OnTextChanged="txtQuantity_TextChanged" ValidationGroup="calculate"></asp:TextBox>
    </td>
    </tr>
    <tr>
    <td>
    Purchase Amount :
    </td>
    <td>
    <asp:TextBox ID="txtAmount" runat="server"></asp:TextBox>
    </td>
    </tr>
    </table>
    </div>
    </form>
    </body>
    </html>

    Code Behind
    ----------------

    protected void txtQuantity_TextChanged(object sender, EventArgs e)
    {
    try
    {
    if (txtPrice.Text != String.Empty && txtQuantity.Text != string.Empty)
    {
    decimal result = Convert.ToDecimal(Convert.ToDecimal(txtPrice.Text) * Convert.ToInt32(txtQuantity.Text));
    txtAmount.Text = Convert.ToString(result);
    }
    else
    {
    if (txtPrice.Text == String.Empty)
    {
    Page.ClientScript.RegisterStartupScript(this.GetType(), "message", string.Format("alert('Please enter price');"), true);
    }
    if (txtQuantity.Text == String.Empty)
    {
    Page.ClientScript.RegisterStartupScript(this.GetType(), "message", string.Format("alert('Please enter quantity');"), true);
    }
    }
    }
    catch (Exception ex)
    {
    Page.ClientScript.RegisterStartupScript(this.GetType(), "message", string.Format("alert('Exception occur due to : {0}');", ex.Message), true);
    }
    }

    protected void txtPrice_TextChanged(object sender, EventArgs e)
    {
    try
    {
    if (txtPrice.Text != String.Empty && txtQuantity.Text != string.Empty)
    {
    decimal result = Convert.ToDecimal(Convert.ToDecimal(txtPrice.Text) * Convert.ToInt32(txtQuantity.Text));
    txtAmount.Text = Convert.ToString(result);
    }
    else
    {
    if (txtPrice.Text == String.Empty)
    {
    Page.ClientScript.RegisterStartupScript(this.GetType(), "message", string.Format("alert('Please enter price');"), true);
    }
    if (txtQuantity.Text == String.Empty)
    {
    Page.ClientScript.RegisterStartupScript(this.GetType(), "message", string.Format("alert('Please enter quantity');"), true);
    }
    }
    }
    catch (Exception ex)
    {
    Page.ClientScript.RegisterStartupScript(this.GetType(), "message", string.Format("alert('Exception occur due to : {0}');", ex.Message), true);
    }
    }

    3. By Using Javascript
    ------------------------


    HTML Part (With Javascript)
    ------------------------------------
    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
    <title>Untitled Page</title>
    <script type="text/javascript" language="javascript">
    function multiply()
    {
    var price = document.getElementById('txtPrice').value;
    var quantity = document.getElementById('txtQuantity').value;
    if (!isNaN(price * quantity))
    {
    document.getElementById('txtAmount').value = price * quantity;
    }
    else
    {
    alert('Input is in incorrect format');
    }
    }
    </script>
    </head>
    <body>
    <form id="formCalculate" runat="server">
    <div>
    <table>
    <tr>
    <td>
    Price :
    </td>
    <td>
    <input id="txtPrice" type="text" onkeyup="javascript:multiply();" />
    </td>
    </tr>
    <tr>
    <td>
    Quantity :
    </td>
    <td>
    <input id="txtQuantity" type="text" onkeyup="javascript:multiply();" />
    </td>
    </tr>
    <tr>
    <td>
    Purchase Amount :
    </td>
    <td>
    <input id="txtAmount" type="text" />
    </td>
    </tr>
    </table>
    </div>
    </form>
    </body>
    </html>


    If the above code is in case not visible please find the attached notepad file for the same.

    Hope this will solve your problem.

    Thanks

    Help-Code.txt

    Delete Attachment

  • #768521
    just use javascript to multiple two textboxes and show it in third place, see below snippet for more details
    The numbers in textBox1 is (for example) 2.50, and textBox2 is 10

    2.50 is a decimal value and cannot be parsed to int
    you need to use below code

    public void Multiply()
    {
    int a, b;

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

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

    else
    textBox7.Text = "Invalid input";
    }

    Thanks
    Koolprasd2003
    Editor, DotNetSpider MVM
    Microsoft MVP 2014 [ASP.NET/IIS]


  • Sign In to post your comments