2012-04-21 11 views
0

好的,我試圖在運行時在TScrollBox表面上創建一些自定義數量的TPanel,就像您可以在以下圖像中看到的一樣。動態TImage在動態TPanel的?

enter image description here

爲了得到這個我用下面的代碼,並且工作正常。

var 
    pan: array of TPanel; 
    maxp, i, x, y: Integer; 

... 

maxp := 10; 
SetLength(pan, maxp); 

for i := 1 to maxp do begin 
    // x is correct value; doesn't cause problem 
    // y is correct value; doesn't cause problem 
    pan[i-1] := TPanel.Create(form1); 
    with pan[i-1] do begin 
    Width := 100; 
    Height := 150; 
    Top := x * 151; 
    Left := y * 101; 
    Parent := ScrollBox1; 
    end; 
    end; 

現在,我有問題放的TImage對象在每TPanel與相同索引(IMG [0] - >泛[0],IMG [1] - >泛[1],等等)。請看下面的圖片:

enter image description here

使用相同的邏輯,我試圖創建的TImage的,但瓦特/沒有成功。

我正在使用此代碼,並不能找出什麼是錯的。它對我來說看起來很簡單,但它不會提供預期的效果。

var 
    pan: array of TPanel; 
    img: array of TImage; 
    maxp, i, x, y: Integer; 

... 

maxp := 10; 
SetLength(pan, maxp); 
SetLength(img, maxp); 

for i := 1 to maxp do begin 
    // x is correct value; doesn't cause problem 
    // y is correct value; doesn't cause problem 
    pan[i-1] := TPanel.Create(form1); 
    with pan[i-1] do begin 
    Width := 100; 
    Height := 150; 
    Top := x * 151; 
    Left := y * 101; 
    Parent := ScrollBox1; 
    end; 
    img[i-1] := TImage.Create(form1); 
    with img[i-1] do begin 
    Width := 98; 
    Left := 1; 
    Height := 148; 
    Top := 1; 
    // in original code next line had img[0]. which caused problem 
    Picture.LoadFromFile('some_image_file'); 
    Parent := pan[i-1]; 
    end; 
    end; 

不知何故,它將所有TImage對象放置在第一個TPanel(pan [0])的相同位置。這對我來說很困惑,因爲它說Parent := pan[i-1];,但由於某種原因,它總是將TImage放在pan [0]中。我已經嘗試過使用斷點來查看每個for循環週期後發生了什麼(最後添加了Application.ProcessMessages),它確實創建了10個不同的映像,但將它們放到了pan [0]上。當然,最後它只顯示加載到pan [0]中的最後一個圖像。

我的問題是如何爲每個動態TPanel製作一個動態TImage(具有相同的數組索引)?

已解決!

+0

也許更好用,以'一個調用做到這一點SetBounds' – 2012-04-21 21:47:14

+0

@DavidHeffernan - 對不起,我一直在使用'的setBounds(1,1,98,148)審判;'和'SetBouds(X * 151 ,y * 101,100,150)而不是設置單獨的Width,Height,Left和Top屬性,但效果是相同的。 – Wh1T3h4Ck5 2012-04-21 21:53:12

+0

確定效果是一樣的。我不是故意回答你的問題。只是它更乾淨。 – 2012-04-21 21:54:54

回答

1

啊,我發現它...我怎麼瞎我真的...

要獲得自動完成在Delphi中,我已經在前面Picture.LoadFromFile()使用img[0]。然後,顯然我忘了將它從代碼中刪除,並且從小時以前,'前綴'留在那裏使所有圖像加載到相同的img [0]。我確定Parent或Pos/Size屬性有問題,並且一直專注於這些不關心這個問題的東西。

其實我有

with img[i-1] do begin 
    Width := 98; 
    Left := 1; 
    Height := 148; 
    Top := 1; 
    img[0].Picture.LoadFromFile('some_image_file'); 
    Parent := pan[i-1]; 
    end; 

但不知何故,我已刪除的img [0]部分,同時發佈了這個問題,並沒有看到它在我的Delphi代碼的問題。很明顯,當我被格式化這段代碼,我去掉了一些部件,這讓這裏不可能:(

實在不好意思回答我的問題打擾你們,這是我的壞

+2

就我而言,沒有什麼不對的:)有時候別人比我們更容易發現我們的錯誤,不管他們有多麼平庸。所以在SO上發佈這樣的問題沒有任何問題。 – Tibor 2012-04-21 22:25:46

+0

@Tibor,據我所知,但也因爲'Parent:= pan [i-1];'這行總是在'pan [0]'中顯示圖像而讓我感到沮喪,這讓我的注意力集中在以前。最糟糕的是,我正在將我的頭靠在牆上一個小時,試圖修復某些完全正確的邏輯。猜猜我太累了,看到明顯的錯誤:( – Wh1T3h4Ck5 2012-04-21 22:45:26

2

您設置了Height兩次,並沒有Left,所以它似乎。

with pan[i-1] do begin 
    Width := 100; 
    Height := 150; 
    Top := x * 151; 
    Height := y * 101; 
    Parent := ScrollBox1; 
end; 
+1

+1爲你的努力。剛剛發現錯誤。這裏沒什麼不對,另一個錯字。正如你從上面的圖片可以看到,面板位於正確的位置。不過,多謝m8。 – Wh1T3h4Ck5 2012-04-21 22:21:35

5

和建議的話 - 擺脫with塊,它們看起來似乎是無辜和簡單的,但從長遠來看,它們只能用於編寫難以排除故障的潦草代碼。如果您使用顯式變量引用,那麼這個問題在第一次就不會發生地點。

var 
  Panels: array of TPanel; 
    Panel: TPanel; 
  Images: array of TImage; 
    Image: TImage; 
  maxp, i, x, y: Integer; 

... 

maxp := 10; 
SetLength(Panels, maxp); 
SetLength(Images, maxp); 

for i := 1 to maxp do begin 
  Panel := TPanel.Create(form1); 
    Panels[i-1] := Panel; 
    Panel.Parent := ScrollBox1; 
  Panel.SetBounds(...); 
  Image := TImage.Create(form1); 
    Images[i-1] := Image; 
    Image.Parent := Panel; 
    Image.SetBounds(...); 
  Image.Picture.LoadFromFile('some_image_file'); 
end; 
+0

+1感謝您的建議,你是絕對正確的。不幸的是,我發現,在困難的方式。 – Wh1T3h4Ck5 2012-04-22 16:40:53