Recent

Author Topic: DateTimePicker.DateTime value is not updated  (Read 4099 times)

Vodnik

  • Full Member
  • ***
  • Posts: 210
DateTimePicker.DateTime value is not updated
« on: March 25, 2018, 02:32:12 pm »
I have DateTimePicker and Menu controls in my form.
Sometimes it happens, that user click to Menu, not having finish entering Time, e.g.
previous value was 00:00:00, user entered 1:00:00 and clicked Menu.
In that case DateTimePicker.DateTime property is not updated to a new value,
and MenuItemClick do processing of an old value (see screenshot).
This happens only with Menu, if user click to another control, DateTime value is applied.
I would expect there should be a standard procedure that can be called to push update of the control's value or even all values of all controls of a form, but I didn't find one.
I tried calling DateTimePicker.Update from MenuItemClick, but it didn't help.
What is a workaround for this situation?


Windows 7, Lazarus 1.8.0, FPC 3.0.4

jamie

  • Hero Member
  • *****
  • Posts: 6130
Re: DateTimePicker.DateTime value is not updated
« Reply #1 on: March 25, 2018, 04:09:40 pm »
Implement the OnEnter event:

   Tag := 0; // indicates no change;

Implement the OnChange event;

  Tag := 1;

Implement the OnExit event;

 If Tag = 0 then Dosomething about it!
The only true wisdom is knowing you know nothing

Vodnik

  • Full Member
  • ***
  • Posts: 210
Re: DateTimePicker.DateTime value is not updated
« Reply #2 on: March 25, 2018, 05:55:21 pm »
jamie, I didn't catch...

Whether I knew what to do behind "Dosomething", I also could do this in MenuItemClick, isn't it?



jamie

  • Hero Member
  • *****
  • Posts: 6130
Re: DateTimePicker.DateTime value is not updated
« Reply #3 on: March 25, 2018, 06:14:29 pm »
It means you can handle the fact that the user didn't finish, at which point, I guess you could
reset the current value in the picker..?

 Show a message box explaining it etc...

I suppose you could even store a copy of the current date within the OnEnter event.
and then use that to Restore the value if user does not properly exit.

There are many options... you need to decide..

I suppose you could also call the OnChange event directly in the OnExit event if you determine that the change didn't
take effect..

If Tag = 0 then If Assigned(OnChanged) then OnChanged(Self);

at which point you can then take what ever is in there.

The only true wisdom is knowing you know nothing

Vodnik

  • Full Member
  • ***
  • Posts: 210
Re: DateTimePicker.DateTime value is not updated
« Reply #4 on: March 25, 2018, 06:55:54 pm »
jamie, problem is that DateTimePicker.onExit event do not shoot when user stops entering time from keyboard and clicks a menu item. DateTimePicker control is not loosing the focus while user is clicking menu items. Clicking other controls shoots DateTimePicker.onExit event, but in this case it's own built-in onExit event do the work as well, updating the DateTime property. So your solution do not help in this case.

Zoran

  • Hero Member
  • *****
  • Posts: 1830
    • http://wiki.lazarus.freepascal.org/User:Zoran
Re: DateTimePicker.DateTime value is not updated
« Reply #5 on: March 25, 2018, 07:10:21 pm »
In MenuItem1Click, first call DateTimePicker1.EditingDone, then do what you want. For example:
Code: Pascal  [Select][+][-]
  1. procedure TForm1.MenuItem2Click(Sender: TObject);
  2. begin
  3.   DateTimePicker1.EditingDone; // makes sure user changes are applied inside DateTimePicker
  4.   Label1.Caption := DateToStr(DateTimePicker1.DateTime);
  5. end;

Vodnik

  • Full Member
  • ***
  • Posts: 210
Re: DateTimePicker.DateTime value is not updated
« Reply #6 on: March 25, 2018, 07:58:12 pm »
Thanks a lot, Zoran, this did the work!

 

TinyPortal © 2005-2018