Recent

Author Topic: Problem with DateTimePicker  (Read 2166 times)

greenmask

  • Newbie
  • Posts: 4
Problem with DateTimePicker
« on: November 20, 2020, 11:05:45 pm »
Hi! everyone, I've a problem with DateTimePicker, when I select date,  it's execute event Form OnActivate, if I have edit1.clear on event form OnActivate,  it's is executed, How can solution this? My Ide is versión 2.0.10, thank for answer.

Eleazar

  • New Member
  • *
  • Posts: 23
Re: Problem with DateTimePicker
« Reply #1 on: November 20, 2020, 11:45:21 pm »
Hi Greenmask,
I think it is normal behavior of the TDateTimePicker component. The TDateTimePicker uses a customform to draw the calendar on. When it is closed a killfocus is executed and the focus is given back to the parent, in this case your form. So when this is activated again, you get the OnActivate. I don't see a quick solution to prevent this. Maybe someone else does?
Greetings, Leo

howardpc

  • Hero Member
  • *****
  • Posts: 3612
Re: Problem with DateTimePicker
« Reply #2 on: November 20, 2020, 11:52:03 pm »
Yes, the parent form's reactivation is built in to the date selection (which causes the control's window to close and the parent to regain focus, if possible).
Perhaps you could do your edit1.Clear in another form event such as OnShow?

greenmask

  • Newbie
  • Posts: 4
Re: Problem with DateTimePicker
« Reply #3 on: November 21, 2020, 12:22:20 am »
The problem is that if I have some code in the onActivate event it will be executed again, in the embarcadero delphi ide I have no problem, I can change the date over and over again without any code that I have in onActivate being activated, without a doubt I don't like this behavior, it causes me a lot of trouble and wasted time. Return to Embarcadero IDE. Greetings.
« Last Edit: November 21, 2020, 12:31:51 am by greenmask »

Sieben

  • Full Member
  • ***
  • Posts: 196
Re: Problem with DateTimePicker
« Reply #4 on: November 21, 2020, 01:31:59 am »
There is a quick and dirty workaround: in DateEdit.OnCloseUp set a form flag FSuspend to true and reset it in Form.OnActivate. For example:

Code: Pascal  [Select][+][-]
  1. procedure TfrmTouchMain.edDateCloseUp(Sender: TObject);
  2. begin
  3.   FSuspended := True;
  4. end;
  5.  
  6. procedure TfrmTouchMain.FormActivate(Sender: TObject);
  7. begin
  8.   if not FSuspended then
  9.     // activation code here
  10.   FSuspended := False;
  11. end;

But I don't think this is correct behaviour since no other control on a form would trigger OnActivate when it's contents are altered. I'd suggest to file a bug report.
Lazarus 2.0.10, FPC 3.2.0, .deb install on Ubuntu Xenial 32 / Gtk2 / Unity7

greenmask

  • Newbie
  • Posts: 4
Re: Problem with DateTimePicker
« Reply #5 on: November 21, 2020, 03:10:15 am »
Sieben: Thank you very much for the code, but what is Fsuspended, is a variable boolean?  I think that DateTimePicker has a bug, in embarcadero delphi I can use the datetimepicker without problem. Respectfully lazarus does not outperform delphi 7 much less recent versions, however where can I report the bug?
« Last Edit: November 21, 2020, 03:32:11 am by greenmask »

howardpc

  • Hero Member
  • *****
  • Posts: 3612
Re: Problem with DateTimePicker
« Reply #6 on: November 21, 2020, 09:21:48 am »
I agree with Sieben: no changing control should trigger its parent form's OnActivate.
The Delphi control is primarily designed for Windows, and can fully rely on WinAPI calls.

The Lazarus control has to be fully cross-platform, and has resorted to the hack of using an activation message in an attempt to manage automatic change of focus. While this 'works' it has the undesired side effect you encountered, and I think will ultimately be regarded as a poor design decision.

I've never used the Lazarus TDateTimePicker, and now I see how it is implemented, I doubt I ever will use it.

lucamar

  • Hero Member
  • *****
  • Posts: 3455
Re: Problem with DateTimePicker
« Reply #7 on: November 21, 2020, 11:19:41 am »
Sieben: Thank you very much for the code, but what is Fsuspended, is a variable boolean?

I'm not Sieben (obviously :)) but the normal way of implementing those kind of things is as a field of the form, so in the interface section of your form's unit you'd have something like:

Code: Pascal  [Select][+][-]
  1. type
  2.   TfrmTouchMain = class(TForm)
  3.      {controls, etc, here}
  4.   private
  5.     FSuspend: Boolean;
  6.   public
  7.     {other things here}
  8.   end;

One small gotcha of those workarounds is that you should make sure the fields start with a known value, so you have to add an OnCreate event handler and initialize them:

Code: Pascal  [Select][+][-]
  1. procedure TfrmTouchMain.FormCreate(Sender: TObject);
  2. begin
  3.   FSuspend := False;
  4.   {... other initializations, if any ...}
  5. end;
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.10/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

wp

  • Hero Member
  • *****
  • Posts: 7966
Re: Problem with DateTimePicker
« Reply #8 on: November 21, 2020, 11:20:41 am »
if I have edit1.clear on event form OnActivate,  it's is executed.
I agree that triggering OnActivate by the DateTimePicker should be avoided (although I am not sure whether this is possible). But what you are saying here indicates bad design, don't clear an Edit control in OnActivate: When you open a second form OnActivate will be called, too, and erase your edit. The same will happen when your for contains another nonmodal form which you activate and you return to the starting form. Put initialization code into OnCreate, or call it from the routine which activated this form. Don't say "This does not apply to me" - who knows what you are doing in a year when you forgot all the details of this project?
« Last Edit: November 21, 2020, 11:24:44 am by wp »
Mainly Lazarus trunk / fpc 3.2.0 / all 32-bit on Win-10, but many more...

lucamar

  • Hero Member
  • *****
  • Posts: 3455
Re: Problem with DateTimePicker
« Reply #9 on: November 21, 2020, 11:39:21 am »
Put initialization code into OnCreate, [...]

One small problem with that approach is that at the time OnCreate is called not all controls are yet initialized so one has to defer further initializations (for example filling a TListBox) to OnActivate. For one time initializations one can add a boolean field called, say, DoneActivate or, if that's all the OnActivate handler does, simply start it with:

Code: Pascal  [Select][+][-]
  1. procedure TForm1.FormActivate(Sender: TObject);
  2. begin
  3.   OnActivate := Nil;
  4.   {... rest of deferred initializations ...}
  5. end;

That will prevent it being called more than once in the same run.
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.10/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

wp

  • Hero Member
  • *****
  • Posts: 7966
Re: Problem with DateTimePicker
« Reply #10 on: November 21, 2020, 11:45:36 am »
Agreed. I only wanted to emphasize that OnActivate can occur at any time, and the coder must be aware of that. When a program behaves incorrectly due to OnActivate being called this is not necessarily due to a bug of TDateTimePicker.
Mainly Lazarus trunk / fpc 3.2.0 / all 32-bit on Win-10, but many more...

Sieben

  • Full Member
  • ***
  • Posts: 196
Re: Problem with DateTimePicker
« Reply #11 on: November 21, 2020, 12:56:03 pm »
Quote from: lucamar
One small gotcha of those workarounds is that you should make sure the fields start with a known value, so you have to add an OnCreate event handler and initialize them:
In Delphi one could rely on any class vars to be initialized with zero, so explicitely initializing strings to '', integers to 0 or Booleans to false wasn't really necessary. Different with Lazarus...?
Lazarus 2.0.10, FPC 3.2.0, .deb install on Ubuntu Xenial 32 / Gtk2 / Unity7

lucamar

  • Hero Member
  • *****
  • Posts: 3455
Re: Problem with DateTimePicker
« Reply #12 on: November 21, 2020, 01:22:20 pm »
Quote from: lucamar
One small gotcha of those workarounds is that you should make sure the fields start with a known value, so you have to add an OnCreate event handler and initialize them:
In Delphi one could rely on any class vars to be initialized with zero, so explicitely initializing strings to '', integers to 0 or Booleans to false wasn't really necessary. Different with Lazarus...?

Yes, IIRC simple-type fields are initialized to its defaults, but it's nevertheless a good rule of thumb to make a explicit initialization. That way you can be absolutely sure of what each contains and, perhaps more important, you know exactly where to change that initial value if the need arises later on.

It may sound a little paranoid but, hey, even paranoids can have real enemies ;)
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.10/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

Zoran

  • Hero Member
  • *****
  • Posts: 1643
    • http://wiki.lazarus.freepascal.org/User:Zoran
Re: Problem with DateTimePicker
« Reply #13 on: November 21, 2020, 01:24:22 pm »
Would you please provide a minimal application which produces the bug?

I am the author and maintainer of TDateTimePicker and I would like to solve all bugs from the component.

However, I have to admit I do not understand what this is about. :-[
I am not a native English speaker.
I cannot see what has DateTimePicker to do with clearing of an edit box.

You also didn't provide all information about your system.
Which OS, widgetset.

So, test application please!
« Last Edit: November 21, 2020, 01:29:28 pm by Zoran »

Sieben

  • Full Member
  • ***
  • Posts: 196
Re: Problem with DateTimePicker
« Reply #14 on: November 21, 2020, 01:36:46 pm »
The 'bug' is simply that OnActivate of the parent form will be called after selecting a value from the calendar in dtkDate mode. This is at least unusual for a control that just changes value. Gtk2 here.
Lazarus 2.0.10, FPC 3.2.0, .deb install on Ubuntu Xenial 32 / Gtk2 / Unity7

 

TinyPortal © 2005-2018