OnScroll (C++)

From RAD Studio Code Examples
Jump to: navigation, search


This example shows how to use a scroll bar to navigate the pages of a page control. To run this example, place a scroll bar and a page control on a form. Right-click the page control and add 12 pages to it. Add the following code to the OnCreate event of the form, the OnChange event of the page control, and the OnScroll event of the scroll bar. Note that the LargeChange and SmallChange properties are set to automatically change the active page by one tab or one row respectively. The OnScroll event is used to make the actual change in the page control, while the OnChange event of the page control ensures that the scroll bar is kept in sync with changes made directly to the page control.


__fastcall TForm1::TForm1(TComponent* Owner)
  : TForm(Owner)
  const TColor colorPalette[12] = {clRed, clGreen, clYellow, clBlue, clWhite, clFuchsia, clTeal, clNavy, clMaroon, clLime, clOlive, clPurple};
  // First, initialize the page control.
  PageControl1->TabWidth = PageControl1->ClientWidth/4 - 1;
  PageControl1->ActivePage = PageControl1->Pages[0];
  for (int i = 0; i < PageControl1->PageCount; i++)
	PageControl1->Pages[i]->Brush->Color = colorPalette[i];
  /* Set up the scroll bar to reflect
	 the structure of the page control. */
  ScrollBar1->Max = PageControl1->PageCount - 1;
  ScrollBar1->Min = 0;
  ScrollBar1->SmallChange = 1;
  // Set LargeChange to a single row of tabs.
  ScrollBar1->LargeChange = PageControl1->ClientWidth / PageControl1->TabWidth;

void __fastcall TForm1::PageControl1Change(TObject *Sender)
  ScrollBar1->Position = (dynamic_cast<TPageControl *>(Sender))->ActivePage->PageIndex;

void __fastcall TForm1::ScrollBar1Scroll(TObject *Sender,
      TScrollCode ScrollCode, int &ScrollPos)
  PageControl1->ActivePage = PageControl1->Pages[ScrollPos];