* * *

Author Topic: [SOLVED] CalendarDialog Date  (Read 3743 times)

Pascaluvr

  • Full Member
  • ***
  • Posts: 216
[SOLVED] CalendarDialog Date
« on: March 14, 2012, 10:48:16 pm »
When a CalendarDialog is first displayed the default selection is in a field (.Date) that can be set when creating the application (or defaults to the date that TCalendarDialog) was added to the form.

Is there a way to set this to Today's Date at run time?

I tried setting it with the OnShow Event but this didn't have any effect.

Any ideas would be much appreciated.

Thanks in advance.
« Last Edit: March 15, 2012, 01:38:41 am by Pascaluvr »
Windows 7, Lazarus 1.0.8, FPC 2.6.2, SQLite 3

howardpc

  • Hero Member
  • *****
  • Posts: 2096
Re: CalendarDialog Date
« Reply #1 on: March 14, 2012, 11:40:13 pm »
Set the Date property before showing the dialog.

CalendarDialog1.Date := Now;
  if   CalendarDialog1.Execute then ...

Pascaluvr

  • Full Member
  • ***
  • Posts: 216
Re: CalendarDialog Date
« Reply #2 on: March 15, 2012, 01:38:11 am »
Thank you Howard.

Obvious really :) I just had some sort of mental block that it wasn't created yet and so couldn't do the assignment at this stage :(

Thanks again
Windows 7, Lazarus 1.0.8, FPC 2.6.2, SQLite 3

han

  • New member
  • *
  • Posts: 8
Re: [NOT SOLVED] CalendarDialog Date
« Reply #3 on: March 02, 2017, 01:03:28 pm »
This doesn't work currently for FPC 3.0.0. It is always showing current date. For my application it would be nice to change the date.

Example:
  CalendarDialog1.Date :=encodedate(2000,1,1);
  if calendardialog1.Execute then
 

The date is written and can be read back, but the calendar position is not updated. Anybody an idea? Maybe I should use Tdatetimepicker instead.

Later: It works for Tdatetimepicker. Will use that one.

Han
« Last Edit: March 02, 2017, 04:49:47 pm by han »

Arion58

  • Newbie
  • Posts: 4
Re: [SOLVED] CalendarDialog Date
« Reply #4 on: March 06, 2017, 04:39:13 pm »
@han
I am struggling with exactly the same problem. And I have noticed something even more weird: WHen I assign just a number to the DateTime value of the calendar befor first showing it, then the active field of the the calendar is set to the according date, but only the first time. And if I assign a variable (which I pass to the routine) it doesn't work either the first time. I can't imagine what happens, and I can't imagine this is the desired behaviour. What am I toing wrong?
I attach a very small module just to show the code I am speaking of.

Arion58

  • Newbie
  • Posts: 4
Re: [SOLVED] CalendarDialog Date
« Reply #5 on: March 06, 2017, 05:09:42 pm »
I am new 0n this forum. I think it had been better to add the code directly in the text, so everybody can read it:
Code: Pascal  [Select]
  1. unit CalDialog;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, Calendar;
  9.  
  10. type
  11.  
  12.   { TCalDialogForm }
  13.  
  14.   TCalDialogForm = class(TForm)
  15.     MyCalendar: TCalendar;
  16.   private
  17.     { private declarations }
  18.   public
  19.     { public declarations }
  20.     procedure ShowCalendar (var Date:TDateTime);
  21.   end;
  22.  
  23. var
  24.   CalDialogForm: TCalDialogForm;
  25.  
  26. implementation
  27.  
  28. {$R *.lfm}
  29.  
  30. uses LclType, crt;
  31.  
  32. { TCalDialogForm }
  33.  
  34. procedure TCalDialogForm.ShowCalendar (var Date:TDateTime);
  35. begin
  36.   //MyCalendar.DateTime:= Date;
  37.   MyCalendar.DateTime:= 42788;
  38.   ShowModal;
  39.   Date:= MyCalendar.DateTime;
  40. end;
  41.  
  42. end.
  43.  
« Last Edit: March 07, 2017, 12:39:05 pm by Arion58 »

howardpc

  • Hero Member
  • *****
  • Posts: 2096
Re: [SOLVED] CalendarDialog Date
« Reply #6 on: March 06, 2017, 08:26:23 pm »
I think you may be looking for a modal calendar dialog, such as in the attached basic project.

Arion58

  • Newbie
  • Posts: 4
Re: [SOLVED] CalendarDialog Date
« Reply #7 on: March 07, 2017, 11:49:54 am »
Thank you for answering. I couldnt test yet, because the compiler complains "Error: Can't open resource file". And, actually, Lazarus is frozen. This happens often, I don't know if others have this problem. I run Lazarus 1.6.2 on Linux.
I will try your solution as soon as I have time for it.

Zoran

  • Hero Member
  • *****
  • Posts: 1103
    • http://wiki.lazarus.freepascal.org/User:Zoran
Re: [SOLVED] CalendarDialog Date
« Reply #8 on: March 07, 2017, 12:22:52 pm »
I am new 0n this forum. I think it had been better to add the code directly in the text, so everybody can read it:

First, welcome to our forum.
Yes, and use code tags (see here), so your code looks like this:

Code: Pascal  [Select]
  1. unit CalDialog;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, Calendar;
  9.  
  10. type
  11.  
  12.   { TCalDialogForm }
  13.  
  14.   TCalDialogForm = class(TForm)
  15.     MyCalendar: TCalendar;
  16.   private
  17.     { private declarations }
  18.   public
  19.     { public declarations }
  20.     procedure ShowCalendar (var Date:TDateTime);
  21.   end;
  22.  
  23. var
  24.   CalDialogForm: TCalDialogForm;
  25.  
  26. implementation
  27.  
  28. {$R *.lfm}
  29.  
  30. uses LclType, crt;
  31.  
  32. { TCalDialogForm }
  33.  
  34. procedure TCalDialogForm.ShowCalendar (var Date:TDateTime);
  35. begin
  36.   //MyCalendar.DateTime:= Date;
  37.   MyCalendar.DateTime:= 42788;
  38.   ShowModal;
  39.   Date:= MyCalendar.DateTime;
  40. end;
  41.  
  42. end.
  43.  

Arion58

  • Newbie
  • Posts: 4
Re: [SOLVED] CalendarDialog Date
« Reply #9 on: March 07, 2017, 12:30:40 pm »
I tried the solution of howardpc, it works as desired. But I am confused: Why doesn't mine? I dont see big differences in the code. I have modified the code of howardpc to have just one var-parameter to the ShowCalendar-function, and it still works. What is wrong in my code?

 

Recent

Get Lazarus at SourceForge.net. Fast, secure and Free Open Source software downloads Open Hub project report for Lazarus