Reputation: 17
I want a timespan with a message box that tells which part of the day it is. I have the goodmorning, good first part of the day and good second part of the day. I also have good evening. I am a beginner with C#. Now the code show's all the messageboxes at once (after 1 another). Can someone explain to me what i am doing wrong here.
This is my code:
I tried to do it like: if(morgenStart == b && morgenEnd == b)
But that doesn't work either, the expected code needs to show me which part of the day it is with the messagebox.
TimeSpan morgenStart = new TimeSpan(06, 0, 0);
TimeSpan morgenEnd = new TimeSpan(12, 0, 0);
TimeSpan middagStart = new TimeSpan(12, 0, 0);
TimeSpan middagEnd = new TimeSpan(14, 0, 0);
TimeSpan namiddagStart = new TimeSpan(17, 0, 0);
TimeSpan namiddagEnd = new TimeSpan(18, 0, 0);
String a = DateTime.Now.ToLongDateString();
TimeSpan b = DateTime.Now.TimeOfDay;
textBox1.Text = a + " " + b;
if (morgenStart < morgenEnd)
{
MessageBox.Show("Goedemorgen", "Tijd",
MessageBoxButtons.OK, MessageBoxIcon.Information);
}
if (middagStart < middagEnd)
{
MessageBox.Show("Goedemiddag", "Tijd",
MessageBoxButtons.OK, MessageBoxIcon.Information);
}
if (namiddagStart < namiddagEnd)
{
MessageBox.Show("Goede namiddag", "Tijd",
MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
MessageBox.Show("Goede avond", "Tijd",
MessageBoxButtons.OK, MessageBoxIcon.Information);
}
Upvotes: 1
Views: 97
Reputation: 119206
Your if
statements should be comparing with b
. Also, consider using else if
blocks to prevent multiple matches. For example:
if (b > morgenStart && b < morgenEnd)
{
//Goedemorgen
}
else if (b > middagStart && b < middagEnd)
{
//Goedemiddag
}
else if (b > namiddagStart && b < namiddagEnd)
{
//Goede namiddag
}
else
{
//Not really sure what you want to do here, I don't speak Dutch!
//Goede avond
}
Upvotes: 2