TCoolBarBands (C++)

From RAD Studio Code Examples
Jump to: navigation, search

Description

To run this example, add the example code to a new project. The example code dynamically creates a TCoolbar and three TCoolBand objects populated with a windowed control in each TCoolBand.

Code

void AddBands(TCoolBar *CoolBar, TList *Objects)
{
  TControl *pCurrent;
  for (int i = 0; i < Objects->Count; i++)
  {
	pCurrent = reinterpret_cast<TControl *>(Objects->Items[i]);
    // Add only windowed controls to the coolbar.
    if (dynamic_cast<TWinControl *>(pCurrent) != NULL)
    {
	   // Placing the control onto the CoolBar
      // causes the TCoolBar object to create a TCoolBand 
	   // and place the control within the band.
	  pCurrent->Parent = CoolBar;   // This statement increments CoolBar->Bands->Count.
	   // Get the reference of the newly created TCoolBand and assign text.
	  String S = pCurrent->ClassName();
	  CoolBar->Bands->Items[CoolBar->Bands->Count - 1]->Text = S;
	}
  }
}

#include <memory>       //For STL auto_ptr class

__fastcall TForm1::TForm1(TComponent* Owner)
  : TForm(Owner)
{
  TCoolBar *CoolBar = new TCoolBar(this); // The owner (Form1) will clean this up.
  CoolBar->Parent = this;
  CoolBar->Align = alTop;
  std::auto_ptr<TList> List(new TList);
  try
  {
	TControl *Control;
	Control = new TButton(CoolBar); // The owner (CoolBar) will clean this up.
	List->Add(Control);
	Control = new TCheckBox(CoolBar); // The owner (CoolBar) will clean this up.
	List->Add(Control);
	Control = new TEdit(CoolBar); // The owner (CoolBar) will clean this up.
	List->Add(Control);
	AddBands(CoolBar, List.get());
  }
  catch (...)
  {
    ShowMessage("Some objects could not be added to Coolband");
  }
}

Uses

Personal tools
Assistance