Recent

Author Topic: Objects and stringlists  (Read 681 times)

BLL

  • Sr. Member
  • ****
  • Posts: 276
Objects and stringlists
« on: November 20, 2019, 08:02:48 pm »
Hi
I would like some advice please.
I am writing an alarm clock program.
At the  moment, I save the date/times in a sorted TListBox, so multiple alarms are in date/time order.
They are saved using SaveToFile and loaded using LoadForFile.
Each time an alarm occurs, it is deleted from the list.
When setting an alarm, I would like to be able to choose single, repeat daily or repeat weekly.
I thought of usingan  object added to each string item, but as I understand it, objects are not saved/loaded using SaveToFile/LoadFromFile!!!!! How do you do that?
Is there a better way of achieving this?

In confusion.

Brian

winni

  • Hero Member
  • *****
  • Posts: 578
Re: Objects and stringlists
« Reply #1 on: November 20, 2019, 08:13:55 pm »
Hi!

I wrote  once a little Alam Clock and saved all dates in a StringList but used the csv logic for every line:

date | time | repeat | reason

You split this one StringList.item in 4 strings and convert them into the needed type.

So you don't have to care abjout saving objects.

Winni

jamie

  • Hero Member
  • *****
  • Posts: 2159
Re: Objects and stringlists
« Reply #2 on: November 20, 2019, 10:47:27 pm »
The best thing to do is encode a string of each entry in the list box..

for example

01/19/2020=Once, Repeat, OneAWeek, etc;


you when reading the selection you can get what ever is after = as parameters for that date.

 if you don't want to see this in the list box but would rather be able to encode this string in a different part of the app, you can use the OwnerDraw Option of the Listbox so that you can draw out only the Date and not display the rest of it, but it will still be there. This is just for cosmetics.

 Since a ListBox constains a Stringlist, you can use the Values array to get the values of each entry. The Values come after the "="

 Then you can split the line up using Split if you wish to break the commands in different lines.

 More later if you are interested.
Number 1 at blue screen app creations!

Zvoni

  • Sr. Member
  • ****
  • Posts: 299
Re: Objects and stringlists
« Reply #3 on: November 21, 2019, 09:16:51 am »
The best thing to do is encode a string of each entry in the list box..

for example

01/19/2020=Once, Repeat, OneAWeek, etc;


you when reading the selection you can get what ever is after = as parameters for that date.

 if you don't want to see this in the list box but would rather be able to encode this string in a different part of the app, you can use the OwnerDraw Option of the Listbox so that you can draw out only the Date and not display the rest of it, but it will still be there. This is just for cosmetics.

 Since a ListBox constains a Stringlist, you can use the Values array to get the values of each entry. The Values come after the "="

 Then you can split the line up using Split if you wish to break the commands in different lines.

 More later if you are interested.

Or just use the second Argument of AddItem.
https://lazarus-ccr.sourceforge.io/docs/lcl/stdctrls/tcustomlistbox.additem.html
Code: Pascal  [Select]
  1.  public procedure TCustomListBox.AddItem(
  2.  
  3.   const Item: string;
  4.  
  5.   AnObject: TObject
  6.  
  7. );
Split your string to get your Date/Time and the Parameters.
Use "Item" for Date/Time, use AnObject for Parameters
One System to rule them all, One IDE to find them,
One Code to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
People call me crazy, because i'm jumping out of perfectly fine aircrafts

BLL

  • Sr. Member
  • ****
  • Posts: 276
Re: Objects and stringlists
« Reply #4 on: November 21, 2019, 11:09:35 am »
Hi both
Thanks for the replies. I prefer the second approach. So, from your example, do I need to instantiate a TCustomListbox to do this?

Both these approaches are completely new to me.

Thanks

Brian

Zvoni

  • Sr. Member
  • ****
  • Posts: 299
Re: Objects and stringlists
« Reply #5 on: November 21, 2019, 11:19:22 am »
No, TListBox inherits from TCustomListBox.
AddItem is the same for TListBox.
THIS AIRCODE:
Code: Pascal  [Select]
  1. Var
  2.    s:String;
  3.    a:Array Of String
  4. Begin
  5.    s:='01/19/2020 08:00=parWeekly';
  6.    a:=s.Split('=');
  7.    ListBox.AddItem(a[0], TObject(a[1]));
  8. end;
  9.  
Selecting now an entry in your listbox (or in your case it reaches the time to trigger some action) you can read the additional Data back.
« Last Edit: November 21, 2019, 11:21:21 am by Zvoni »
One System to rule them all, One IDE to find them,
One Code to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
People call me crazy, because i'm jumping out of perfectly fine aircrafts

BLL

  • Sr. Member
  • ****
  • Posts: 276
Re: Objects and stringlists
« Reply #6 on: November 21, 2019, 04:50:48 pm »
Hi Zvoni
Thank you for your help - I am gradually getting a grip on this.
I am now adding an object, as well as the time/date string to my Listbox, using your code.
I notice that if I look at the saved alarm file, it shows just the time/date string and the program works fine, so is the object associated with each string being saved - it appears not (the part follwing =)?
If so, can I just use split again to get at each part?

Brian

wp

  • Hero Member
  • *****
  • Posts: 6462
Re: Objects and stringlists
« Reply #7 on: November 21, 2019, 04:56:39 pm »
Code: Pascal  [Select]
  1. Var
  2.    s:String;
  3.    a:Array Of String
  4. Begin
  5.    s:='01/19/2020 08:00=parWeekly';
  6.    a:=s.Split('=');
  7.    ListBox.AddItem(a[0], TObject(a[1]));
  8. end;
  9.  
This looks dangerous: "a" is a local array of string and is destroyed automatically when this function/procedure is exited. Therefore the string stored in the Object of the listbox item will no longer exist.
Lazarus trunk / fpc 3.0.4 / all 32-bit on Win-10

Zvoni

  • Sr. Member
  • ****
  • Posts: 299
Re: Objects and stringlists
« Reply #8 on: November 21, 2019, 05:10:30 pm »
wp, i know that.
as i wrote: This is Aircode!
Just to show how it might be done
One System to rule them all, One IDE to find them,
One Code to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
People call me crazy, because i'm jumping out of perfectly fine aircrafts

BLL

  • Sr. Member
  • ****
  • Posts: 276
Re: Objects and stringlists
« Reply #9 on: November 21, 2019, 07:05:23 pm »
Hi both
Now I am more confused than ever and no closer to achieving my goal!

Brian

mangakissa

  • Hero Member
  • *****
  • Posts: 948
Re: Objects and stringlists
« Reply #10 on: November 21, 2019, 08:54:01 pm »
Very old fashion, but works as it should be.
Code: Pascal  [Select]
  1. interface
  2.  
  3. type TAlarmItem = class
  4.   timestring : string;
  5.   alarmtype  : string;  
  6. end;
  7.  
  8. implenmentation
  9.  
  10. Var
  11.    s:String;
  12.    a:Array Of String
  13.    AlarmItem : TAlarmItem;
  14. Begin
  15.    AlarmItem := TAlarmItem.create;
  16.    s:='01/19/2020 08:00=parWeekly';
  17.    a:=s.Split('=');
  18.    AlarmItem.timestring := a[0];
  19.    AlarmItem.alarmtype := a[1];
  20.    ListBox.AddObjectItem(a[0], AlarmItem);
  21. end;
  22.  
create a precedure to all an object
Code: Pascal  [Select]
  1. procedure GetObject(aItem : TStrings; aIndex : integer);
  2. var AlarmItem : TAlarmItem;
  3. begin
  4.   AlarmItem := TAlarmItem(aItem.object[aindex]);
  5.   //do something with the object  
  6. end;
  7.  
for each item you can call the object
Code: Pascal  [Select]
  1. GetObject(listbox.items, listbox.itemindex);
  2.  
Lazarus 1.84 (32b) / FPC 3.0.4
Windows 10

BLL

  • Sr. Member
  • ****
  • Posts: 276
Re: Objects and stringlists
« Reply #11 on: November 21, 2019, 09:03:17 pm »
Hi Mangakissa, your post overlapped mine. I will try to understand it - thanks;
Following my "confused" post, can I respectfully say that:
1). I am very grateful for any constructive help.
2). I am NOT a professional programmer, but a mere hobbyist?
3). I have recently come to linux & free pascal from CBuilder under Windows?
4). I am 68 years old and perhaps my brain is not as sharp as in my younger days?
5). My qualification is in Electronic Engineering and not in Computer Programming?
6). I did spend 30 years as a teacher, before I retired. Had I wished to give my students an example of something I was teaching them, then it would have been a fully working example, so that they could replicate it, play with it and try to modify it. I would not have given them an "aircode" solution that dod not work!! Perhaps "aircode" is fine for experienced programnmers, who would see limitations or shortcomings, but I am not in that category! Would that I were!!
7). I am trying very hard to learn how to use objects with listboxes, so I can improve my knowledge, but I need a working example of how to add a date/time group and single/daily/weekly parameter to my Listbox, save it, recall it and retrieve both the date/time value and the single,daily or weekly value.
At the moment, I can't do this.

Brian

winni

  • Hero Member
  • *****
  • Posts: 578
Re: Objects and stringlists
« Reply #12 on: November 21, 2019, 11:01:06 pm »
Hi!

Some stuff to read about StringLists and objects:

* The free pascal tutorial: https://wiki.freepascal.org/TStringList-TStrings_Tutorial

* The Delphi example: http://docs.embarcadero.com/products/rad_studio/delphiAndcpp2009/HelpUpdate2/EN/html/delphivclwin32/Classes_TStrings_Objects.html

* Nice discussion on stackoverflow: https://stackoverflow.com/questions/8947400/tstringlists-addobject-method

The combobox somewhere mentioned in the examples also contains a stringlist!

Winni


jamie

  • Hero Member
  • *****
  • Posts: 2159
Re: Objects and stringlists
« Reply #13 on: November 21, 2019, 11:21:43 pm »
This is the code I was talking about on how to show the strings in a list box
here is a simple project to show how..

 This will split the string and show only the left side from the "=" but the full contents of the string is stored in the listbox so is simple to load and save it …

 this shows how you can limit the view and split the values
« Last Edit: November 21, 2019, 11:24:12 pm by jamie »
Number 1 at blue screen app creations!

BLL

  • Sr. Member
  • ****
  • Posts: 276
Re: Objects and stringlists
« Reply #14 on: November 22, 2019, 05:41:33 pm »
Hello Jamie
Thank you very much for taking the trouble to put together project1. I was able to run it, study it and make alterations to it. I now understand how to store multiple items in a listbox!
I have now edited my clock program and it is working well!

Thanks again.

Brian