RAD Studio VCL Reference
|
Indicates the number of components owned by the component.
property ComponentCount: Integer;
__property int ComponentCount;
Use the ComponentCount property to determine the number of components owned by a component, for example, when iterating through the components list to perform some action on all owned components. The ComponentCount property equals the number of items in the components list. This value is one more than the highest Components index, because the first components index is 0.
C++ Examples:
/* This code fragment moves any nonvisual components on the form into a separate data module. Note that the components are removed starting with the last component, so that the unprocessed portion of the Components array does not change. Note: This code does not save the form or data module to disk after the nonvisual components are moved. If executed at runtime, the effect will not persist. Add a second TForm and name it ComponentCount2. Put "#include ComponentCount2.cpp" in ComponentCount.h. Place a TMemo in each form and a TButton in Form1. */ void __fastcall TForm1::Button1Click(TObject *Sender) { int I; TComponent *Temp; Memo1->Lines->Add("Components removed: "); Form2->Memo1->Lines->Add("Components added: "); for (I = ComponentCount - 1; I >= 0; I--) { Temp = Components[I]; // only move components that are not controls if (dynamic_cast<TControl *>(Temp) == NULL) { RemoveComponent(Temp); Memo1->Lines->Add(Temp->Name); Form2->InsertComponent(Temp); Form2->Memo1->Lines->Add(Temp->Name); } } }
/* This example fills a list box with the names of TDataSources that are present and then fills a DBGrid a second form with the data from the selected data source. The DBGrid is not filled in until Form2 is activated. Here is the code for Form1 that fills the list box. */ TForm1 *Form1; TTable *Customers, *Blobs; Boolean Form1Done = False; void CreateCustSource() { Customers = new TTable(Form1); // The owner will clean this up. Customers->Active = false; // The Table component must not be active Customers->DatabaseName = "DBDEMOS"; Customers->TableType = ttParadox; Customers->TableName = "CustInfo"; Customers->Active = False; if (Customers->Exists) // Don't overwrite an existing table { Customers->Close(); Customers->DeleteTable(); } // The Table component must not be active // First, describe the type of table and give // it a name // Next, describe the fields in the table Customers->FieldDefs->Clear(); TFieldDef *pNewDef = Customers->FieldDefs->AddFieldDef(); pNewDef->Name = "Field1"; pNewDef->DataType = ftInteger; pNewDef->Required = true; pNewDef = Customers->FieldDefs->AddFieldDef(); pNewDef->Name = "Field2"; pNewDef->DataType = ftString; pNewDef->Size = 30; // Next, describe any indexes Customers->IndexDefs->Clear(); /* the 1st index has no name because it is a Paradox primary key */ Customers->IndexDefs->Add("","Field1", TIndexOptions() <<ixPrimary << ixUnique); Customers->IndexDefs->Add("Fld2Index","Field2", TIndexOptions() << ixCaseInsensitive); // Now that we have specified what we want, create the table Customers->CreateTable(); Customers->Active = True; for (int i = 1; i <= 20; i++) Customers->AppendRecord(ARRAYOFCONST((i*111, i*222))); Form1->CustomerDS->DataSet = Customers; Customers->Active = True; } void CreateBlobSource() { int i; Blobs = new TTable(Form1); Blobs->DatabaseName = "DBDEMOS"; Blobs->TableType = ttParadox; Blobs->TableName = "MyBlobInfo"; Blobs->Active = False; // Don't overwrite an existing table // if Blobs.Exists then // MessageDlg('CustInfo table already exists.', mtWarning, [mbOK], 0) // else if (Blobs->Exists) { Blobs->Close(); Blobs->DeleteTable(); }; // The Table component must not be active } // First, describe the type of table and give } // it a name } // Next, describe the fields in the table } Blobs->FieldDefs->Clear(); TFieldDef *newDef = Blobs->FieldDefs->AddFieldDef(); newDef->Name = "Field1"; newDef->DataType = ftInteger; newDef->Required = True; newDef = Blobs->FieldDefs->AddFieldDef(); newDef->Name = "Field2"; newDef->DataType = ftBlob; newDef->Size = 30; // Next, describe any indexes } Blobs->IndexDefs->Clear(); // The 1st index has no name because it is // a Paradox primary key } TIndexDef *newIDef = Blobs->IndexDefs->AddIndexDef(); newIDef->Name = ""; newIDef->Fields = "Field1"; newIDef->Options << ixPrimary; /* with AddIndexDef do begin Name := 'Fld2Indx'; Fields := 'Field2'; Options := [ixCaseInsensitive]; end; */ // Call the CreateTable method to create the table } Blobs->CreateTable(); Blobs->Active = True; for (int i = 1; i <= 20; i++) Blobs->AppendRecord(ARRAYOFCONST((i*111, IntToStr(i*222)))); Form1->BlobDS->DataSet = Blobs; Blobs->Active = True; }; void __fastcall TForm1::FormCreate(TObject *Sender) { CreateCustSource(); CreateBlobSource(); for (int i = 0; i < ComponentCount; i++) { if (Components[i]->ClassNameIs("TDataSource")) ListBox1->Items->AddObject(Components[i]->Name, dynamic_cast<TObject *>(Components[i])); } } void __fastcall TForm1::ListBox1MouseDown(TObject *Sender, TMouseButton Button, TShiftState Shift, int X, int Y) { Form1Done = True; }
/* The following code sets the DataSource for a DBGrid component in Form2 to the currently selected data source in a list provided in Form1. This allows users to specify the desired information in Form1, and then move to Form2 to view the information. Select an item in the listbox and then activate Form2 by clicking in it. Also, set the visible property of Form2 to true. */ void __fastcall TForm2::FormActivate(TObject *Sender) { if (Form1Done) { DBGrid2->DataSource = dynamic_cast<TDataSource *>(Form1->ListBox1->Items->Objects[Form1->ListBox1->ItemIndex]); DBNavigator2->DataSource = dynamic_cast<TDataSource *>(Form1->ListBox1->Items->Objects[Form1->ListBox1->ItemIndex]); }; }
Delphi Examples:
{ This code fragment moves any nonvisual components on the form into a separate data module. Note that the components are removed starting with the last component, so that the unprocessed portion of the Components array does not change. Note: This code does not save the form or data module to disk after the nonvisual components are moved. If executed at runtime, the effect will not persist. Add a second TForm and name it ComponentCount2. Put "ComponentCount2" at the end of the uses clause of Form1. Place a TMemo in each form and a TButton in Form1. } procedure TForm1.Button1Click(Sender: TObject); var I: Integer; Temp: TComponent; begin Form1.Memo1.Lines.Add('Components removed: '); Form2.Memo1.Lines.Add('Components added: '); for I := ComponentCount - 1 downto 0 do begin Temp := Components[I]; if not (Temp is TControl) then begin RemoveComponent(Temp); Form1.Memo1.Lines.Add(Temp.Name); Form2.InsertComponent(Temp); Form2.Memo1.Lines.Add(Temp.Name); end; end; end;
{ This example fills a list box with the names of TDataSources that are present and then fills a DBGrid a second form with the data from the selected data source. The DBGrid is not filled in until Form2 is activated. Here is the code for Form1 that fills the list box. } var Form1: TForm1; Customers: TTable; Blobs: TTable; Form1Done : Boolean; procedure CreateMyTable; procedure CreateCustSource; procedure CreateBlobSource; implementation {$R *.dfm} procedure TForm1.FormCreate(Sender: TObject); var I: Integer; begin CreateCustSource; CreateBlobSource; for I := 0 to ComponentCount - 1 do begin if (Components[I] is TDataSource) then ListBox1.Items.AddObject(Components[I].Name, Components[I] as TObject); end; end; procedure CreateCustSource(); begin Customers:= TTable.Create(Form1); with Customers do begin DatabaseName := 'DBDEMOS'; TableType := ttParadox; TableName := 'MyCustInfo'; ReadOnly:= False; // Don't overwrite an existing table // Looking for c:/ProgramFiles/Common Files/Codegear Shared/Data/dbdemos.gdb if (not Customers.Exists) then CreateMyTable else begin if (Customers.Exists AND (MessageDlg('MyCustInfo table already exists. Do you want to rebuild it?', mtConfirmation, [mbYes, mbNo], 0) = mrYes)) then begin Customers.Close; Customers.DeleteTable; CreateMyTable; end; end; end; Form1.CustomerDS.DataSet:= Customers; Customers.Active:= True; end; procedure CreateMyTable(); var i: Integer; begin // The Table component must not be active // First, describe the type of table and give // it a name // Next, describe the fields in the table with Customers do begin with FieldDefs do begin Clear; with AddFieldDef do begin Name := 'Field1'; DataType := ftInteger; Required := True; end; with AddFieldDef do begin Name := 'Field2'; DataType := ftString; Size := 30; end; with AddFieldDef do begin Name := 'Field3'; DataType := ftString; Size := 30; end; with AddFieldDef do begin Name := 'Field4'; DataType := ftString; Size := 30; end; end; // Take out this part to remove indexing // Next, describe any indexes with IndexDefs do begin Clear; // The 1st index has no name because it is // a Paradox primary key with AddIndexDef do begin Name := ''; Fields := 'Field1'; Options := [ixPrimary]; end; with AddIndexDef do begin Name := 'Fld2Indx'; Fields := 'Field2'; Options := [ixCaseInsensitive]; end; end; // Call the CreateTable method to create the table CreateTable; Customers.Active:= True; for i := 1 to 20 do Customers.AppendRecord([i*111, i*222, i*333, i*444]); end; end; procedure CreateBlobSource; var i: Integer; begin Blobs:= TTable.Create(Form1); with Blobs do begin DatabaseName := 'DBDEMOS'; TableType := ttParadox; TableName := 'MyBlobInfo'; Blobs.Active := False; { Don't overwrite an existing table } // if Blobs.Exists then // MessageDlg('CustInfo table already exists.', mtWarning, [mbOK], 0) // else begin if (Blobs.Exists) then begin Blobs.Close; Blobs.DeleteTable; end; { The Table component must not be active } { First, describe the type of table and give } { it a name } { Next, describe the fields in the table } with FieldDefs do begin Clear; with AddFieldDef do begin Name := 'Field1'; DataType := ftInteger; Required := True; end; with AddFieldDef do begin Name := 'Field2'; DataType := ftBlob; Size := 30; end; end; { Next, describe any indexes } with IndexDefs do begin Clear; { The 1st index has no name because it is { a Paradox primary key } with AddIndexDef do begin Name := ''; Fields := 'Field1'; Options := [ixPrimary]; end; { with AddIndexDef do begin Name := 'Fld2Indx'; Fields := 'Field2'; Options := [ixCaseInsensitive]; end; } end; { Call the CreateTable method to create the table } CreateTable; Blobs.Active:= True; for i := 1 to 20 do Blobs.AppendRecord([i*111, IntToStr(i*222)]); end; end; Form1.BlobDS.DataSet:= Blobs; Blobs.Active:= True; end; procedure TForm1.ListBox1MouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer); begin Form1Done := True; end; initialization Form1Done := False;
{ The following code sets the DataSource for a DBGrid component in Form2 to the currently selected data source in a list provided in Form1. This allows users to specify the desired information in Form1, and then move to Form2 to view the information. Select an item in the listbox and then activate Form2 by clicking in it. Also, set the visible property of Form2 to true. } procedure TForm2.FormActivate(Sender: TObject); begin if (Form1Done) then begin DBGrid2.DataSource := Form1.ListBox1.Items.Objects[Form1.ListBox1.ItemIndex] as TDataSource; DBNavigator2.DataSource := Form1.ListBox1.Items.Objects[Form1.ListBox1.ItemIndex] as TDataSource; end; end;
Copyright(C) 2009 Embarcadero Technologies, Inc. All Rights Reserved.
|
What do you think about this topic? Send feedback!
|