Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

run calculations without erroring out 1

Status
Not open for further replies.

Saturn57

Programmer
Aug 30, 2007
275
CA
I have a form with many text boxes that are used to make calculations for text boxes on the same form. I would like all calculations to happen every time one of the text boxes that is used to calculate another text box. I have created the code for the calculations but the problem occurs where i'm just starting to input some of the text boxes and because other variables are not complete the calculation errors out and kicks me into debugger mode. Any suggestions?
 
Put code in that will first check to ensure that the other required variables exist before it will attempt to run the code for the calculations. That would be the best option.

------------------------------------------------------------------------------------------------------------------------
Reason and free inquiry are the only effectual agents against error; they are the natural enemies of error and of error only.

Thomas Jefferson

 
That is what I thought. I was looking for a faster way as I have many variables.
 
I'm not aware of one off the top of my head but that doesn't mean there may not be a way. There probably is but there are no guarantees that it would be any more efficient...

Perhaps setting a global variable and then incrementing it each time that a textbox has the variable entered (and decrementing it if the variable is deleted). Then you can check to confirm that the global variable equals the total number of variables required and, if so, perform your calculation. But this thought is just random musing at this point... Again, not sure if it's necessarily more efficient.

------------------------------------------------------------------------------------------------------------------------
Reason and free inquiry are the only effectual agents against error; they are the natural enemies of error and of error only.

Thomas Jefferson

 
The problem occurs when the form loads values from the database to the combo box instead of selecting the value in the combo box. When I use a case statement similar to the following "switch (combobox.Text)" there is no value assigned to combobox.Text although it is in the database and a string shows up in the combobox on the form. I dont know how to call this string. If I reselect the item in the combo box then that string is assigned to combobox.Text and the case statement works. But once I close the form and come back and open the record it no longer has the value although again it shows in the combobox.

HELP Please. Thanks for hanging in.
 
I'm a little tired and not following very well. Can you post the relevant code and that may allow us to help you find the problem?

------------------------------------------------------------------------------------------------------------------------
Reason and free inquiry are the only effectual agents against error; they are the natural enemies of error and of error only.

Thomas Jefferson

 
Thanks for hanging in Chopstick here is the code

//metric/inch conversion


switch (bunitsCB.Text)
{
case "Metric":
bl = Convert.ToDecimal(blanklTB.Text) / Convert.ToDecimal(25.4);
bw = Convert.ToDecimal(blankwTB.Text) / Convert.ToDecimal(25.4);
bp = Convert.ToDecimal(blankpTB.Text) / Convert.ToDecimal(25.4);
pd = Convert.ToDecimal(partdepthTB.Text) / Convert.ToDecimal(25.4);break;
case "English":
bl = Convert.ToDecimal(blanklTB.Text);
bw = Convert.ToDecimal(blankwTB.Text);
bp = Convert.ToDecimal(blankpTB.Text);
pd = Convert.ToDecimal(partdepthTB.Text);break;
default:
bl = 0;
bw = 0;
bp = 0;
pd = 0;
listBox1.Items.Add("YOU MUST SELECT A BLANK UNIT OF MEASURE");break;
}
switch (dieunitsCB.Text)
{
case "Metric":
dl = Convert.ToDecimal(dielTB.Text) / Convert.ToDecimal(25.4);
dw = Convert.ToDecimal(diewTB.Text) / Convert.ToDecimal(25.4);
ds = Convert.ToDecimal(dieshTB.Text) / Convert.ToDecimal(25.4);break;
case "English":
dl = Convert.ToDecimal(dielTB.Text);
dw = Convert.ToDecimal(diewTB.Text);
ds = Convert.ToDecimal(dieshTB.Text);break;
default:
dl = 0;
dw = 0;
ds = 0;
listBox1.Items.Add("YOU MUST SELECT A DIE UNIT OF MEASURE");break;
}
 
Sorry it's taken me a while to get back to this. I'm afraid that I still don't really see where your issue lies. The code above is a couple of switch statements that will set some other variables depending on the values in the (presumed) textboxes checked in the switch cases. But I'm not sure what this has to do with the problem you had described earlier.

------------------------------------------------------------------------------------------------------------------------
Reason and free inquiry are the only effectual agents against error; they are the natural enemies of error and of error only.

Thomas Jefferson

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top