简体   繁体   English

我需要将两个变量一起添加到用于C#编程项目的消息框/文本框

[英]i need to add two variables together for a messagebox/textbox for a programming project IN C#

hey all so I'm having a hard time with my code when trying to display in a textbox .. testing goes well till i try something along the lines of a start time being a higher number than the end time when i do it displays something like this on my program -04-30 but on the example program it shows -0430. 嘿,所以尝试在textbox显示代码时,我遇到了textbox 。.测试一直很好,直到我尝试按开始时间大于结束时间的次数显示某些内容时,在我的程序-04-30上是这样的,但是在示例程序上它显示为-0430。

How can i get it to display properly? 我如何使其正确显示?

My last three lines of code read. 我的最后三行代码已读。 (this is for a programming project for my college class. i have my form program i have developed and then an example one. for the program it asks to give a start time, and end time. and the program calculates a new arrival time due to construction in the area(which makes the original trip 25% longer) (这是针对我大学课程的一个编程项目。我已经开发了我的表单程序,然后是一个示例。对于该程序,它要求给出开始时间和结束时间。该程序计算出新的到达时间到该地区的建筑(这会使原始行程延长25%)

int endTimeHours = (int) totalTimeMins / DIVISOR_SIXTY;
int endTimeMins = (int)totalTimeMins - (endTimeHours * DIVISOR_SIXTY);

//display answer in textbox for user to see and understand. //在文本框中显示答案,供用户查看和理解。

textBoxNewArrival.Text = string.Format("{0:d2}" + "{1:d2}", endTimeHours, endTimeMins);

entire code is; 整个代码是; `// Declare Constants const int HUNDRED = 100, DIVISOR_SIXTY = 60; `//声明常量const int HUNDRED = 100,DIVISOR_SIXTY = 60; const double CONSTRUCTION = 1.25; const double CONSTRUCTION = 1.25;

        //get start time from user
        int startTime = int.Parse(textBoxStart.Text);

        //get end time from user
        int endTime = int.Parse(textBoxEnd.Text);

        //separate and convert to hours and minutes for start time
        int hoursStart = startTime / HUNDRED;
        int minsStart = startTime % HUNDRED;
        int totalMinutesStart = (hoursStart * DIVISOR_SIXTY) + minsStart;

        //separate and convert to hours and minutes for end time
        int hoursEnd = endTime / HUNDRED;
        int minEnd = endTime % HUNDRED;
        int totalMinutesEnd = (hoursEnd * DIVISOR_SIXTY) + minEnd;

        //find total duration by subtracting total end times in minutes from total start time in minutes
        int totalDuration = totalMinutesEnd - totalMinutesStart;

        //take the totalDuration or time between start and end and multiply by 1.25 for construction time.
        double construction = totalDuration * CONSTRUCTION;

        //take totalmintuesstart from begining and add construction.
        double totalTimeMins = totalMinutesStart + construction;

        //find and convert the answers into hours and minutes total
        int endTimeHours = (int) totalTimeMins / DIVISOR_SIXTY;
        int endTimeMins = (int) totalTimeMins - (endTimeHours * DIVISOR_SIXTY);




        //display answer in textbox for user to see and understand.
        textBoxNewArrival.Text = string.Format("{0:d2}" + "{1:d2]", endTimeHours, endTimeMins.ToString().Remove(0, 1));          
        `

For a quick solution you should try doing this in the textBox assignation: 为了快速解决方案,您应该尝试在textBox分配中执行以下操作:

textBoxNewArrival.Text = string.Format("{0:d2}" + "{1:d2}", int.Parse(endTimeHours, endTimeMins.ToString().Remove(0,1)));

this is the test i did to check if this time worked: 这是我所做的测试,目的是检查这次是否有效:

class Forms
{
    public static void Main(string[] args)
    {
        int  endTimeHours = -4;;
        int endTimeMins = -30;
        string temp = string.Format("{0:d2}" + "{1:d2}", endTimeHours, int.Parse(endTimeMins.ToString().Remove(0, 1)));
        Console.WriteLine(temp);
    }
}

只需保留第一个值的符号,并始终使用第二个值的绝对值?

textBoxNewArrival.Text = string.Format("{0:d2}" + "{1:d2}", endTimeHours, Math.Abs(endTimeMins));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM