2013-02-05 36 views
2

我在我的TableLayoutPanel中動態添加行,但我無法配置那裏的高度。如何在TableLayoutPanel中設置行的高度

該代碼可能看起來很長,但它是一個非常簡單的代碼。

有關代碼的解釋:

的代碼創建一個TableLayoutPanel並設置其屬性。之後,根據數據庫中有多少部電影,該代碼將創建PictureboxesLabels。在創建PictureboxLabel後,該代碼將它們都放在Panel中,然後代碼將Panel插入到TableLayoutPanel中。問題在於排的高度。

輸出:

enter image description here

這裏是我使用的代碼:

Dim movieN As Integer = MoviesDataSet.movies.Rows.Count 
    Dim tablePanel As New TableLayoutPanel 

    With tablePanel 
     .Size = New Point(Me.ClientRectangle.Width - 10, Me.ClientRectangle.Bottom - 55) 
     .ColumnCount = 4 
     .GrowStyle = TableLayoutPanelGrowStyle.AddRows 
     .AutoScroll = True 
     .Margin = New System.Windows.Forms.Padding(0) 
     .Location = New System.Drawing.Point(5, 50) 
     .CellBorderStyle = TableLayoutPanelCellBorderStyle.Inset 
     .ColumnStyles.Add(New ColumnStyle(SizeType.Percent, 25.0!)) 
     .ColumnStyles.Add(New ColumnStyle(SizeType.Percent, 25.0!)) 
     .ColumnStyles.Add(New ColumnStyle(SizeType.Percent, 25.0!)) 
     .ColumnStyles.Add(New ColumnStyle(SizeType.Percent, 25.0!)) 
     .Anchor = AnchorStyles.Bottom Or AnchorStyles.Left Or AnchorStyles.Right Or AnchorStyles.Top 
    End With 


    For Each MovieRow As DataRow In MoviesDataSet.Tables("movies").Rows 
     'define two new controls to be added 
     Dim myLabel As New Label 
     Dim myPicture As New PictureBox 
     Dim container As New Panel 

     'set the properties of the new controls 
     myLabel.Text = MovieRow("movieName") 
     myLabel.AutoSize = True 
     myLabel.Location = New System.Drawing.Point(30, 110) 
     With myPicture 
      .Image = Image.FromFile(MovieRow("moviePhoto")) 
      .Tag = MovieRow("ID") 
      .Size = New System.Drawing.Size(100, 100) 
      .SizeMode = PictureBoxSizeMode.StretchImage 
      .Location = New System.Drawing.Point(2, 2) 
      .Cursor = Cursors.Hand 
     End With 

     'here we add the controls to a layout panel to 
     'manage the positioning of the controls 
     With container 
      .Dock = DockStyle.Fill 
      .Margin = New System.Windows.Forms.Padding(0) 
      .Controls.Add(myPicture) 
      .Controls.Add(myLabel) 
     End With 


     With tablePanel.Controls 

      .Add(container) 

     End With 

     'here we add a handler for the picture boxs click event 
     AddHandler myPicture.Click, AddressOf MyPictureClickEvent 
    Next 

    Me.Controls.Add(tablePanel) 
End Sub 

提前感謝!

+0

是最後一行困擾着你? – AbZy

+0

一切都在困擾着我。第一行沒有顯示標籤,這是因爲沒有足夠的高度給予該行,並且我不知道該怎麼做。 我想將所有**行的高度設置爲180 – kfirba

回答

0

我得到了一個答案。爲了設置行的高度,所有你需要做的就是要補充一點:添加PanelTableLayoutPanel

一個片段後

tablePanel.RowStyles.Add(New RowStyle(SizeType.Absolute, 150)) 

你應該加入這一行

 '.... THE CODE ABOVE CAN BE SEEN IN THE QUESTION POST 
     With container 
      .Dock = DockStyle.Fill 
      .Margin = New System.Windows.Forms.Padding(0) 
      .Controls.Add(myPicture) 
      .Controls.Add(myLabel) 
     End With 


     With tablePanel.Controls 

      .Add(container) 

     End With 
     tablePanel.RowStyles.Add(New RowStyle(SizeType.Absolute, 150)) 
     'here we add a handler for the picture boxs click event 
     AddHandler myPicture.Click, AddressOf MyPictureClickEvent 
    Next 

    Me.Controls.Add(tablePanel) 
End Sub 

希望幫助別人

8

試試這個:

For Each RS As RowStyle In tablePanel.RowStyles  
    RS.SizeType = SizeType.Absolute   
    RS.Height = 180  
Next 
+0

感謝您的回覆,但我找到了解決方案! – kfirba