Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

c# Date Time picker help

Options
  • 03-04-2013 10:45am
    #1
    Registered Users Posts: 3,411 ✭✭✭


    Dear folks,

    Consider the following form
    247761.jpg

    There are three datetime pickers. We are interested in the hour : minute values only. Dates are irrelevant. So I have set them up to use custom format HH:mm.

    When a user sets bedtime, they can then set alarm 1 and alarm 2. However a1 and a2 must be within the alarm window (bedtime +- 3 hours as indicated by the alarm window label). So if we set a bedtime of 22:00, then the alarm window is 19:00 - 01:00.

    How do I police the updating of a1 and a2 to prevent the user setting a time outside the window? It's not as simple as it seems, bear in mind we do not care about the date, we are using hour and minute values only but in the case where the window falls around midnight, the clock goes into the next/previous day.

    So if I check the value of a1 in its value changed event, I need to check that its >= 19:00 but <= 01:00. Can you see the problem?

    Any help appreciated


Comments

  • Registered Users Posts: 527 ✭✭✭Sean^DCT4


    Seems straightforward, what have you tried?


  • Registered Users Posts: 196 ✭✭PersonalJesus


    The validation action itself seems like a candidate for an errorprovider http://www.dotnetperls.com/errorprovider

    DateTime also has a TimeOfDay property for comparing time components.


  • Registered Users Posts: 14,714 ✭✭✭✭Earthhorse


    Okay, so, think about this.

    It may not update the day itself so how do you resolve that?


  • Registered Users Posts: 14,714 ✭✭✭✭Earthhorse


    Well, you know how many hours are in a day and you know what the alarm window is.

    So I would advise using those known values in resolving the problem.


  • Registered Users Posts: 3,411 ✭✭✭dnme


    Here's my breakthrough, Just wondering if it's overkill and am I missing something far more elegant? Sometimes trying to solve these type of issues makes me snow blind (especially when my mind is on something else).
    What do you think of this?
    private void dtAlarm1_ValueChanged(object sender, EventArgs e)
            {
                int btHour = dtBedtime.Value.Hour;
                int lowlimit = (btHour -3) < 0 ? 24 + (btHour -3) : (btHour - 3);
                int highlimit = (btHour + 3) >= 24 ? Math.Abs(24 - (btHour + 3)) : (btHour + 3);
                bool includeMidnight = (((lowlimit + 6) - 24) >= 0);
    
                int high = lowlimit > highlimit ? lowlimit : highlimit;
                int low = lowlimit < highlimit ? lowlimit : highlimit;
    
                if (includeMidnight)
                {
                    if ((dtAlarm1.Value.Hour > low && dtAlarm1.Value.Hour < high))
                    {
                        dtAlarm1.Value = new DateTime(dtAlarm1.Value.Year, dtAlarm1.Value.Month,
                            dtAlarm1.Value.Day, low, dtAlarm1.Value.Minute, 0);
                    }
                }
                else
                {
                    if ((dtAlarm1.Value.Hour < low || dtAlarm1.Value.Hour > high))
                    {
                        dtAlarm1.Value = new DateTime(dtAlarm1.Value.Year, dtAlarm1.Value.Month,
                            dtAlarm1.Value.Day, low, dtAlarm1.Value.Minute, 0);
                    }
                }
            }
    


  • Advertisement
Advertisement