2014-09-05 37 views
0

如何在c#中獲取TableLayoutPanel右側的圖片框圖像?目前我得到兩個圖像,但我不需要。我只需要一個在極端右側圖像他們,我需要兩行在桌面佈局如何保持圖片框右側以及如何減少窗口形式的兩排圖像之間的空間

  for (int i = 0, r = 0; i < dt.Rows.Count; i++, r++) 
     { 
      PictureBox pb = new PictureBox(); 
      pb.ImageLocation = ../imagesDT/answered.gif 
      tableLayoutPanel1.Controls.Add(pb); 
     } 

下面之間移除空間我的連接如上所述的圖像我收到的問題。 enter image description here

+0

你解決了你的問題嗎? – TaW 2014-09-09 13:55:38

+0

工作,但不完全是我所需要的。在圖像之前,它們將是兩個標籤,第二個標籤文本會到另一個行,當我將圖像放置在右側時,所有文本流都會丟失。@ TaW – user2837291 2014-09-10 06:30:03

+0

也許您應該1)告訴我們表格的佈局(數量列)和2)包括現在看起來像的新形象。 Yopu將不得不按照正確的順序添加所有控件,並且不會留下任何空白或設置右列和可能的行位置。 – TaW 2014-09-10 07:11:56

回答

0

您需要設置列和行的PictureBoxes應坐下:

for (int i = 0, r = 0; i < dt.Rows.Count; i++, r++) 
{ 
    int maxCol = tableLayoutPanel1.ColumnCount - 1; 
    PictureBox pb = new PictureBox(); 
    pb.ImageLocation = ../imagesDT/answered.gif 
    tableLayoutPanel1.Controls.Add(pb); 
    tableLayoutPanel1.SetRow(pb, i); 
    tableLayoutPanel1.SetColumn(pb, maxCol); 
} 

你可以得到列的寬度和行高地這樣的:

var widths = tableLayoutPanel1.GetColumnWidths(); 
var heights = tableLayoutPanel1.GetRowHeights(); 

你可以通過設置邊距來微調位置,例如:

pb.Margin = New Padding(50, 20, 0, 0); 

要改變He行ights您可以使用設計器或者你可以在每一行的RowStyle設置其身高:

tableLayoutPanel1.RowStyles[row].SizeType = SizeType.Absolute; 
tableLayoutPanel1.RowStyles[row].Height = newHeight; 

爲了讓您在您的測試表佈局可見,你可能想在插入這樣的代碼CellPaint事件:

private void tableLayoutPanel1_CellPaint(object sender, TableLayoutCellPaintEventArgs e) 
{ 
    Random R = new Random(1 + e.Row * 3 + e.Column * 7); 
    using (SolidBrush brush = new SolidBrush(
      Color.FromArgb(99, Color.FromArgb(R.Next(123456789))))) 
     e.Graphics.FillRectangle(brush, e.CellBounds); 
} 
相關問題