Solved

Creating controls on TPageControl pages at runtime

Posted on 2010-11-10
10
479 Views
Last Modified: 2012-05-10
I can drop a TPagecontrol on a form, add a tabsheet, and place a TPanel on the tabsheet and i can change the color of the Tpanel.

I can't seem to do this at runtime !  Why?

   AUnit:= TPanel.Create(ATabSheet);
    AUnit.Parent:= ATabSheet;
    AUnit.ParentColor:= False;
    AUnit.BevelOuter:= bvRaised;
    AUnit.BevelInner:= bvLowered;
    AUnit.Color:= clGreen;
0
Comment
Question by:Looking_4_Answers
  • 6
  • 3
10 Comments
 
LVL 58

Expert Comment

by:cyberkiwi
ID: 34108468
That works perfectly for me, what error are you getting?
Maybe it is behind some other control, or too low?
0
 
LVL 58

Expert Comment

by:cyberkiwi
ID: 34108479
What if you added these 3 lines?

    AUnit.BringToFront;
    AUnit.Align := alClient;
    ATabSheet.PageControl.ActivePage := ATabSheet;
0
 

Author Comment

by:Looking_4_Answers
ID: 34108543
The Panel is not aligned alclient

The TPagecontrol is already on the form (it is not created at runtime)

I create the TTabsheets at runtime, one at a time. After each one is created, i create the panels (many of them n a row) on each of the TTabsheets.  

I am getting the panels, its just that the have taken the color of the parent (TTabsheet) and i can't change the color.

your suggestion : AUnit.BringToFront;

does not work

setting the active page would not be appropriate in the case, as i am createing a page, adding the panels, creating another page, and so on
0
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 58

Expert Comment

by:cyberkiwi
ID: 34108588
The 3 things to test were just to make sure you can see it

    AUnit.BringToFront;   // if it is behind another panel
    AUnit.Align := alClient;   // if the panel has been created outside of visible region of ttabsheet
    ATabSheet.PageControl.ActivePage := ATabSheet;   // if you are not looking at the right sheet!

What do you get for this line?
    showmessage(inttostr(aunit.color));

It should be 32768.
There is no reason why your code shouldn't work.
0
 

Author Comment

by:Looking_4_Answers
ID: 34108612
well, after testing, i can do it using

  ATabsheet:= TTabsheet.Create(PageControl1);
  ATabsheet.PageControl:= PageControl1;
  ATabSheet.Parent:= PageControl1;
  APanel:= TPanel.Create(ATabSheet);
  APanel.Parent:= ATabSheet;
  APanel.Color:= clGreen;


but my code below does not work


procedure TfrmMain.UpdateUnits(ATabSheet: TTabSheet);
var
 AUnit: TPanel;
 X, Y, W, H, S: Integer;
 P: Integer;
begin
 X:= 10; Y:= 10; W:= 75; H:= 75; S:= 5;
 frmDataMod.qryUnits.Active:= False;
 try
  frmDataMod.qryUnits.Parameters.ParamByName('locname').Value:= ATabSheet.Caption;
  frmDataMod.qryUnits.Active:= True;
  if frmDataMod.qryUnits.RecordCount > 0 then
  begin
   while not frmDataMod.qryUnits.EOF do
   begin
    AUnit:= TPanel.Create(ATabSheet);
    AUnit.Parent:= ATabSheet;
    AUnit.BringToFront;
    AUnit.ParentColor:= False;
    AUnit.BevelOuter:= bvRaised;
    AUnit.BevelInner:= bvLowered;
    AUnit.Color:= clGreen; //********* THIS DOES NOT WORK *******************
    AUnit.Left:= X;
    AUnit.Top:= Y;
    AUnit.Width:= W;
    AUnit.Height:= H;
    AUnit.Caption:= frmDataMod.qryUnits.fieldByName('unit_number').AsString + ' - ' + frmDataMod.qryUnits.fieldByName('unit_type').AsString;
    frmDataMod.qryUnits.Next;
    P:= X + W + S;
    if P < ATabSheet.ClientWidth then
     X:= P //X = X + Width + space
    else
    begin
     X:= 5;
     Y:= Y + H + S; //Y = Y + Hieght + Space
    end;
   end;
  end;//if
 except
 begin
  ShowMessage('Error retrieving units to display on location pages!');
 end;
 end;//try
end;



procedure TfrmMain.UpdateLocations;
var
 Locations: TStringList;
 I: Integer;
 APage: TTabSheet;
begin
 RemoveLocations;
 Locations:= TStringList.Create;
 frmDataMod.GetLocations(Locations, False);
 for I:= 0 to Locations.Count-1 do
 begin
  APage:=  TTabSheet.Create(pgeMain);
  APage.PageControl:= pgeMain;
  APage.Parent:= pgeMain;
  APage.Caption:= Locations[I];
  UpdateUnits(APage);
 end;
 Locations.Free;
end;


procedure TfrmMain.RemoveLocations;
var
 I: Integer;
begin
 for I:= pgeMain.PageCount-1 downto 0 do
  pgeMain.Pages[I].Destroy;
end;
0
 
LVL 58

Expert Comment

by:cyberkiwi
ID: 34108644
No idea what the issue is, but try adding the ShowMessage in my previous comment to your code, it should work.
Now, this is my test with minor variations (not controls related) to your code.

It worked.

Form has nothing but 1 PageControl, dfm listed below.
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  ComCtrls, ExtCtrls;

type
  TForm1 = class(TForm)
    pgeMain: TPageControl;
    procedure FormDblClick(Sender: TObject);
  private
    procedure UpdateLocations;
    procedure UpdateUnits(ATabSheet: TTabSheet);
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure Tform1.UpdateUnits(ATabSheet: TTabSheet);
var
 AUnit: TPanel;
 X, Y, W, H, S: Integer;
 P: Integer;
 i: integer;
begin
 X:= 10; Y:= 10; W:= 75; H:= 75; S:= 5;
 try
   for i := 1 to 5 do
   begin
    AUnit:= TPanel.Create(ATabSheet);
    AUnit.Parent:= ATabSheet;
    AUnit.BringToFront;
    AUnit.ParentColor:= False;
    AUnit.BevelOuter:= bvRaised;
    AUnit.BevelInner:= bvLowered;
    AUnit.Color:= clGreen; //********* THIS DOES NOT WORK *******************
    AUnit.Left:= X;
    AUnit.Top:= Y;
    AUnit.Width:= W;
    AUnit.Height:= H;
    AUnit.Caption:= inttostr(i);
    P:= X + W + S;
    if P < ATabSheet.ClientWidth then
     X:= P //X = X + Width + space
    else
    begin
     X:= 5;
     Y:= Y + H + S; //Y = Y + Hieght + Space
    end;
   end;
 except
 begin
  ShowMessage('Error retrieving units to display on location pages!');
 end;
 end;//try
end;

procedure TForm1.UpdateLocations;
var
 Locations: TStringList;
 I: Integer;
 APage: TTabSheet;
begin
 //RemoveLocations;
 //Locations:= TStringList.Create;
 //frmDataMod.GetLocations(Locations, False);
 for I:= 0 to 3 do
 begin
  APage:=  TTabSheet.Create(pgeMain);
  APage.PageControl:= pgeMain;
  APage.Parent:= pgeMain;
  APage.Caption:= 'tab' + IntToStr(i);
  UpdateUnits(APage);
 end;
 //Locations.Free;
end;

procedure TForm1.FormDblClick(Sender: TObject);
var
  aunit: Tpanel;
  atabsheet: ttabsheet;
begin
  UpdateLocations;
end;

end.

===============================

object Form1: TForm1
  Left = 273
  Top = 55
  Width = 783
  Height = 540
  Caption = 'Form1'
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'MS Sans Serif'
  Font.Style = []
  OldCreateOrder = False
  OnDblClick = FormDblClick
  PixelsPerInch = 96
  TextHeight = 13
  object pgeMain: TPageControl
    Left = 228
    Top = 108
    Width = 289
    Height = 193
    TabOrder = 0
  end
end

Open in new window

0
 

Author Comment

by:Looking_4_Answers
ID: 34108767
thanks, but i believe i already stated above that i know that works.....im trying to figure out why my code doesn't work. Im headed to bed, its late here, i will wait to hear from other experts in the morning
0
 
LVL 58

Expert Comment

by:cyberkiwi
ID: 34108811
I was using your code "that does not work" in my snippet, but let's see if anyone has any clues.
Did you try the ShowMessage?
0
 
LVL 32

Accepted Solution

by:
ewangoya earned 500 total points
ID: 34108996
For the panel to take on te color you assign it, you must set the ParentBackground to False
AUnit.ParentBackground := False;

Ialso changed how you check whether to start adding panels to the next line
if (P + W ) < ATabSheet.ClientWidth then..

procedure TfrmMain.UpdateUnits(ATabSheet: TTabSheet);
var
 AUnit: TPanel;
 X, Y, W, H, S: Integer;
 P: Integer;
begin
 X:= 10; Y:= 10; W:= 75; H:= 75; S:= 5;
 frmDataMod.qryUnits.Active:= False;
 try
  frmDataMod.qryUnits.Parameters.ParamByName('locname').Value:= ATabSheet.Caption;
  frmDataMod.qryUnits.Active:= True;
  if frmDataMod.qryUnits.RecordCount > 0 then
  begin
  while not frmDataMod.qryUnits.EOF do
   begin
    AUnit:= TPanel.Create(ATabSheet);
    AUnit.Parent:= ATabSheet;
    AUnit.ParentColor:= False;
    AUnit.BevelOuter:= bvRaised;
    AUnit.BevelInner:= bvLowered;
    AUnit.ParentBackground := False;
    AUnit.Color:= clGreen;
    AUnit.Left:= X;
    AUnit.Top:= Y;
    AUnit.Width:= W;
    AUnit.Height:= H;
    AUnit.Caption:= frmDataMod.qryUnits.fieldByName('unit_number').AsString + ' - ' + frmDataMod.qryUnits.fieldByName('unit_type').AsString;
    frmDataMod.qryUnits.Next;
    P:= X + W + S;
    if (P + W ) < ATabSheet.ClientWidth then
     X:= P //X = X + Width + space
    else
    begin
     X:= 10;
     Y:= Y + H + S; //Y = Y + Hieght + Space
    end;
   end;
  end;//if
 except
 begin
  ShowMessage('Error retrieving units to display on location pages!');
 end;
 end;//try
end;

procedure TfrmMain.UpdateLocations;
var
 Locations: TStringList;
 I: Integer;
 APage: TTabSheet;
begin
 RemoveLocations;
 Locations:= TStringList.Create;
 frmDataMod.GetLocations(Locations, False);
 for I:= 0 to Locations.Count-1 do
 begin
  APage:=  TTabSheet.Create(pgeMain);
  APage.PageControl:= pgeMain;
  APage.Parent:= pgeMain;
  APage.Caption:= Locations[I];
  UpdateUnits(APage);
 end;
 Locations.Free;
end;


procedure TfrmMain.RemoveLocations;
var
 I: Integer;
begin
 for I:= pgeMain.PageCount-1 downto 0 do
  pgeMain.Pages[I].Destroy;
end;
0
 
LVL 58

Expert Comment

by:cyberkiwi
ID: 34109846
ewangoya may just have your answer there.
I was most recently working in D5, so I was testing there.
If you are working with later/st versions with XP themes, then that would be the issue.

Link below for my own reference as well.

https://forums.codegear.com/thread.jspa;jsessionid=2CE3A47AFD8C54BEC6BD96A22BF71DED?messageID=183851𬸫
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Delphi XE10 Round Image 2 160
select query - oracle 16 100
Need Help Delphi 2010 CheckBox1 Stored value in memo 13 73
update joined tables 2 55
The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

821 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