A message box depends on time

Ahmed Uossery

Member
Joined
May 10, 2011
Messages
6
Programming Experience
1-3
First, I wanted to say that I couldn't describe thread title so I'm sorry and thanks for every one helped me.


Second, I want to make a form that tells the user good morning if he was in the morning or tell him good afternoon if he was in the afternoon and it depends on the computer clock like if the clock was 3.00 AM the message box shown and tell him good afternoon.


Thanks for every thing.
 
That's not really anything to do with the message box itself. The message box will just display the String that you give it. You just need to construct the appropriate String.

First, you use Date.Now to get a Date that represents the current date and time. Next, you use the TimeOfDay property of that Date to get a TimeSpan that represents the current time. Next you use the Hours property of that TimeSpan to get the whole hours that have passed today, which will be in the range 0 - 23. Anything less than 12 is morning, anything else less than 18 is afternoon and anything else is evening. You would probably use a Select Case statement to differentiate those ranges and then use String.Format or just straight string concatenation to then build the appropriate message.

Once you have the message in a String, you display that String in a message box as you would any other.
 
The following code will display the appropriate message depending on the time of day:

VB.NET:
   Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim wholeday As Date = Now
        Dim daytime As Decimal
        Dim partday As String = ""
        daytime = wholeday.TimeOfDay.Hours
        Select Case daytime
            Case 17 To 23
                partday = "evening"
            Case 11 To 17
                partday = "afternoon"
            Case 5 To 11
                partday = "morning"
            Case 0 To 5
                partday = "night"
        End Select
        MessageBox.Show("Good " & partday & "!")
    End Sub
 
Dim daytime As Decimal
was browsing. small notes: i think 'Now.TimeOfDay.Hours' returns an integer? a integer data type will probably be enough?

if you didn't want/need to use variables you could also do 'Select Case Now.TimeOfDay.Hours'
 
In actual fact, there's no need to even use the TimeOfDay. The Date itself has an Hour property, so the Select Case would most efficiently be done like this:
Select Case Date.Now.Hour
    Case Is < 12
        'Morning
    Case Is < 18
        'Afternoon
    Case Else
        'Evening
End Select
 
Back
Top