Click here to Skip to main content
15,898,134 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hey guys, I'm new in this forum and this is my first post. I come from Germany and I'm almost 17 years old.

My problem is a little extensive.
I hava a normal WindowsFormsApplication.
There are 7 DateTimePickers where the user can set 7 different times (hh:mm:ss).
At the bottom of my program, there is a Label which should show the smallest difference between the actual time and one of the 7 user defined times.
If the difference equals 00:00:00 the program should show me a MessageBox and the label with the difference should update from 00:00:00 to the new difference with the 2nd smallest difference between the actual time and one of the 6 left user defined times.
This function I need for all 7 days of the week.


I hope, you understood. If you have any questions or a suggested solution, please write me.

Thanks for efforts.
Posted
Comments
OriginalGriff 28-Sep-15 12:10pm    
And?
What have you tried?
Where are you stuck?
What help do you need?
Tobias Wälde 28-Sep-15 13:02pm    
Hey,
I tried to test for every DateTimePicker. But I want to show only the smalles difference in the label. So I wrote a lot of if-querys in a timer to show only the smallest difference.
How can I write the if-querys for 7 DateTimePickers?
And in my test-project when the time difference was equal 00:00:00 or less it opened a lot of MessageBoxes:

if([smallest Value of all DateTimePickers] < DateTime.Now)
{
MessageBox.Show("Notification: Time is over!");
}

How can I let the program show the messagebox only once?
How can I say the program that it should show the 2nd smallest difference?
[no name] 28-Sep-15 12:15pm    
Hi Tobias
"If the difference equals 00:00:00" is dangerous, nobody guarantees that your process get enough attention to recognize that. You need to catch more information, something like "diff > 0", set a "trigger flag" and handle whether a specific time event has already been handled.
Tobias Wälde 28-Sep-15 13:05pm    
Hi,

I tried this:

if([smallest Value of all DateTimePickers] < DateTime.Now)
{
MessageBox.Show("Notification: Time is over!");
}

But I get lots of MessageBoxes...
[no name] 28-Sep-15 13:19pm    
"But I get lots of Message Boxes": That I would expect also...
I think you are able to solve it (this according what you post here and according to this it seems you are analyzing the problem), try a little bit harder. Handling such timer events _exactly one time _ is not that trivial as it looks on a first glance.
Regards, Bruno

1 solution

"I tried to test for every DateTimePicker. But I want to show only the smalles difference in the label. So I wrote a lot of if-querys in a timer to show only the smallest difference.
How can I write the if-querys for 7 DateTimePickers?"


Don't! :laugh: way, way too many combinations.
So...set up an array of TimeSpan values, and load it with the 7 DateTimePicker values minus the current dateTime:
C#
DateTime now = DateTime.Now;
Timespan[] diffs = new TimeSpan[7];
diffs[0] = dateTimePicker1.Value - now;
diffs[1] = dateTimePicker2.Value - now;
diffs[2] = dateTimePicker3.Value - now;
diffs[3] = dateTimePicker4.Value - now;
diffs[4] = dateTimePicker5.Value - now;
diffs[5] = dateTimePicker6.Value - now;
diffs[6] = dateTimePicker7.Value - now;

Then use Array.Sort and you have the maximum and minimum easily available.
C#
Array.Sort(diffs);
Timespan min = diffs[0];
Timespan max = diffs[6];
(Obviously, I wouldn't use "magic numbers" in the real world)

And only ever read the time once in a method: it changes outside your control, so even doing this:
DateTime now1 = DateTime.Now;
DateTime now2 = DateTime.Now;
C#
if (now1 != now2) throw new ApplicationException("The time changed!");
Sometimes, you will get an exception, and sometimes you won't...and they can seem very different - different millenium if you do it at the wrong time! :laugh:
 
Share this answer
 
Comments
Tobias Wälde 29-Sep-15 2:22am    
Hey, thanks a lot for your answer.
But I have a question:
Where should I set the first code?
Should I write the code into a running timer?
OriginalGriff 29-Sep-15 3:21am    
If you want it to execute repeatedly, then yes...

And it's all code you need together, not separate chunks - I separated them so you could see what did what more easily.
Tobias Wälde 29-Sep-15 11:58am    
Ok, my program runs perfectly.
But I want to test for the DateTimePickers which are activated.
This is my actual code:

<pre lang="c#">private void timer_Tick(object sender, EventArgs e)
{
DateTime now = DateTime.Now;
TimeSpan[] diffs = new TimeSpan[7];

if(dateTimePicker1.Checked)
diffs[0] = now - dateTimePicker1.Value;
if(dateTimePicker2.Checked)
diffs[1] = now - dateTimePicker2.Value;
if(dateTimePicker3.Checked)
diffs[2] = now - dateTimePicker3.Value;
if(dateTimePicker4.Checked)
diffs[3] = now - dateTimePicker4.Value;
if(dateTimePicker5.Checked)
diffs[4] = now - dateTimePicker5.Value;
if(dateTimePicker6.Checked)
diffs[5] = now - dateTimePicker6.Value;

Array.Sort(diffs);
TimeSpan max = diffs[0];
TimeSpan min = diffs[6];

this.lab_timeLeft.Text = (min.Hours * (-1)).ToString("00") + ":" + (min.Minutes * (-1)).ToString("00") + ":" + (min.Seconds * (-1)).ToString("00");
}</pre>

By the way, how can I paste the code as 'Code' and not as 'Text'?
Richard Deeming 29-Sep-15 15:07pm    
Since you don't know how many elements you'll be dealing with, try using a List<TimeSpan> instead of the array:

DateTime now = DateTime.Now;
List<TimeSpan> diffs = new List<TimeSpan>(7);

if (dateTimePicker1.Checked) diffs.Add(dateTimePicker1.Value - now);
if (dateTimePicker2.Checked) diffs.Add(dateTimePicker2.Value - now);
if (dateTimePicker3.Checked) diffs.Add(dateTimePicker3.Value - now);
if (dateTimePicker4.Checked) diffs.Add(dateTimePicker4.Value - now);
if (dateTimePicker5.Checked) diffs.Add(dateTimePicker5.Value - now);
if (dateTimePicker6.Checked) diffs.Add(dateTimePicker6.Value - now);

// Make sure at least one is selected:
if (diffs.Count == 0) return;

diffs.Sort();
TimeSpan min = diffs[0];
TimeSpan max = diffs[diffs.Count - 1];

this.lab_timeLeft.Text = min.ToString("c");

If you're going to negate the value at the end, you might as well swap the order of the operands in the subtraction: picker - now is the same as -(now - picker).

Using the TimeSpan's format specifier[^] is easier than formatting and combining the individual parts.

NB: If it's not already there, you might need to add using System.Collections.Generic; to the top of your code file.
Tobias Wälde 1-Oct-15 0:51am    
Hey,
thanks a lot for your informations. My programm is running nearly perfectly now. But I want to set three notification times. This is my actual code:
[code]
How can I write a function where my program can view a MessageBox when the left time is e.g. at 10 minutes?

I tried this:

TimeSpan zero = new TimeSpan(0, 0, 0, 0, 0);
TimeSpan notification1 = new TimeSpan(0, 0, 10, 0, 0); //10mins

if(dateTimePicker1.Checked)
{
if((dateTimePicker1.Value – DateTime.Now) > zero)
diffs.Add(dateTimePicker1.Value – DateTime.Now);

if((dateTimePicker1.Value – DateTime.Now) < notification1)
MessageBox.Show("Notification: 10 minutes left!");
}


My problem is that I got the MessageBox 10 times per second (cause timer.Intervall = 100) if the left time is lower than 10 minutes.
How can I let the program show me the messagebox only once?

And how can I add a screenshot of my program? Because the GUI is nearly completed and so I can show you what I want to do.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900