Guaranteed order TTabsheets are read from TPageControl thru Controls[]

Posted on 2011-09-28
Medium Priority
Last Modified: 2012-06-27
HELP! I asked a similiar question awhile back and am still frustrated with the same issue. I'm writing a generic function that will be used in many forms.

Actually I'm reading all Controls on a form in order (i hope). However, when I get to a certain TPageControl and read it's child controls thru PageContolPtr->Controls(i) I DO NOT get the TTabSheets in order. The PageControl has 4 TTabSheets. For any individual TabSheet I've verified that TabOrder = TabIndex = PageIndex. It is. However using PageContolPtr->Controls(i) it reads Tab0, Tab2, Tab3 and then Tab1. How do I read them Tab0, Tab1, Tab2, Tab3?

Please, can anyone help on this.

Question by:flins
  • 3
  • 3
LVL 16

Accepted Solution

George Tokas earned 2000 total points
ID: 36813317
Hello there,
Again a question belongs to C++ Builder topic area.
I don't know if my post will help you but I will mention AGAIN that if this question doesn't attracted my attention as "Neglected Question Alert" I wouldn't post this.
All of us (developers using C++ Builder or Delphi) AND I AM NOT AN EXCEPTION tend to forget very useful properties of the controls we are using.
One of them is the "Tag" property and it is of integer type.
Check out that by default ALL controls you use in the form has Tag = 0.
So "Guaranteed order TTabsheets .."
Set the Tag property of the first TTabSheet a number and increment by one in the next and so on...
It will be good NOT to start with a 0(zero) in order not to mess with other controls, try 100 as a base or whatever number you decide.
Check the Tag property of the tabsheet and you will know which tabsheet it is.

George Tokas.

Author Closing Comment

ID: 36903355
I'm accepting this solution since effort wasn't made by anyone else. The actual solution, and eiither something I don't understand in Borland or a Borland bug, is to save the Form with TabSheet 0 as the active TabSheet. I've since found that if any TabSheet other than TS 0 is active at save time, the TabOrder, TabIndex, etc are meaningless.

Author Comment

ID: 36903413
I'd like to add an additional comment. The Form in question has one PageControl with 4 TabSheets. The TabSheets were created in order TabSheet1, TabSheet2, TabSheet3 and TabSheet4. If TabSheet1 is the active TabSheet when the form is saved, the TabOrder, TabIndex and PageIndex are precisely 0,1,2 and 3 as I would expect. When TabSheet 2 is the active TabSheet at save time, the TabOrder, etc are 0, 3, 1, 2. It makes no sense to me.
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

LVL 16

Expert Comment

by:George Tokas
ID: 36903863
>>It makes no sense to me.
Unfortunately it MAKES SENSE to me...
Thanks for accepting the answer by the way.
The order of tabsheets inside the page control depending on the active page in design phase.
By "default" BCB assumes that YOU WANT this particular tabsheet as the default active one and enumerates the rest in incremental order.
Except the proposal I made to be SURE about the order using the tag property, you can use the following at TForm constructor:
PageControlx->ActivePage = TabSheet1;
This way regardless of the active page is set in design time allways will use TabSheet1 at runtime.
The SAME problem I had some years ago and to tell the true I allways set the first as default in designtime since I didn't use the method you are using.
One more thing: There are not many people here knowing how to use the VCL with C++ Builder but the safer way to get an answer - fast - is to post in C++ Builder topic area..
AT LEAST I will try to help you and I hope and more people.
This question was "Neglected Question Alert" for me via email, but I am monitoring every day the C++ Builder topic area.

George Tokas.

Author Comment

ID: 36906004

I understand your explanation above. However, let me ask the following. Let's call the 4 TabSheets T1 - T4. When T1 is the active page on save, the TabSheet (TabOrder) is T1 = 0. T2 = 1, T3 = 2 and T4 =  3. When T2 is the active page the TabOrder is T1 = 0, T2 = 3, T3 = 2 and T4 = 2. Why? The TabSheets were definifely and absolutely created in the exact order of T1, T2, T3 and T4.
LVL 16

Expert Comment

by:George Tokas
ID: 36908544
Hello again,
I have to dig a bit about this.
I told you that I am not using the method you are using.
I assume that all tabsheets inside the pagecontrol is meant to be used for one task so that is the reason you are "circling" through controls.
This problem appeared to me back in 2005 when a colegue of mine used this way and solved using the tag the way I proposed before. There was a huge mess happened before we realise where was the problem because the application created a data file depending with the order and there were many pagecontrols and tabsheets mostly created at runtime.
Imagine an application controls multiple clients and creates configuration and report files for each one at runtime in a LAN depending on a kind of database.
To save coding space this colegue used circling through controls to find out what group the control had to respond and called the handler. Imagine 100 buttons because that was one of the cases.
So intead of the method he used we set the tag property and retrieve it from "Sender" and used it as parameter. Same used and to pagecontrols panels and more the application had.
This way saved us from using loops when they don't needed and the code was less - and faster -after eliminated check loops.
Now for the tabsheet order I have to dig a bit at the magazine and I will be back.

George Tokas.

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

601 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question