Recent

Author Topic: need help with calender  (Read 49812 times)

shs

  • Sr. Member
  • ****
  • Posts: 310
need help with calender
« on: September 20, 2017, 06:52:15 am »
Hi i made this 'goal planner program' where i can save goals everyday. if i add goals with the date i select, it will be saved to the record. But when it's saved to the record, I made the goals to be added in the list box so when i double click it, i can see what date the goal is for what the name of the goal is and etc.

HOWEVER I want that to happen when i double click or click the day on calendar, but i do not know how to code for that to happen.

For more information please have a look at the attachment and compile it.

rvk

  • Hero Member
  • *****
  • Posts: 6163
Re: need help with calender
« Reply #1 on: September 20, 2017, 12:49:03 pm »
... so when i double click it, i can see what date the goal is for what the name of the goal is and etc.
HOWEVER I want that to happen when i double click or click the day on calendar...
You have made an event Calendar1DayChanged() but haven't put anything in it.

In there you should open your goalsfile and read all records (one by one) and display the ones that are on calendar1.date.
« Last Edit: September 20, 2017, 12:50:37 pm by rvk »

shs

  • Sr. Member
  • ****
  • Posts: 310
Re: need help with calender
« Reply #2 on: September 20, 2017, 01:11:13 pm »
Code: Pascal  [Select][+][-]
  1. procedure TForm1.Calendar1DayChanged(Sender: TObject);
  2. begin
  3. current_goals:=strtoint(calendar1.date);
  4.  
  5.  reset(goalsfile);
  6.  seek(goalsfile, current_goals);
  7.  read(goalsfile, info);
  8.  memo1.clear;
  9.  memo1.lines.add(info.name);
  10.  memo1.lines.add(inttostr(info.goal));
  11.  memo1.lines.add(info.date);
  12.  
  13.  closefile(goalsfile);
  14.  
  15. end;          

i tried this but when i compile it does not work

rvk

  • Hero Member
  • *****
  • Posts: 6163
Re: need help with calender
« Reply #3 on: September 20, 2017, 01:28:42 pm »
strtoint(calendar1.date); ???
Calendar1.Date is a string. You also saved a date-string to the file. So no need to convert it to an integer (which will fail).

(b.t.w. always mention any error messages or why it doesn't work. "Doesn't work" isn't really a good problem description.)

You try to seek() to current_goals (integer) which isn't filled.

Your goalsfile could look like this
12-09-2017 test1 1
13-09-2017 test2 1
13-09-2017 test3 1
14-09-2017 test4 1
14-09-2017 test5 1

(the goalnumer isn't really used when saving the goal because you always add to the end of the file)

So after the reset you should read ALL the records. That could mean you have multiple goals on one day (as you can see in my example above). You need to do a while not EOF(goalsfile) loop to read all those records. If a read records is the same as the chosen date (Calendar1.Date) then you can print out that record.

(Try to google some examples of reading a complete file of records. For example https://stackoverflow.com/a/5763698/1037511)

shs

  • Sr. Member
  • ****
  • Posts: 310
Re: need help with calender
« Reply #4 on: September 20, 2017, 01:42:55 pm »
strtoint(calendar1.date); ???
Calendar1.Date is a string. You also saved a date-string to the file. So no need to convert it to an integer (which will fail).

(b.t.w. always mention any error messages or why it doesn't work. "Doesn't work" isn't really a good problem description.)

You try to seek() to current_goals (integer) which isn't filled.

Your goalsfile could look like this
12-09-2017 test1 1
13-09-2017 test2 1
13-09-2017 test3 1
14-09-2017 test4 1
14-09-2017 test5 1

(the goalnumer isn't really used when saving the goal because you always add to the end of the file)

So after the reset you should read ALL the records. That could mean you have multiple goals on one day (as you can see in my example above). You need to do a while not EOF(goalsfile) loop to read all those records. If a read records is the same as the chosen date (Calendar1.Date) then you can print out that record.

(Try to google some examples of reading a complete file of records. For example https://stackoverflow.com/a/5763698/1037511)


okay thank you so much. but how to code this?
Code: Pascal  [Select][+][-]
  1. If a read records is the same as the chosen date (Calendar1.Date) then you can print out that record.


rvk

  • Hero Member
  • *****
  • Posts: 6163
Re: need help with calender
« Reply #5 on: September 20, 2017, 01:46:07 pm »
okay thank you so much. but how to code this?
Code: Pascal  [Select][+][-]
  1. If a read records is the same as the chosen date (Calendar1.Date) then you can print out that record.
You read every record into info.

You do know how to check if Calendar1.Date is the same as info.date?
"If" they are the same then do your memo1.lines.add().

Because you can have multiple goals on the same day you should do the Memo1.Clear; above the while loop.
And inside the while loop you check for the data (like stated above) and do the Memo1.Lines.Add() if the dates are the same.

shs

  • Sr. Member
  • ****
  • Posts: 310
Re: need help with calender
« Reply #6 on: September 20, 2017, 01:58:38 pm »
when i compile it it says tdate not found and my form1 is gone now. can you please help me coding? i dont want to lose my work again

rvk

  • Hero Member
  • *****
  • Posts: 6163
Re: need help with calender
« Reply #7 on: September 20, 2017, 02:05:14 pm »
when i compile it it says tdate not found and my form1 is gone now. can you please help me coding? i dont want to lose my work again
tdate?

I didn't see any tdate in your code.

What do you have now and what error-message do you get exactly.

I can "help" you code the thing but I won't code it for you.

shs

  • Sr. Member
  • ****
  • Posts: 310
Re: need help with calender
« Reply #8 on: September 20, 2017, 02:08:22 pm »
i added teditdate after this
Code: Pascal  [Select][+][-]
  1. procedure TForm1.Calendar1DayChanged(Sender: TObject);
  2. begin
  3. current_goals:=strtoint(calendar1.date);
  4.  
  5.  reset(goalsfile);
  6.  seek(goalsfile, current_goals);
  7.  read(goalsfile, info);
  8.  memo1.clear;
  9.  memo1.lines.add(info.name);
  10.  memo1.lines.add(inttostr(info.goal));
  11.  memo1.lines.add(info.date);
  12.  
  13.  closefile(goalsfile);
  14.  
  15. end;          

i tried this but when i compile it does not work

and it didn't work so i deleted and did what you told me to do but when i compiled it it said the tedit was not found and the lazarus suddenly turned off and when i tried to open it again there was a blank form 1

rvk

  • Hero Member
  • *****
  • Posts: 6163
Re: need help with calender
« Reply #9 on: September 20, 2017, 02:15:12 pm »
i added teditdate after this
Not sure where I told you to add a TEditDate.

You mentioned you wanted to click the calendar and then want the goals for that day displayed in the TMemo.

Quote
and it didn't work so i deleted and did what you told me to do but when i compiled it it said the tedit was not found and the lazarus suddenly turned off and when i tried to open it again there was a blank form 1
So, take your code from your backup (or take it from the code already posted here) and make the changes again as I suggested.

If you get stuck show us that code (and error message).


shs

  • Sr. Member
  • ****
  • Posts: 310
Re: need help with calender
« Reply #10 on: September 20, 2017, 02:21:14 pm »
Code: Pascal  [Select][+][-]
  1. procedure TForm1.Calendar1DayChanged(Sender: TObject);
  2. begin
  3.   while not EOF(goalsfile) do
  4.     begin
  5.       if calendar1.date = info.date then
  6.       begin
  7.       reset(goalsfile);
  8.       seek(goalsfile, filesize(goalsfile));
  9.       read(goalsfile, info);
  10.       memo1.lines.add(info.name);
  11.       memo1.lines.add(inttostr(info.goal));
  12.       memo1.lines.add(info.date);
  13.       end;
  14.        closefile(goalsfile);
  15.  end;
  16.  
  17. end;      

i compiled this and the error message came up

project raised exception class 'RunError(103)'

rvk

  • Hero Member
  • *****
  • Posts: 6163
Re: need help with calender
« Reply #11 on: September 20, 2017, 02:23:31 pm »
You need to open the file first before you do the while loop. (that's why you get the 103 error)

In the while loop you don't need seek() because you want to read ALL the records. So just read(goalsfile, info) is enhough.

And last, now you add ALL the goals into the memo. You only wanted the records where Calendar1.Date = info.Date. So you need to check with an if-statement if that's the case and only do the memo1.lines.add() inside that if statement.

shs

  • Sr. Member
  • ****
  • Posts: 310
Re: need help with calender
« Reply #12 on: September 20, 2017, 02:28:43 pm »
You need to open the file first before you do the while loop. (that's why you get the 103 error)

In the while loop you don't need seek() because you want to read ALL the records. So just read(goalsfile, info) is enhough.

And last, now you add ALL the goals into the memo. You only wanted the records where Calendar1.Date = info.Date. So you need to check with an if-statement if that's the case and only do the memo1.lines.add() inside that if statement.


how to open the file? is it assignfile() like that? i tried that one but i still got the error message. and what is EOF(goalsfile)? what does it do?

shs

  • Sr. Member
  • ****
  • Posts: 310
Re: need help with calender
« Reply #13 on: September 20, 2017, 02:31:20 pm »
oh btw i get that error message when i click on the calendar

rvk

  • Hero Member
  • *****
  • Posts: 6163
Re: need help with calender
« Reply #14 on: September 20, 2017, 02:41:44 pm »
how to open the file?
Who programmed the code you already have???

You shouldn't use reset(goalsfile) inside the loop but above the while-loop.
That opens the file with records. You do need to use assignfile() first but you already did that in FormCreate().
I'm not sure why you don't know that because you used it elsewhere in your code already multiple times.

Quote
and what is EOF(goalsfile)? what does it do?
Type lazarus eof file in Google.
You end up here: https://www.freepascal.org/docs-html/rtl/system/eof.html

Quote
Eof returns True if the file-pointer has reached the end of the file, or if the file is empty. In all other cases Eof returns False.
So you can use that to loop through all the records until you reach the end of the file.

 

TinyPortal © 2005-2018