7

I am using a pagecontrol component and I need to add a button and click it to go to a specified page.

How can I do this please?

Rob Kennedy
  • 161,384
  • 21
  • 275
  • 467
Satch3000
  • 47,356
  • 86
  • 216
  • 346

3 Answers3

13

Add a button to the form and write an OnClick event handler like this:

procedure TMyForm.Button1Click(Sender: TObject);
begin
  PageControl1.ActivePage := TabSheet1;
end;
David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • 7
    @Satch3000, good suggestion but note that this will not trigger an OnPageChange event for the PageControl, if you want **that** to happen you'll need to call that event in your `Button1Click` explicitly. – Johan May 26 '11 at 09:41
9

You can use ActivePageIndex:

procedure TForm1.Button1Click(Sender: TObject);
begin
  PageControl1.ActivePageIndex := 0;
end;
David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
Jarek Bielicki
  • 856
  • 6
  • 16
  • 2
    This is equivalent to using `ActivePage`, the two properties are synonymous – David Heffernan May 26 '11 at 09:58
  • 4
    I would still prefer setting ActivePage, because the index of a page is variable. Or at least: has that ability. The reference to a page on the other hand will always stay the same. And most likely will the code be more readable with a self-descriptive variable name than with a meaningless digit. – NGLN May 26 '11 at 15:44
  • 4
    I think it depends on situation. Many times I used ActivePageIndex set to 0 on formShow to be sure that first one will be visible on start. After rearenging tabsheets code its still ok. – Jarek Bielicki May 26 '11 at 16:39
  • 1
    Well, that's hardly a _situation_, but just a knack to reset the changes you've made in the designer. ;) Good tip though! +1 – NGLN May 26 '11 at 16:58
1

Can I just add that you cannot set the active page within the OnChange event (I tried for ages!). Any checks that are needed must be done within the OnChanging event and then set the Allowchange var to true or false accordingly:

procedure Tfrm_AspireParams.PC_OptionsChanging(Sender: TObject;
  var AllowChange: Boolean);
begin
 AllowChange := true;
 if fActivated then
 begin
   if BBtn_Timesheets_Save.Enabled then  // They have not saved changes on this tab.
   begin
     messagedlg('Please save the page first', mtInformation, [mbOK], 0);
     AllowChange := False;
   end;
 end;

end;