Textbox & Math Calculations

  • Thread starter Thread starter RobRich
  • Start date Start date
R

RobRich

I'm trying to figure out the best way to accomplish a task. I need to
calculate the numerical values from
about 300+ textboxes. Basiclly I have an application that users input
checks and credit card receipts into
and the program calculates the total.. Right now I am just doing

Decimal.Parse(textbox1.Text) + Decimal.Parse(textbox2.Text) + etc...


So is this what I should be doing to accomplish what I'm trying to do? The
application works fine using
that functionality but not sure if thats the best way to code the
application..

Any suggestions welcome.

Thanks
 
You could use this code to reduce the amount of code you type, It could make
it more readable. This code will check for all controls in the screen and if
that control is a textbox, it will add that data into a variable.

You could use a if condition if u do not want to add few textbox data. You
wouldnt want to add textbox like name, id and stuff like that... just use
one more condition like if((ctrl.Name != txtName) && (ctrl.Name != txtId))
and so on.

Type t = typeof(System.Windows.Forms.TextBox);

decimal numTxtCount = 0;

foreach(Control ctrl in this.Controls)
{
if (ctrl.GetType() == t)
{
numTxtCount += Decimal.Parse(ctrl.Text);
}
}
 
hi RobRich:

You can use for sentence to accomplish this task,looks like this
following code


public Decimal GetValue()
{
TextBox textControl = new TextBox();
string controlName = "TextBox";
Decimal textValue = 0;

for(int i = 1 ; i < 5; i++)
{
textControl = checked((TextBox)Page.FindControl(controlName + i));
textValue = textValue + Decimal.Parse(textControl.Text);
}

return textValue;

}

And you can use this method looks like following code

private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
Response.Write(this.GetValue());
}




jiangyh
 
Back
Top