传递从一个表单变量(时间)到另一个C# [英] Passing variable (time) from one form to another C#

查看:111
本文介绍了传递从一个表单变量(时间)到另一个C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我们说我有两种形式。第一个将包含启动按钮,而另一个是停止按钮。有没有一种方法,其中我能确定按下启动和停止按钮,并显示在第二个形式之间的时间。

Let's say I have two forms. The first one will contain the start button and the other one is the stop button. Is there a way wherein I can determine the elapsed time between pressing the start and stop button and show it in the 2nd form.

我试着这样做,并在这些代码到达

I tried doing this and arrive at these codes

表1:启动按钮

namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
    public DateTime startTime2;
    public DateTime endTime;
    public TimeSpan ts_timeElapsed;
    public string s_timeElapsed;


    public Form1()
    {
        InitializeComponent();
    }

    private void StartButton_Click(object sender, EventArgs e)
    {
        startTime2 = DateTime.Now;
        Form2 frm = new Form2();
        frm.Show();
        this.Hide();


    }

    private void Button2_Click(object sender, EventArgs e)
    {
        Instructions frm = new Instructions();
        frm.Show();
        this.Hide();

    }


}
}

表2:停止按钮

namespace WindowsFormsApplication1
{
public partial class RoadSign1Meaning : Form
{
    public DateTime startTime1;
    public DateTime endTime;
    public TimeSpan ts_timeElapsed;
    public string s_timeElapsed;

    public RoadSign1Meaning()
    {
        InitializeComponent();
    }
    public string GetElapsedTimeString()
    {
        int days = ts_timeElapsed.Days;
        int hours = ts_timeElapsed.Hours;
        int mins = ts_timeElapsed.Minutes;
        int secs = ts_timeElapsed.Seconds;
        string x = "";
        if (days != 0)
        {
            x += days.ToString() + ":";
        }
        if (hours != 0)
        {
            x += hours.ToString() + ":";
        }
        if (mins != 0)
        {
            x += mins.ToString() + ":";
        }
        if (secs != 0)
        {
            x += secs.ToString();
        }

        return x;
    }

    private void StopButton_Click(object sender, EventArgs e)
    {
                   endTime = DateTime.Now;
        ts_timeElapsed = (endTime - startTime1);
        s_timeElapsed = GetElapsedTimeString();
        ElapsedLabel.Text = "Time Elapsed: " + s_timeElapsed;

        Form3 frm = new Form3();
        frm.Show();
    }
}
}



然而,问题是从表1时间值不保存,因此形成2显示了错误的经过时间值。任何建议,使我的代码工作的?谢谢!

However, the problem is the time value from form 1 is not save therefore form 2 display a wrong elapsed time value. Any suggestions to make my code work? Thanks!

推荐答案

道次开始时间为第二种形式

Pass start time to second form

private void StartButton_Click(object sender, EventArgs e)
{
    Form2 frm = new Form2(DateTime.Now);
    frm.Show();
    this.Hide();
}



,然后用它

And then use it

public partial class Form2 : Form
{
    private DateTime startTime;

    public Form2(DateTime startTime)
    {
        InitializeComponent();
        this.startTime = startTime;
    }

    private void StopButton_Click(object sender, EventArgs e)
    {
        endTime = DateTime.Now;
        ts_timeElapsed = (endTime - startTime);
        s_timeElapsed = GetElapsedTimeString();
        ElapsedLabel.Text = "Time Elapsed: " + s_timeElapsed;

        Form3 frm = new Form3();
        frm.Show();
    }     
}

这篇关于传递从一个表单变量(时间)到另一个C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆