问题
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
Form 1: Start button
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();
}
}
}
Form 2: Stop button
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();
}
}
}
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!
回答1:
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();
}
}
回答2:
Depending on if these forms exist within the same application, you could pass a reference to the first form into the second. And then (because the starttime1
DateTime
object is public, you can access it from the second form.
回答3:
It would be cleaner and more maintainable if stored the data in a place that is accessible from both forms. For example a static class. (You could also use a database or text file)
public static TempStore
{
public DateTime StartTime { get; set; }
}
Set StartTime
from From1
and read back the difference in Form2
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void StartButton_Click(object sender, EventArgs e)
{
TempStore.StartTime = DateTime.Now;
// ...
}
}
Form 2:
public partial class RoadSign1Meaning : Form
{
public RoadSign1Meaning()
{
InitializeComponent();
}
private void StopButton_Click(object sender, EventArgs e)
{
TimeSpan span = DateTime.Now - TempStore.StartTime;
// ...
}
}
This way, if you decide to pass more data to and from forms you can extend this static
class.
回答4:
There are several solutions but also depends on the complexity of what would you like to achieve. The simplest one with less modifications in your code would be to declare a constructor on your second form which accepts a parameter of type DateTime and pass in startTime2 from Form1 when creating the Form2 just like Form2 form2 = new Form2(startTime2). A more elegant way would be to define an interface with a method GetElapsedTime() and create a concrete in which takes care of the start/end time and you can pass the concrete instance to Form2 like before from Form1.
来源:https://stackoverflow.com/questions/18311215/passing-variable-time-from-one-form-to-another-c-sharp