Delphi 2009 - create a TPanel at runtime and change its color
Asked Answered
K

4

14

got a strange problem: I create a TPanele at runtime and change its color - however, the color is still clBtnFace.

Here' the code:

procedure TForm1.Button1Click(Sender: TObject);
var
  pnlTest : TPanel;
begin
    pnlTest := TPanel.Create(Form1);
    pnlTest.Parent := Form1;
    pnlTest.Width := 100;
    pnlTest.Height := 100;
    pnlTest.Color := clRed;
end; 

Any ideas? Thanks!

Kauffmann answered 23/9, 2010 at 12:5 Comment(0)
R
21

When you want to have colored panels under a themed OS you have to set ParentBackground to False.

Refuse answered 23/9, 2010 at 12:21 Comment(3)
Ah sorry, it works! I set ParentColot to false, not ParentBackground. Thx!Kauffmann
This does not seem to be the case if the TPanel was added to a form at design time. Even after setting the Color property to clWhite, and the ParentBackground and ParentColor properties to false, at runtime the panel still shows with a grey background, even though the Form background is also clWhite. This may be related to the Default style selection in the Project Options.Fuji
This only seems to work if you have the style set to Windows. When you change the style, it reverts to the default style colour.Zermatt
V
1

Try this :-)

procedure TForm1.Button1Click(Sender: TObject);
var
  pnlTest : TPanel;
begin
    pnlTest := TPanel.Create(Form1);
    pnlTest.Parent := Form1;
    pnlTest.Width := 100;
    pnlTest.Height := 100;
    pnlTest.ParentBackground := false;
    pnlTest.Color := clRed;
end;
Vespertine answered 1/11, 2015 at 12:14 Comment(0)
E
1

This the code for maXbox scripting:

procedure SetArrayLength2Panels(var arr: array of array of TPanel;
                                            asize1, asize2: Integer);
var i: Integer;
begin setlength(arr, asize1);
   for i:= 0 to asize1-1 do SetLength(arr[i], asize2);
end;

procedure TMyFormInitialisePanels(aform: Tform; RowCount,ColCount: Integer);
var
  aLeft,aTop,aWidth,aHeight, row,col: Integer;
  Panel: TPanel;
  FPanels: array of array of TPanel;
begin
  //SetLength(FPanels, RowCount, ColCount);
  SetArrayLength2Panels(Fpanels, RowCount, ColCount)
  aTop:= 0;
  for Row:= 0 to RowCount-1 do begin
    aLeft:= 0;
    aHeight:= (aform.ClientHeight-aTop) div (RowCount-Row);
    for Col:= 0 to ColCount-1 do begin
      Panel:= TPanel.Create(Self);
      FPanels[Row][Col]:= Panel;
      Panel.Parent:= aform; //Self;
      //panel.parentcolor:= false;
      panel.ParentBackground:= false;
      panel.color:= random(clred)
      aWidth:= (aform.ClientWidth-aLeft) div (ColCount-Col);
      Panel.SetBounds(aLeft, aTop, aWidth, aHeight);
      inc2(aLeft, aWidth);
    end;
    inc2(aTop, aHeight);
  end;
end;
Encumber answered 7/2, 2021 at 19:31 Comment(1)
and the call aform:= TForm.create(self) aform.setbounds(10,10,600,500); TMyFormInitialisePanels(aform, 26, 19); aform.show;Encumber
A
0

For those of you, cpp builder developers who arrive here:

ParentBackground, ParentColor and FullRepaint should be set to false

Alpenstock answered 22/5 at 23:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.