GetBookmark (C++)

From RAD Studio Code Examples
Jump to: navigation, search

Description

This example uses a button to copy the value of a field from the previous record into the corresponding field in the current record.


To ensure that the button is disabled when there is no previous record, the OnDataChange event of the DataSource detects when you move to the beginning of the file (the BOF property becomes true) and disables the button. Detection occurs on scrolling and editing, not on mouse selection.

Code

void __fastcall TForm1::DS2DataChange(TObject *Sender, TField *Field)
{
  if (Customers->Bof)
	Button2->Enabled = False;
  else
	Button2->Enabled = True;
}

void __fastcall TForm1::Button2Click(TObject *Sender)
{
  TBookmark SavePlace;
  Variant PrevValue;

  // Get a bookmark so that you can return to the same record.
  SavePlace = Customers->GetBookmark();
  Customers->FindPrior();// Move to prior record.
  PrevValue = Customers->FindField("Field1")->Value; // Get the value.
  // Move back to the bookmark.
  // This may not be the next record anymore,
  // if something else is changing the dataset asynchronously.
  Customers->GotoBookmark(SavePlace);
  Customers->Edit();
  Customers->FindField("Field1")->Value = PrevValue; // Set the value.
}

Uses