How Can I Enable and Disable Time by The time in C#

In my Windows forms I want to disable and enable the button by the time example: 4:00 PM the button will be disable.

Here is my code:

labelTime.Text = DateTime.Now.ToShortTimeString(); // this will be the time

timer1.Start();

switch (DateTime.Now.ToShortTimeString())
{
    case "4:51 AM":
        OutBtn.Enabled = true;
        break;

    default:
        OutBtn.Enabled = false;
        break;
}

Upvotes: 0

Views: 471

Answers (1)

Hossein Ebrahimi
Hossein Ebrahimi

Reputation: 822

This is one way you can do it:

// In constructor of form.
Timer timer = new Timer();
timer.Tick += timer_Tick;

DateTime now = DateTime.Now;
DateTime disableTime = new DateTime(now.Year, now.Month, now.Day, 4, 51, 0);

if(disableTime > now)
   timer.Interval = (int)(disableTime - now).TotalMilliseconds;
timer.Start();

Timer_Tick would be like this:

private void timer_Tick(object sender, EventArgs e)
{
    yourButton.Enabled = false;
    (sender as Timer).Stop();
}

Update

As mentioned by Brett Caswell in the comment below, the above code doesn't "enable and disable" the button.

Below is an example which enables/disables the button according to the specified enableTime and disableTime(I'm disabling/enabling BtnExit).

DateTime enableTime = new DateTime(1, 1, 1, 4, 51, 0);
DateTime disableTime = new DateTime(1, 1, 1, 16, 0, 0);

public FrmMain()
{
    InitializeComponent();

    Timer timer = new Timer();

    DateTime now = DateTime.Now;
    
    bool isNowEnableTime = enableTime.TimeOfDay < now.TimeOfDay && now.TimeOfDay < disableTime.TimeOfDay;

    if (isNowEnableTime)
    {
        setIntervalForNextDisableTime(timer);
        timer.Tick += disableButton;
    }
    else
    {
        setIntervalForNextEnableTime(timer);
        timer.Tick += enableButton;
    }

    timer.Start();
}

private void disableButton(object sender, EventArgs e)
{
    BtnExit.Enabled = false;
    
    var timer = sender as Timer;

    timer.Stop();
    timer.Tick -= disableButton;

    timer.Tick += enableButton;
    setIntervalForNextEnableTime(timer);

    timer.Start();
}

private void enableButton(object sender, EventArgs e)
{
    BtnExit.Enabled = true;

    var timer = sender as Timer;

    timer.Stop();
    timer.Tick -= enableButton;

    timer.Tick += disableButton;
    setIntervalForNextDisableTime(timer);

    timer.Start();
}

private void setIntervalForNextDisableTime(Timer timer) =>
    timer.Interval = (int)(getNextOccurranceOfTime(disableTime) - DateTime.Now).TotalMilliseconds;

private void setIntervalForNextEnableTime(Timer timer) =>
    timer.Interval = (int)(getNextOccurranceOfTime(enableTime) - DateTime.Now).TotalMilliseconds;

/// <summary>
/// Get the next occurrance of <paramref name="dateTime"/> relative to now.
/// </summary>
private DateTime getNextOccurranceOfTime(DateTime dateTime) => getNextOccurranceOfTime(dateTime, DateTime.Now);
private DateTime getNextOccurranceOfTime(DateTime dateTime, DateTime relativeTo)
{
    if(relativeTo.TimeOfDay > dateTime.TimeOfDay) /// dateTime was in past. Next occurrance is going to be tommorrow.
    {
        var tomorrow = relativeTo.AddDays(1);
        return new DateTime(tomorrow.Year, tomorrow.Month, tomorrow.Day, dateTime.Hour, dateTime.Minute, dateTime.Second);
    }

    /// dateTime hasn't come yet, it's going to be today.
    return new DateTime(relativeTo.Year, relativeTo.Month, relativeTo.Day, dateTime.Hour, dateTime.Minute, dateTime.Second);
}

Upvotes: 2

Related Questions