Delphi 2010 TList and TBookmark issue

dark3lf
dark3lf used Ask the Experts™
on
Hello everyone,

I am trying to save a TBookmarks from a TTable into TList. But the problem is when I am trying to move to those TBookmark positions in the TTable. It does not position correctly. This used to work back in the older versions of Delphi but I need this for Delphi 2010. Test project is very simple. Form and two tables shown in two grids.

Kind regards
var BookmarkList: TList;

... ON CREATE BookmarkList := TList.Create;

// Feed Bookmarks to TList. You can add random record... just remember the order.
procedure TFormTest.ButtonAddBookmarkToListClick(Sender: TObject);
var TempBookmark: TBookmark;
begin
  TempBookmark := mySQLTable1.GetBookmark;
  BookmarkList.Add(TempBookmark);
end;

// Go to Bookmarks. ShowMessage is there just for me to check if it is pointing back to the right record.
procedure TFormTest.ButtonGoToBookmarksClick(Sender: TObject);
var Cnt: Integer;
begin
  for Cnt := 0 to BookmarkList.Count -1 do begin
    mySQLTable1.GotoBookmark(TBookMark(BookmarkList.Items[Cnt]));
    ShowMessage(mySQLTable1.FieldByName('Customer').AsString);
  end;
end;

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
Hi,

Maybe You should try to do this in that way (code snipet).
I wrote this from my memory, so it can have errors.
If You have any questions, ask me, please.

// interface section

type
  TBookmarkHelperClass = class
    FBookmark: TBookmark;
  public
    constructor Create(Bookmark: TBookmark);
  published
    property Bookmark: TBookmark read FBookmark write FBookmark;
  end;

// implementation section

constructor TBookmarkHelperClass.Create(Bookmark: TBookmark);
begin
  FBookmark:=Bookmark;
end;

// adding to list, because You must add an Object

  TempBookmark := mySQLTable1.GetBookmark;
  BookmarkList.Add(TBookmarkHelperClass.Create(TempBookmark));

// reading from list

var TempBookmark: TBookmark;

  for Cnt := 0 to BookmarkList.Count -1 do begin
    TempBookmark := TBookmarkHelperClass(BookmarkList.Items[Cnt]).Bookmark;
    mySQLTable1.GotoBookmark(TempBookmark);
    ShowMessage(mySQLTable1.FieldByName('Customer').AsString);
  end;

Open in new window

Geert GOracle dba
Top Expert 2009

Commented:
did you close and reopen the table ? (after insert/delete/update) ?
if so then the bookmarks can become invalid



Geert GOracle dba
Top Expert 2009

Commented:
why not try to use the primary key of the table ?
this should allways work (unless you don't have a primary key)
Commented:
Hello everyone!

Paul Hughes already solved that issue on EDN. Thank you for help. Here is the link.
https://forums.embarcadero.com/message.jspa?messageID=254288

Kind regards

Commented:
Pleze, copy and paste the solution here,so we can grow our knowledge base without depends on another website be online...

Author

Commented:
This was written by  Paul Hughes on EDN:)

It's probably because the TBookmark is now of type TBytes.

Try this, the code is simpler:

add Generics.Collections to the uses clause.

var
BookmarkList: TList<TBookmark>;
...
implementation
...

procedure TForm3.AddBkMarkClick(Sender: TObject);
begin
BookmarkList.Add(Table1.GetBookmark);
end;

procedure TForm3.FormCreate(Sender: TObject);
begin
BookmarkList := TList<TBookmark>.Create;
end;

procedure TForm3.GotoBkMarksClick(Sender: TObject);
var
Cnt: Integer;
begin
for Cnt := 0 to Pred(BookmarkList.Count) do
begin
if Table1.BookmarkValid(BookmarkList[Cnt]) then
begin
Table1.GotoBookmark(BookmarkList[Cnt]);
ShowMessage(Table1.FieldByName('Customer').AsString);
end;
end;
end;

Regards, Paul.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial