Home > Software design >  DBNavigator OnClick event
DBNavigator OnClick event

Time:11-06

With the TDBNavigator correctly connected, and without code in the OnClick event it is working as expected. Where I get understanding problems is when I put code in the event:

  • nbInsert: Will the INSERT be done or do I have to do it through code?
  • nbPost: Do I have to do it through code or is the post done anyway?

When the post is done the record will go up to the next record - is that right?

CodePudding user response:

As per the documentation for Vcl.DBCtrls.TDBNavigator.OnClick:

Occurs when a button on the database navigator is clicked, after the action is executed.

Use the OnClick event handler to write code that responds to the click event. Because the buttons on the navigator already have default actions that occur when they are clicked, it isn't necessary to write an OnClick event handler.

Note: The OnClick event occurs after the default action for the clicked button.

So: no, you don't need to do any action yourself. Or more precisely: it will be too late already.

As per the documentation for the navigation buttons using "Post" does not imply any record shift.

CodePudding user response:

The documention, as quoted in the other answer, is correct but does not provide any insight into how the DBNavigator works. The DBNavigator actions merely reflect the default operation of TDataSet.

Whenever a TDataSet prepares to perform a navigation action (i.e. moves the logical cursor which tracks the active record), it calls its CheckBrowseMode method

procedure TDataSet.CheckBrowseMode;
begin
  CheckActive;
  DataEvent(deCheckBrowseMode, 0);
  case State of
    dsEdit, dsInsert:
      begin
        UpdateRecord;
        if Modified then Post else Cancel;
      end;
    dsSetKey:
      Post;
  end;
end;

Leaving aside dsSetKey state, this only does anything if the dataset's state is dsEdit or dsInsert. If it is, it calls UpdateRecord to write any changes which have been made to the dataset's fields back to the dataset's active buffer.

If, but only if, calling UpdateRecord results in the dataset's Modified flag being set, CheckBrowseMode calls its Post method, otherwise it calls Cancel which abandons any changes. The only way that Modified will be set to true is by the operation of TDataLink descendants connecting the dataset's fields to db-aware controls such as TDBEdits.

It is important to understand that it is the call to Post which saves any changes and that this occurs regardless of what triggered the call to CheckBrowseMode. One such trigger, but only one, is one of the dBNavigator's navigation buttons, nbFirst, nbPrior, nbNext, nbLast, being clicked.

CheckBrowseMode's operation also explains what happens if you click nbInsert immediately followed by nbFirst, nbPrior, nbNext or nbLast, namely that by default, the "inserted" record is abandoned.

  • Related