When Delphi (2006) goes quantum: I’ve got “something” that appears to be both a TToolBar and a TPanel, depending on how you observe it. I’d like to understand what’s going on.
Here is how to create it and what happens:
-
in the DFM
- add a TToolBar named bar;
- in that TToolBar, put a TPanel.
-
in the code and at runtime:
- the panel appears in the list of buttons bar.Buttons[], let’s say at index i
- bar.Buttons[i], from the compiler point of view, is a TToolButton
- bar.Buttons[i].ClassName = ‘TPanel’
- (bar.Buttons[i] is TToolButton) = true, but that’s the compiler optimising the call to ‘is’ out;
- indeed IsBarButton(bar.Buttons[i]) is false for function IsBarButton (defined below);
- bar.Buttons[i].Name is the name I gave the TPanel in the DFM
- inspecting the value bar.Buttons[i] in the debugging:
- it has a property ‘Caption’ the real TToolButton’s don’t have
- strangely, it has all properties TToolButton’s have, like TToolButton.Indeterminate (=true).
IsToolButton:
function IsToolButton(X : TObject) : boolean;
begin
Result := X is TToolButton;
end;
So bar.Buttons[i] both is and is not a TToolButton… what’s up ?
(Bottom story is I’d like to distinguish my TPanel from the genuine TToolButton’s. This I can do in more or less hackish ways. My goal by asking this question here, is to get a fuller understanding of what’s really happening here.)
Question: what is happening ?
Sub-question: is it legitimate to add a TPanel to a TToolBar ?
The only thing the OS allows to be added to a tool bar is a tool button. To add anything else, you technically need to create a button and then put your other things on top of it. The button that gets added is literally a placeholder. It’s there to take up space so the next thing you add gets positioned properly.
You can see this sometimes if the non-tool-button control you add is transparent. Then you can see the tool bar’s separator underneath, so it looks like there’s a vertical line running through the middle of your control.
When you add a non-tool-button control to the tool bar, the
Buttonsproperty indeed lies about the type of the control. You’ll notice throughout ComCtrls.pas thatTToolBaritself always casts the buttons toTControland then checks whether they really descend fromTToolButton. It’s completely legitimate to add non-buttons to a tool bar; that’s why the Form Designer allows it in the first place.I suggest you use the Form Designer to create your tool bar. That way, the IDE will maintain an identifier for you in your form, so you’ll always have a direct reference to your panel. You won’t have to go hunting for it in the tool bar. Even if you’re creating the tool bar manually, it’s a good idea to make an extra field to refer to the panel. Even if you move the panel around within the tool bar, it will still be the same object the whole time, so you needn’t worry about dangling references.