Lazarus

Programming => LCL => Topic started by: Jvan on May 08, 2021, 01:31:20 am

Title: TDateTimePicker: Setting Min and Max Date
Post by: Jvan on May 08, 2021, 01:31:20 am
This is my code:

Code: Pascal  [Select][+][-]
  1. procedure TfrmMain._MinMaxFechaContable();
  2. var
  3.   anho, mes: Integer;
  4.   myDtp: TDateTimePicker;
  5. begin
  6.   anho := 2000 + cmbPerContAnho.ItemIndex;
  7.   mes := cmbPerContMes.ItemIndex;
  8.   myDtp := frmAstCont.dtpAstFchCont;
  9.   if (mes = 0) then begin
  10.     //El 1er día del mes seleccionado:
  11.     myDtp.MinDate := EncodeDate(anho, 1, 1);
  12.     //El 31 de diciembre del año seleccionado:
  13.     myDtp.MaxDate := EncodeDate(anho, 12, 31);
  14.   end else begin
  15.     //El 1er día del mes seleccionado:
  16.     myDtp.MinDate := EncodeDate(anho, mes, 1);
  17.     //El mes siguiente menos 1 día:
  18.     myDtp.MaxDate := IncMonth(myDtp.MinDate, 1) - 1.0;
  19.   end;
  20.  
  21. end;
  22.  

But I had this error showed in "error1.png".

Title: Re: TDateTimePicker: Setting Min and Max Date
Post by: lucamar on May 08, 2021, 01:53:47 am
Does frmAstCont (which I assume is a form) exist and does it contain a (TDateTimePicker?) dtpAstFchCont control?
Title: Re: TDateTimePicker: Setting Min and Max Date
Post by: speter on May 08, 2021, 02:07:08 am
In addition to lucamar's comments, do "cmbPerContAnho" and "cmbPerContMes" exist (have they been created)!?

cheers
S.
Title: Re: TDateTimePicker: Setting Min and Max Date
Post by: Jvan on May 08, 2021, 04:05:49 pm
Yes, they exist. I can see them, so they exist.

"cmbPerContAnho" and "cmbPerContMes" are on the main form. And "dtpAstFchCont" in on a ShowModal form.
Title: Re: TDateTimePicker: Setting Min and Max Date
Post by: wp on May 08, 2021, 04:29:46 pm
Maybe the form frmAstCont does not yet exist when your code is called? Is it in the list of auto-created forms (in the project options)? Or do you create it at run-time? Maybe you forgot to create it? It's hard to tell without seeing all of your code...

In general: Put a breakpoint into the procedure which raises the exception. When the program stops open "View" > "Debug windows" > "call stack" -- this shows you from which routine the breakpoint procedure is called, and the routine from which this other routine is called etc. Study these lines and check whether the form already exists when they are called.
Title: Re: TDateTimePicker: Setting Min and Max Date
Post by: Jvan on May 08, 2021, 06:21:42 pm
I created a new project with my code. I had the same problem.
Title: Re: TDateTimePicker: Setting Min and Max Date
Post by: wp on May 08, 2021, 07:00:52 pm
As lucamar and I told you...

You access frmAstCont in the OnCreate event of Form1. Looking at the project file you will see that at this time the frmAstCont is not yet created and thus nil.

You can use the OnShow or OnActivate events instead. On the other hand, these events may be fired later during execution of the program again. To prevent unwanted side-effects when this happens, I often implement a method "BeforeRun" of the mainform which is called in the project unit immediately before Application.Run.
Code: Pascal  [Select][+][-]
  1. type
  2.   TForm1 = class(TForm)
  3.   ...
  4.   public
  5.     procedure BeforeRun;
  6.     ...
  7.   end;
  8.  
  9. procedure TForm1.BeforeRun;
  10. begin
  11.   _MinMaxFechaContable;
  12. end;
  13.  
  14. ----------
  15.  
  16. program Project;
  17. ...
  18. begin
  19.   ....
  20.   Application.Initialize;
  21.   Application.CreateForm(TForm1, Form1);
  22.   Application.CreateForm(TfrmAstCont, frmAstCont);
  23.   Form1.BeforeRun;
  24.   Application.Run;
  25. end.  

This way the crash due to nonexisting frmAstConCont is prevented. But as you'll notice there is another problem with your comboboxes - I'll leave this to you to find a solution...
Title: Re: TDateTimePicker: Setting Min and Max Date
Post by: Jvan on May 08, 2021, 09:19:33 pm
Thanks.
TinyPortal © 2005-2018