You can download our sample files as part of the file nov98.zip. Visit www.zdjournals.com/cpb and click on the Source Code hyperlink.
Certain types of applications change the look of the main window, depending on the state of the application. In this article, we'll show you how to make your application behave as if it can change the main form on the fly. Our main focus will be on the PageControl and TabControl components, but we'll also take a quick peek at the Notebook component.
You can approach this problem in several ways. The simplest and most effective way is to implement some sort of paging mechanism on the main form. The other methods are more difficult to implement then a paging solution, so we won't waste time discussing them.
A paging solution means using a page container component which, naturally, contains two or more child pages. These pages can contain other components such as edits, labels, memos, check boxes, and so on. A paging component can switch pages as needed at runtime, using one of two VCL components: PageControl or Notebook. Let's take a look at these components in more detail.
PageControl->ActivePage = TabSheet1;The PageCount property tells you how many pages the page control contains. The Pages property is an array of TTabSheet pointers; you can also use this property to set the active page by index:
// set page 1 active PageControl->ActivePage = PageControl->Pages[0];When used in the way we present in this article, the PageControl component doesn't have any methods of interest to us. The methods of TPageControl depend on the pages' tabs being visible, and in this case, we'll hide the tabs.
The PageControl component has two events of note, but unfortunately neither works when changing pages via code. The OnChanging event will notify you that a page is about to change, and the OnChange event will tell you when a page has already changed. In particular, the OnChanging event would be useful for validating a page's contents. Both events depend on a tab click, so don't waste your time trying to use them in this type of implementation.
To begin creating your multi-page main form, first drop a PageControl component on your main form. You'll probably want to change the Align property to alClient, so the page control fills the entire form.
Naturally, the PageControl component isn't much good without pages--you need to create some. To create pages at design time, right-click on the page control and choose New Page from the speed menu. (Note the Next Page and Previous Page menu items as well--these will come in handy later when you're working with the individual pages.) C++Builder will create a new TTabSheet object and place it on the page control.
At this point, a tab appears at the top of the form with the name of the tab sheet in the caption. In this case, you don't want to see the tabs; set the TabVisible property of the tab sheet to False. When you do this, the tab sheet expands to occupy the full size of the page control.
Add as many pages as you need, placing any components you want on each page. Use the Next Page and Previous Page items on the speed menu to navigate between the pages. Since the tabs aren't visible, this is the most convenient way to switch between pages. (You can also use the component selector at the top of the Object Inspector.)
That's essentially all there is to using a PageControl component. Figure A shows the first page of a sample multi-page application available from our Web site.
Figure A: This application's Page menu provides easy access to several pages in the
PageControl component.
You can change the pages at runtime as we indicated earlier, using the ActivePage property. As your users interact with your program, you can change pages as needed. The resulting effect is an application that changes views seamlessly.
| Note: Don't ignore the Win 3.1 tab |
|---|
| Don't neglect a whole set of components simply because they appear on the Component Palette's Win 3.1 tab! Some very useful components reside in this group. |
The Notebook component is every bit as useful and capable as the PageControl component for our purposes. In fact, it has some features that make it more desirable. The PageIndex property, for instance, can set the active page by page index--the PageControl component doesn't have that ability except through the Pages property. The Notebook component also has an ActivePage property that can set the active page by name rather than by index.
The Notebook component differs from the PageControl component in that the notebook manages its pages internally by the notebook (as opposed to the PageControl's pages, which are individual instances of the TTabSheet class). The page list is simply a list of strings contained in the Pages property. Other than that, you can use the Notebook component just as we describe in the section on the PageControl component.