Click here to Skip to main content
15,917,568 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hai, how do i make the messagebox popup by itself, if the time of clock is set by user.

example: first clock - 5.30pm
second clock - 5.35pm

if the first clock is 5.35pm, the messagebox will popup.

What I have tried:

C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace SessionManager__Demo
{
    public partial class timeCompare : Form
    {
        public timeCompare()
        {
            InitializeComponent();
            timer1.Start();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            /*******************************************
             *          Display Current Time
             *******************************************/
            DateTime time1 = DateTime.Now;
            label1.Text = time1.ToString("hh:mm:ss:tt");
        }

        private void button1_Click(object sender, EventArgs e)
        {
            DateTime time1 = DateTime.Now;

            var time2 = time1.AddHours(0).AddMinutes(0).AddSeconds(0);
            var t2 = time2.ToString("hh:mm:ss:tt");

            label2.Text = t2;


            //compare time
            int compare = DateTime.Compare(time1, time2);

            if (compare == -1)
            {
                MessageBox.Show("time 1 is early");
            }else if (compare == 0)
            {
                MessageBox.Show("time 1 is same with time 2");
            }
            else
            {
                MessageBox.Show("time 1 is later");

            }


            
        }




    }
}
Posted
Updated 26-Dec-17 22:53pm

1 solution

Set up a timer (you've done this). Set it's interval to half a second or half a minute (depending on what precision you want).
Handle the TextChanged event for your text box, and use DateTime.TryParse to convert this to a DateTime value. Store that in a class level variable.

In your timer.Tick event handler, compare the current DateTime with the stored DateTime. If it is less then, ignore it.
Otherwise, set the stored time to DateTime.MaxValue and show your message box.
 
Share this answer
 

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