Recent

Author Topic: [SOLVED] CalendarDialog Date  (Read 11160 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: 4144
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

  • Jr. Member
  • **
  • Posts: 96
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

  • New Member
  • *
  • Posts: 30
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

  • New Member
  • *
  • Posts: 30
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: 4144
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

  • New Member
  • *
  • Posts: 30
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: 1826
    • 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

  • New Member
  • *
  • Posts: 30
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?

software@vasilko.eu

  • Newbie
  • Posts: 1
Re: [SOLVED] CalendarDialog Date
« Reply #10 on: November 17, 2017, 10:04:31 am »
Hi,

There is a bug in TCalendar component.

I have tried this Calendar Dialog on Mac OS X and it once showed the current month (and no day selected) and once it showed correctly the given date (shown year, month and selected the day). The problem - it has the invernal variable shich is used incorrectly for this behavior of TCalendar usage and I have solved the problem:

in procedure TCustomCalendar.SetDateTime(const AValue: TDateTime); comment of the first line: //if AValue=FDate then exit;   

This way TCalendar will ALWAYS set the given date no matter if it is the same as the internal date stored in the variable FDate.

There are many bugs in the components but we have the source code so we can solve anything...

======================
After the modification it is like this:

procedure TCustomCalendar.SetDateTime(const AValue: TDateTime);
{$IFDEF WINDOWS}
var
  CalendarMinDate,CalendarMaxDate: integer;
{$ENDIF}
begin
  //if AValue=FDate then exit;   

Arion58

  • New Member
  • *
  • Posts: 30
Re: [SOLVED] CalendarDialog Date
« Reply #11 on: November 22, 2017, 09:38:01 pm »
Hi Vasili,
thank you for answering after such a long time. Actually, I have the previously proposed solution working for a while, and at this moment I don't have the time to try yours. But I will, as soon as possible.

 

TinyPortal © 2005-2018