Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

TImage -> created dynamically

Status
Not open for further replies.

mischmasch

Programmer
May 10, 2011
11
Hi,
I have problem with dynamic creation of TImage on TTabSheet. To make it, I use code below but it doesn't work - I don't see any Image on TabSheet. I don't know what I do wrong. Anybody knows?

-------------
On Form, I have PageControl where I want to create some number of TabSheet, for example 4 sheets, so my code looks like below:

procedure DoSomething;
var
NewTabSheet: TTabSheet;
NewImage: TImage;
i: Integer;​
begin

for i := 0 to 3 do
begin
NewTabSheet := TTabSheet.Create(Form.PageControl);
NewTabSheet.PageControl := Form.PageControl;
NewTabSheet.Name := 'MySheet' + IntToStr(i);
NewTabSheet.Caption := IntToStr(i);

NewImage := TImage.Create(NewTabSheet);
NewImage.Parent := NewTabSheet;
NewImage.Name := 'imgMyPicture' + IntToStr(i);
NewImage.Align := alClient;
NewImage.Canvas.Font.Color := clGray;
NewImage.Canvas.TextOut(10, 10, 'Page: ' + IntToStr(i));​
end;​
end;​
 
Set the parent of the TImage and you should be fine.

Code:
procedure TForm1.Button1Click(Sender: TObject);
// puts new MyImage on currently selected tab sheet of pagecontrol1.
begin
  if OpenDialog1.Execute then
    begin
      MyImage := TImage.Create(self);
      MyImage.Picture.LoadFromFile(OpenDialog1.FileName);
      MyImage.Parent := PageControl1.ActivePage;
    end;
end;

If nothing is showing up, where are you getting the image data from, and are you sure it exists?

It is not possible for anyone to acknowledge truth when their salary depends on them not doing it.
 
I don't load image from file, I use Canvas.TextOut.

If I load image from file, everything is OK but it's not what I want.
 
Hi,

Try a test with the code below:

Code:
procedure TForm1.Button1Click(Sender: TObject);

  procedure DoSomething;
  var
    MyPageControl: TPageControl;
    NewTabSheet: TTabSheet;
    NewImage: TImage;
    i: Integer;
  begin
    MyPageControl        := TPageControl.Create(Self);
    MyPageControl.Parent := Self;
    MyPageControl.Align  := alClient;
    for i := 0 to 3 do
    begin
      NewTabSheet := TTabSheet.Create(Self);
      NewTabSheet.PageControl := MyPageControl;
      NewTabSheet.Name := 'MySheet' + IntToStr(i);
      NewTabSheet.Caption := IntToStr(i);

      NewImage := TImage.Create(NewTabSheet);
      NewImage.Parent := NewTabSheet;
      NewImage.Name   := 'imgMyPicture' + IntToStr(i);
      //NewImage.Align  := alClient;
      NewImage.Left   := 0;
      NewImage.Top    := 0;
      NewImage.Width  := NewTabSheet.ClientWidth;
      NewImage.Height := NewTabSheet.ClientHeight;
      NewImage.Canvas.Brush.Color := clYellow;
      NewImage.Canvas.FillRect(Rect(0, 0, NewImage.Width, NewImage.Height));
      NewImage.Canvas.Font.Color  := clGray;
      NewImage.Canvas.TextOut(10, 10, 'Page: ' + IntToStr(i));
    end;
  end;

begin
  DoSomething;
end;

Hope it's useful.

[URL unfurl="true"]http://www.imoveisemexposicao.com.br/imobiliarias-em-guarulhos[/url]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top