locked
2 Textboxes display result and convert to hours RRS feed

  • Question

  • User-1901014284 posted

    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

    Thursday, August 9, 2018 3:48 PM

Answers

  • User-1901014284 posted

    I have found my error I replaced the following line:

    TotalTravelminsTextBox.Text = result.ToString();

    with the below:

    sum += int.Parse(TotTravelDur.ToString());

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, August 10, 2018 2:12 PM

All replies

  • User1724605321 posted

    Hi jonnygareth,

    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

    Friday, August 10, 2018 2:45 AM
  • User-1901014284 posted

    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");

    Friday, August 10, 2018 9:19 AM
  • User-1901014284 posted

    I have found my error I replaced the following line:

    TotalTravelminsTextBox.Text = result.ToString();

    with the below:

    sum += int.Parse(TotTravelDur.ToString());

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, August 10, 2018 2:12 PM