Solved

# Help!! DateTimePicker

Posted on 2004-10-28

HI experts,

I have tried this for 3 hours and couldn't get it to work. :((

Scenerio:

1)I have a comboBox which I could choose the time interval in an hour of 15, 20, 30 or 60 Mins.

2)I have a dateTimePicker(shows only time) for schedule items.

The increment or decrement of the DateTimePicker minute is according to the interval chosen.

if interval of 20 is choosen, when I click up sign, 10:20 PM becomes 10:40PM, down button 10:20PM becomes 10:00PM.

3) Schedule items (FYI) are scheduled according to the interval chosen. if interval of 30 is chosen, the schedule can only be the time interval 30. Eg 10.30pm, 11.30pm, 3.30AM.

But some schedule items are schedule before the interval change. Therefore my schedule item time would not have a interval time of the chosen interval. EG. 10.30PM but the interval chosen is 20 minutes.

What I want to do is when I click the up or down button of the date time picker it will go to the nearest time of the current chosen interval. For example.

--------------------------------------------------------

Example 1)

Interval Chosen := 30;

Original Time := 10.20PM; <- the time in datetimepicker

When I click up when i click down

once > 10:30 pm once > 10:00 PM

twice > 11.00pm Twice > 9:30 PM

and so on

--------------------------------------------------

Example 2)

Interval Chosen := 20 minutes;

Original Time := 10.45PM; <- the time in datetimepicker

When I click up when i click down

once > 11:00 pm once > 10:40 PM

twice > 11.20 pm Twice > 10:20 PM

and so on

--------------------------------------------------

Example 2)

Interval Chosen := 20 minutes;

Original Time := 10.15PM; <- the time in datetimepicker

When I click up when i click down

once > 10:20 pm once > 10:00 PM

twice > 10.40 pm Twice > 9:40 PM

and so on

-------------------------------------------------------------------

I'm a student just started learning delphi this year, so please bear with me :)

Below are some of my codes, which are not very good. Just ignore my codes and see if there are any good idea.

The alogrithms of my codes is wrong. It works for some scenerio but not all. And some parts of the codes are not in there.

Thank you very much in advance. I really need it to work ASAP. Thanks.

//************************************************************

procedure TDefaultPlannerItemEditForm.DateTimePickerChange(

Sender: TObject);

var

TempMin, TempHour, DTHour, DTMin, Ohour, Omin, tempsec, tempmsec : Word;

tInterval, tempTime, ainterval : TTime;

Minus, Unequal : Boolean;

a, b, c, CurrentInterval : integer;

begin

CurrentInterval := FormSchedule.SLInterval;

TempMin := CurrentInterval mod MintuesPerHour;

TempHour := CurrentInterval div MintuesPerHour;

tInterval := EncodeTime(TempHour, TempMin, 0, 0);

if sender = StartTimeDateTimePicker then

tempTime := OriginalStartTime

else

tempTime := OriginalEndTime;

DecodeTime((sender as TDateTimePicker).Time, DTHour, DTMin, tempsec, tempmsec);

DecodeTime(tempTime, Ohour, Omin, tempsec, tempmsec);

if (Omin <> 0) and

(Max(CurrentInterval, Omin) mod

Min(CurrentInterval, Omin) <> 0 ) then

begin

a := Omin - CurrentInterval;

b := CurrentInterval - Omin;

c := CurrentInterval - a;

end;

//let temptime and datetimepicker date be the same before comparing the time

(sender as TDateTimePicker).Date := tempTime;

Minus := tempTime > (sender as TDateTimePicker).time;

if DTHour = Ohour then

begin

Unequal := False;

if (Omin <> 0) and (Max(CurrentInterval, Omin) mod

Min(CurrentInterval, Omin) <> 0 ) then

begin

a := Omin - CurrentInterval;

b := CurrentInterval - Omin;

c := Max(CurrentInterval, a) - Min(CurrentInterval, a);

Unequal := True;

end;

if Minus then

begin

if Unequal then

begin

if CurrentInterval > Omin then

(sender as TDateTimePicker).Time := tempTime - EncodeTime(0,b*2,0,0)

else if CurrentInterval < Omin then

(sender as TDateTimePicker).Time := tempTime - EncodeTime(0,a,0,0);

end

else

(sender as TDateTimePicker).Time := tempTime - tInterval;

end

else

begin

if Unequal then

begin

if CurrentInterval > Omin then

(sender as TDateTimePicker).Time := tempTime + EncodeTime(0,b,0,0)

else if CurrentInterval < Omin then

(sender as TDateTimePicker).Time := tempTime + EncodeTime(0,c,0,0);

end

else

(sender as TDateTimePicker).Time :=

tempTime + ifthen (DTMin <> 59, tInterval, -tInterval );

end;

end;

tempTime := (sender as TDateTimePicker).Time;

if sender = StartTimeDateTimePicker then

OriginalStartTime := tempTime

else

OriginalEndTime := tempTime;