Recent

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

shs

  • Sr. Member
  • ****
  • Posts: 310
Re: need help with calender
« Reply #15 on: September 20, 2017, 02:47:45 pm »
oh okay but still when i click on the calender i get the error message i don't know why

rvk

  • Hero Member
  • *****
  • Posts: 6163
Re: need help with calender
« Reply #16 on: September 20, 2017, 02:49:57 pm »
oh okay but still when i click on the calender i get the error message i don't know why
Did you change the code like I suggested?

Without you showing your code I can't help you with that code.

shs

  • Sr. Member
  • ****
  • Posts: 310
Re: need help with calender
« Reply #17 on: September 20, 2017, 02:53:44 pm »
yes
Code: Pascal  [Select][+][-]
  1. procedure TForm1.Calendar1DayChanged(Sender: TObject);
  2. begin
  3.   reset(goalsfile);
  4.    read(goalsfile, info);
  5.   while not EOF(goalsfile) do
  6.     begin
  7.       if calendar1.date = info.date then
  8.       begin
  9.  
  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. end;          

rvk

  • Hero Member
  • *****
  • Posts: 6163
Re: need help with calender
« Reply #18 on: September 20, 2017, 03:01:37 pm »
You should always indent the lines correctly.
If I do that for your code I get:
Code: Pascal  [Select][+][-]
  1. procedure TForm1.Calendar1DayChanged(Sender: TObject);
  2. begin
  3.   reset(goalsfile);
  4.   Read(goalsfile, info); // this should go inside the while loop so all records are read
  5.   while not EOF(goalsfile) do
  6.   begin
  7.     if calendar1.date = info.date then
  8.     begin
  9.       memo1.Lines.add(info.Name);
  10.       memo1.Lines.add(IntToStr(info.goal));
  11.       memo1.Lines.add(info.date);
  12.     end;
  13.     closefile(goalsfile); // this is now done inside the read-loop
  14.   end;
  15. end;

First... you need to put the Read(goalsfile, info); just inside the while loop because you want all the records to read.

Then, look at your closefile(). You see it is inside the "read"-loop. That means if the loop comes back for the second read you've already closed the file (resulting in a 103 error). Move the closefile() to under the while loop (after the end of the while) and it should work. You now see why correct indenting is important.

O, and you forgot the memo1.clear at the top of that procedure.
« Last Edit: September 20, 2017, 03:07:48 pm by rvk »

shs

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

ohh it works like this but when i change date the previous informations are not coming up

rvk

  • Hero Member
  • *****
  • Posts: 6163
Re: need help with calender
« Reply #20 on: September 20, 2017, 03:33:35 pm »
ohh it works like this but when i change date the previous informations are not coming up
You still have closefile inside the while loop. You need to place it after the (second to last end;). Otherwise it will still give you an error when adding lines to memo1.

You also need to add memo1.clear at the top to clear the memo1 when you select a date.

If you add some goals on a certain date and you click on another date and you click that first date again the goals for that date should show up in your memo1.

shs

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

ohh like this right? thank you very much it works very well. and one last thing. how can i delete the goals on any day i want?

rvk

  • Hero Member
  • *****
  • Posts: 6163
Re: need help with calender
« Reply #22 on: September 20, 2017, 03:48:25 pm »
ohh like this right?
Yes, but I would move the memo1.clear to the top of the procedure. That way, if you have multiple goals on one day they will all be under each other. As you have it now only the last goal of that day will display because the memo is cleared each time it finds a goal for that day.

Quote
and one last thing. how can i delete the goals on any day i want?
You would need to rewrite the complete file. So each record from one file and write it to another.

But maybe it's better (and easier) to implement a delete-marker for the record. You could change a record and put an empty date and/or text in it. First you would need to figure out how you are going to mark a goal for deletion.

shs

  • Sr. Member
  • ****
  • Posts: 310
Re: need help with calender
« Reply #23 on: September 20, 2017, 03:57:45 pm »
sorry i don't get what you mean what is delete-marker ?

rvk

  • Hero Member
  • *****
  • Posts: 6163
Re: need help with calender
« Reply #24 on: September 20, 2017, 04:00:06 pm »
sorry i don't get what you mean what is delete-marker ?
DBase files do it like that. They have a extra field DELETED.
When reading the file you know that records with DELETE = true can be skipped.

That why I suggested maybe just emptying the Date and Name field and saving the record again. That way, when reading the file, you know a record with empty Date and Name is "deleted" and you don't need to process that record.

shs

  • Sr. Member
  • ****
  • Posts: 310
Re: need help with calender
« Reply #25 on: September 20, 2017, 04:09:22 pm »
procedure TForm1.Button2Click(Sender: TObject);
begin
  reset(goalsfile);
  while not EOF(goalsfile) do
  begin
  Read(goalsfile, info);
    if calendar1.date = info.date then
    begin
      info.name:=' ';
      info.goal:=0;
      info.date:=' ';
       memo1.Lines.add(info.Name);
      memo1.Lines.add(IntToStr(info.goal));
      memo1.Lines.add(info.date);
    end;

end;
  closefile(goalsfile);

end;                           

like that?
it work when i click the button but when i click that day[ again the original infos are shown

shs

  • Sr. Member
  • ****
  • Posts: 310
Re: need help with calender
« Reply #26 on: September 20, 2017, 04:09:45 pm »
Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button2Click(Sender: TObject);
  2. begin
  3.   reset(goalsfile);
  4.   while not EOF(goalsfile) do
  5.   begin
  6.   Read(goalsfile, info);
  7.     if calendar1.date = info.date then
  8.     begin
  9.       info.name:=' ';
  10.       info.goal:=0;
  11.       info.date:=' ';
  12.        memo1.Lines.add(info.Name);
  13.       memo1.Lines.add(IntToStr(info.goal));
  14.       memo1.Lines.add(info.date);
  15.     end;
  16.  
  17. end;
  18.   closefile(goalsfile);
  19.  
  20. end;                          

rvk

  • Hero Member
  • *****
  • Posts: 6163
Re: need help with calender
« Reply #27 on: September 20, 2017, 04:15:55 pm »
You'll need to add the write(goalsfile, info) to really write that emptied info to the file.
But if you do that right after read you need to go back one position otherwise you erase the wrong record.

But this approach you showed will always remove ALL goals for selected date. If you have multiple goals for one date you erase that too. (can you have multiple goals for one date or not?)

Better would to use the Listbox1 for selecting which goal you want to delete and then click the button. Then you don't need the while loop. All you have to do is empty your info record, reset(goalsfile), seek the listbox1.ItemIndex and do a write(goalsfile, info);. The same you did in ListBox1DblClick but then with a write instead of read (and you don't need to memo1.line.add() anything.

shs

  • Sr. Member
  • ****
  • Posts: 310
Re: need help with calender
« Reply #28 on: September 21, 2017, 01:38:46 am »
hi i'm trying to make informations to be shown when doubleclicking the listbox1

Code: Pascal  [Select][+][-]
  1. procedure TForm1.ListBox1DblClick(Sender: TObject);
  2. begin
  3.   current_goals:=listbox1.itemindex;
  4.  
  5.   reset(goalsfile);
  6.   seek(goalsfile, current_goals);
  7.   Read(goalsfile, info);
  8.   label3.caption:=info.name;
  9.   label4.caption:='/'+inttostr(info.goal);
  10.   label5.caption:=info.date;
  11.   closefile(goalsfile);
  12.  
  13.   end;  

i came up with this code but when i doubleclick the item different info is shown


jamie

  • Hero Member
  • *****
  • Posts: 6134
Re: need help with calender
« Reply #29 on: September 21, 2017, 03:55:57 am »
I don't see your "INFO" define here locally so I'll assume for the moment that
Info.Name is defined as a string?

 When reading/writing Records to file that contain such types you must use
short strings or arrays.

 for example.

 info.Name:String[20];

 or

Info.Name:array[0..19] of char;

if you don't this, then what you get is just a managed pointer in a record and when
writing and reading, that is all you get.
when reading back you'll get a pointer that isn't valid.

Now this only assumes that you are attempting to use managed strings in a record to
write / read from files.
The only true wisdom is knowing you know nothing

 

TinyPortal © 2005-2018