Will
Will

Reputation: 33

how to add numbers in various textboxs and the sum displayed in a label

Am trying to add different numbers from different textbox, in a way that once the values are entered into each textbox, the summed value is displayed in a label showing the total value. eg. textbox1 + textbox 2 values to be displayed in label1. in C#. pls how do i go about this? thanks

Upvotes: 2

Views: 4979

Answers (2)

Adam S
Adam S

Reputation: 3125

Here's a very simplistic approach using two text boxes. Assuming you have the event handler hooked up properly, this should work for you. There's more elegant approaches for sure, but this should get you started.

private void textBox1_TextChanged(object sender, TextChangedEventArgs e)
{
    SumAndDisplay();
}

private void textBox2_TextChanged(object sender, TextChangedEventArgs e)
{
    SumAndDisplay();
}

private void SumAndDisplay()
{
    int a;
    int b;
    if (Int32.TryParse(textBox1.Text, out a) && Int32.TryParse(textBox2.Text, out b))
    {
        label1.Content = (a + b).ToString();
    }
}

Upvotes: 2

Thit Lwin Oo
Thit Lwin Oo

Reputation: 3448

Here you go.. plese check. Please note, you can also use jQuery for dom elements accessing process.

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="JavaScript.aspx.cs" Inherits="WebTest.JavaScript" %>

<!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></title>

    <script language="javascript" type="text/javascript">

        function CalculateTotal() {
            var total = 0;

            for (var e = 0; e < form1.elements.length; e++) {
                var el = form1.elements[e];
                if (el.type == 'text') {
                    if (el.value != "")
                        total += parseInt(el.value);
                }
            }

            document.getElementById("lb").innerText = "Total is: " + total;
        }

        function ValidChar(e, objSrc, validchars) {
            var key, keychar;
            key = window.event.keyCode;
            if (key == null) return true;

            keychar = String.fromCharCode(key);
            keychar = keychar.toLowerCase();
            validchars = validchars.toLowerCase();

            if (validchars.indexOf(keychar) != -1)
                return true;

            if (key == null || key == 0 || key == 8 || key == 9 || key == 13 || key == 27)
                return true;

            return false;
        }

    </script>

</head>
<body>
    <form id="form1" runat="server">
    <table border="0" cellpadding="0" cellspacing="0">
        <tr>
            <td>
                <asp:TextBox ID="txt01" runat="server" onfocusout="CalculateTotal();" onkeypress="return ValidChar(event,this,'0123456789')"></asp:TextBox>
            </td>
        </tr>
         <tr>
            <td>
                <asp:TextBox ID="txt02" runat="server" onfocusout="CalculateTotal();" onkeypress="return ValidChar(event,this,'0123456789')"></asp:TextBox>
            </td>
        </tr>
         <tr>
            <td>
                <asp:TextBox ID="txt03" runat="server" onfocusout="CalculateTotal();" onkeypress="return ValidChar(event,this,'0123456789')"></asp:TextBox>
            </td>
        </tr>
         <tr>
            <td>
                <asp:TextBox ID="txt04" runat="server" onfocusout="CalculateTotal();" onkeypress="return ValidChar(event,this,'0123456789')"></asp:TextBox>
            </td>
        </tr>
         <tr>
            <td>
                <asp:Label ID="lb" runat="server"></asp:Label>
            </td>
        </tr>
    </table>
    </form>
</body>
</html>

Upvotes: 0

Related Questions