Click here to Skip to main content
15,891,762 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Here i have 10 textboxes.when i enter the amount in textboxes with out any button clicking the sum of textboxes value will be display in Totaltextbox. my code is
C#
protected void txtrent_TextChanged(object sender, EventArgs e)
{
    if (!string.IsNullOrEmpty(txtrent.Text) && !string.IsNullOrEmpty(txtmain.Text))
        txttotal.Text = (Convert.ToInt32(txtrent.Text) +       Convert.ToInt32(txtmain.Text)).ToString();
}

This is for two textboxes only.when i write like this the code will be huge.so pls help me how to do this with simple code
Posted
Updated 28-Dec-14 22:24pm
v2
Comments
Maciej Los 29-Dec-14 4:32am    
What framework: WinForms, WebControls, WPF?
It's important!
benzimen 29-Dec-14 4:36am    
its is webform and using asp textbox control
BillWoodruff 29-Dec-14 15:53pm    
If this is a WebForm project you should be using the WebForm RequiredValidator object with your TextBoxes. That gives you everything you need to make sure you have valid user input before you calculate anything.

There's a good tutorial here:

http://msdn.microsoft.com/en-us/library/vstudio/a0z2h4sw%28v=vs.100%29.aspx

You can use a foreach loop to iterate through all the controls on your form and select the contents of the textboxes, accumulating the values as you go. However, you should not use Convert.ToInt32 on text fields because you cannot guarantee that they contain valid data. Use TryParse so you can catch any errors.
 
Share this answer
 
Comments
ridoy 29-Dec-14 4:36am    
5ed
benzimen 29-Dec-14 4:41am    
where can i call this foreach loop?
i mean in all textbox _ontextchanged events?
Richard MacCutchan 29-Dec-14 5:01am    
You would need some event signalled by the user that all text boxes have complete data.
Abhinav S 29-Dec-14 8:17am    
Where is the code for this?
Richard MacCutchan 29-Dec-14 8:36am    
What code?
Use the key down event with a single handler.
For e.g.
Text1_KeyDown += sumHandler;<br />
Text2_KeyDown += sumHandler;

...
and so on.

In they sumHandler, sum all the text boxes and display in the total textbox.
void sumHandler(Control c,Event e)
{
txtTotal = Convert.ToInt(Text1.Text) + Convert.ToInt(Text2.Text) + ... Convert.ToInt(Text10.Text)
}
 
Share this answer
 
v4
Comments
ridoy 29-Dec-14 4:36am    
5ed
Abhinav S 29-Dec-14 5:31am    
Thanks
Richard MacCutchan 29-Dec-14 4:59am    
You cannot cast a text field to an integer. Also at this point you have no idea which textboxes contain values.
Abhinav S 29-Dec-14 5:31am    
Convert.ToInt should take care of that. Updated my answer.
Richard MacCutchan 29-Dec-14 5:54am    
No, because you cannot guarantee that the text contains only integers. You need to use TryParse in order to catch any invalid data. And you still do not know how many text boxes contain complete data.
You can do it in multiple ways in my opinion.
A case:
C#
foreach (Control c in this.Controls)
{               
        if (c is TextBox) // or, c.GetType() == typeof(Textbox)
       {
                int value = 0;
                if(int.TryParse(((TextBox)c).Text,out value))
                     totalValue += value;
       }
}
totalTextbox.Text = totalValue;

Others:
Add values from multiple textboxes and display the sum[^]
How to calculate with text boxes and skip empty text boxes?[^]
 
Share this answer
 
v3
Comments
benzimen 29-Dec-14 4:45am    
in which event i have write this foreach
loop?
ridoy 29-Dec-14 4:49am    
You need to do that on a event, say sum button press. But if you want to sum the amount in textbox textchange event then you need to look up at later methods i had mentioned.
Richard MacCutchan 29-Dec-14 5:03am    
You should use typeof to compare against GetType(), rather than GetType().Name.ToString().
ridoy 29-Dec-14 5:09am    
changed a bit, thanks, :)
Richard MacCutchan 29-Dec-14 5:12am    
:thumbsup:
What i understand from your requirement is that you wanna loop through all the textboxes dynamically and show the final output in the txttotal textbox.

C#
int total;
foreach(Control ctrl in Formname.Controls)
{
    if(ctrl.GetType().Equals("System.Web.UI.WebControls.TextBox"))
    {
        if(((TextBox)ctrl).ID!="txttotal")
        {
            total=+(Convert.ToInt32((TextBox)ctrl).Text);
        }
    }
}
txttotal.text = total.ToString();



I have not tested the above code.If you have any doubts,just let me know.Nothing to explain in the above code.very straight forward.
 
Share this answer
 
C#
int Total = 0;

foreach(Textox txt in Form1.Controls.OfType<textbox>())
{
   if(txt.Name != "txtTotal")
   {
      int textValue= 0;
      if(int.tryparse(txt.text, out textValue) == true)
      {
        total += textValue;
      }
   }
}

txtTotal.Text = Total.ToString();
</textbox>


I have made some assumptions. That all but one textbox is used to add these numbers. The textbox that the total numbers is call txtTotal.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900