## 3 replies

Last post Aug 10, 2018 02:12 PM by jonnygareth30

• jonnygareth3...

Member

74 Points

255 Posts

### 2 Textboxes display result and convert to hours

Hi,

I have the below code which adds 2 values from 2 different textboxes together and displays the result in a third textbox. When breaking the code down the result shows the correct calculation but the third textbox (TotalTravelminsTextBox) I am unable to figure out how this is happening as all is working until I try adding the total value to the textbox.

int hrs = Convert.ToInt32(sum) / 60;
int mins = Convert.ToInt32(sum) % 60;

int tevalue2 = 0;
int result = 0;

if (int.TryParse(TravelStartDurationminsTextBox.Text, out tsvalue1) & int.TryParse(TravelEndDurationMinsTextBox.Text, out tevalue2))
{
result = tsvalue1 + tevalue2;
TotalTravelminsTextBox.Text = result.ToString();   //Displays only the TravelStartDurationminsTextBox.Text value
hrs = Convert.ToInt32(sum) / 60;
mins = Convert.ToInt32(sum) % 60;
TotalTravelHrsTextBox.Text = hrs.ToString("D2") + ":" + mins.ToString("D2");

Any help would be greatly appreciated.

Thanks

Jonny

• Nan Yu

All-Star

18785 Points

3832 Posts

Microsoft

### Re: 2 Textboxes display result and convert to hours

Aug 10, 2018 02:45 AM|Nan Yu|LINK

Hi jonnygareth,

#### jonnygareth30

When breaking the code down the result shows the correct calculation but the third textbox (TotalTravelminsTextBox) I am unable to figure out how this is happening as all is working until I try adding the total value to the textbox.

What is the problem about the TotalTravelminsTextBox ? And please explain more about your detailed requirement , what is the sum ? What is the expected behavior of TotalTravelminsTextBox ?

Best Regards,

Nan Yu

MSDN Community Support
If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.
• jonnygareth3...

Member

74 Points

255 Posts

### Re: 2 Textboxes display result and convert to hours

Hi Nan Yu,

My apologies, i have missed a line of code from my first post, please see below updated code. I have detailed next to each line of code what the problem is with each step.

int tsvalue1 = 0;
int tevalue2 = 0;
int result = 0;

if (int.TryParse(TravelStartDurationminsTextBox.Text, out tsvalue1) & int.TryParse(TravelEndDurationMinsTextBox.Text, out tevalue2))
{
result = tsvalue1 + tevalue2;  // adds the 2 values together which works with no issues

TotalTravelminsTextBox.Text = result.ToString(); // The result.ToString() shows the correct calculation from the above line but in the TotalTravelminsTextBox.Text only the value from tsvalue1 is dispayed. So for example tsvalue1 = 1 and tsvalue2 = 2 the result = 3. When using TotalTravelminsTextBox.Text = result.ToString(); to populate the textbox with the result (which should be 3) value only the tsvalue (which is 1) is being displayed.

TotalTravelminsTextBox.Text = sum.ToString(); //this line and the code below converts the above result into hours, this all seems to be working with no issue.
hrs = Convert.ToInt32(sum) / 60;
mins = Convert.ToInt32(sum) % 60;
TotalTravelHrsTextBox.Text = hrs.ToString("D2") + ":" + mins.ToString("D2");

• jonnygareth3...

Member

74 Points

255 Posts