Lazarus

Programming => Packages and Libraries => Topic started by: Nicole on July 21, 2022, 03:11:54 pm

Title: [solved] tvPlanit - how to create an event by code? - Troubelshooting
Post by: Nicole on July 21, 2022, 03:11:54 pm
I took the code below out of the example which says "add a random date to the monthview".
I tried to do the same trick by using just an ordinary button.
My problem is the add line.
Called from the Month View it probalby "knows" something.
Called from a button out of th blue, it does not.
What I need is something like:
Code: Pascal  [Select][+][-]
  1. "mydatastore.add(newEvent);"

Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. const
  3.   cat = 0;
  4. var
  5.   t1, t2: TDateTime;
  6.   txt: String;
  7. begin
  8.   // Get random start time of the event on the selected day in the weekview
  9.   t1 := trunc(VpWeekView1.Date) + EncodeTime(random(23), Random(60), 0, 0);
  10.   // Get random end time of the event, event duration less than 1 hour.
  11.   t2 := t1 + EncodeTime(0, Random(60), 0, 0);
  12.   // Get event caption
  13.   txt := 'Event' + IntToStr(VpIniDatastore1.Resource.Schedule.EventCount);
  14.   // Add the event
  15. ======>  AddEvent(t1, t2, txt, cat);
  16. end;
Title: Re: tvPlanit - how to create an event by code? - Troubelshooting
Post by: wp on July 21, 2022, 06:12:02 pm
You must do the "AddEvent" in several steps (like in the TForm1.AddEvent method of the sample) - I know it's a bit counter-intuitive (it's not my code...):
Code: Pascal  [Select][+][-]
  1. procedure TForm1.AddEvent(AStartTime, AEndTime: TDateTime;
  2.   ACaption: String; ACategory: Integer);
  3. var
  4.   event: TVpEvent;
  5.   id: Integer;
  6. begin
  7.   // Get the ID of the new event from the datastore
  8.   id := VpIniDataStore1.GetNextID('Events');
  9.   // Add the event...
  10.   event := VpIniDatastore1.Resource.Schedule.AddEvent(id, AStartTime, AEndTime);
  11.   // ... and specify some properties
  12.   event.Description := ACaption;
  13.   event.Category := ACategory;
  14.   event.Changed := true;
  15.   // Save the event in the datastore
  16.   VpIniDatastore1.PostEvents;
  17. end
Title: Re: tvPlanit - how to create an event by code? - Troubelshooting
Post by: Nicole on July 25, 2022, 06:06:18 pm
I am afraid, I failed.
I tried this:

I called the method by this attempts:

Code: Pascal  [Select][+][-]
  1. TvPlanItFrame.neuesEvent(now,now + 2,'loesch',0);

or this
Code: Pascal  [Select][+][-]
  1. TvPlanItFrame.neuesEvent(now,now,'test',1);  


the result is this:
[Window Title]
Fehler

[Content]
Projekt project_Notizen hat Exception-Klasse »External: SIGSEGV« ausgelöst.

 In Datei 'source\vpdata.pas' in Zeile 1088:
if FSchedule = nil then

[Ok]


what was called is this method


Code: Pascal  [Select][+][-]
  1. procedure TTvPlanItFrame.neuesEvent(AStartTime, AEndTime: TDateTime;
  2.   ACaption: String; ACategory: Integer);
  3. var
  4.   event: TVpEvent;
  5.   id: Integer;
  6. begin
  7.   // Get the ID of the new event from the datastore
  8.   id := VpIniDataStore_Quelle.GetNextID('Events');
  9. //  id := self.VpIniDatastore_myLinkDazu.get.GetNextID('Events');
  10.   // Add the event...
  11.   event := VpIniDatastore_Quelle.Resource.Schedule.AddEvent(id, AStartTime, AEndTime);
  12.   // ... and specify some properties
  13.   event.Description := ACaption;
  14.   event.Category := ACategory;
  15.   event.Changed := true;
  16.   // Save the event in the datastore
  17.   VpIniDatastore_Quelle.PostEvents;
  18. end;
  19.  
  20.  

the problem is said to be here
vpdate, line 1088

Code: Pascal  [Select][+][-]
  1. function TVpResource.GetSchedule: TVpSchedule;
  2. begin
  3.   if FSchedule = nil then
  4.     FSchedule := TVpSchedule.Create(self);
  5.   result := FSchedule;
  6. end;
Title: Re: tvPlanit - how to create an event by code? - Troubelshooting
Post by: wp on July 25, 2022, 08:01:38 pm
What is VpIniDatastore_Quelle.Resource? When your application starts it does not automatically select an event. My sample codes contain a few lines to do this:
Code: Pascal  [Select][+][-]
  1. // Load the last resource.
  2. procedure TForm1.FormCreate(Sender: TObject);
  3. var
  4.   lastRes: TVpResource;
  5.   datastore: TVpCustomDatastore;
  6. begin
  7.   datastore := VpControlLink1.Datastore;
  8.   if datastore.Resources.Count > 0 then
  9.   begin
  10.     lastRes := datastore.Resources.Items[datastore.Resources.Count-1];
  11.     datastore.Resource := lastRes;
  12.   end;
  13. end;
Title: [solved] Re: tvPlanit - how to create an event by code? - Troubelshooting
Post by: Nicole on July 26, 2022, 11:30:22 am
Thank you so much, it worked at sudden.
I did not change anything in my code. I just created a new ressource and tried my "add" button again: worked at sudden.

What I like most "now - now"
"TvPlanItFrame.neuesEvent(now,now,'test',1); "

If somebody does not know the background, he will not understand my joy about "now-now" giving reasonable results. The first now is the start, the second now is the end of the event. If this is accepted, this means, I have not to mess around in "creating" an artificial end-date, in the case, I just have a start date, what is the rule, not the exception.

Now I am going to implement the smarter solution

" if datastore.Resources.Count > 0 then...."





TinyPortal © 2005-2018