2015-09-05 67 views
6

基本上我正面臨一個問題,即刪除tablelayoutpanel中控件之間的填充/邊距。C#tablelayoutpanel無法刪除填充/保證金

我已經將tablelayoutpanel margin和padding設置爲0. Cellborderstyle是none。

對於邊距和填充,添加的控件都設置爲0。

然而,神祕的邊緣不斷出現。任何幫助?

嘗試這2種解決方案在這裏和其他各種網絡,但沒有人可以刪除之間的間距。

運行於VS2010,.NET 4.5

Form1.cs的

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Windows.Forms; 

namespace Multiply { 
    public partial class Form1 : Form { 
     public Form1() { 
      InitializeComponent(); 
      LoadBoard(10, 10); 
     } 

    private void LoadBoard(int col, int row) { 
     board.ColumnCount = col; 
     board.RowCount = row; 
     board.BorderStyle = BorderStyle.FixedSingle; 

     float colSize = 100f/col; 
     float rowSize = 100f/row; 

     board.ColumnStyles[0].SizeType = SizeType.Percent; 
     board.ColumnStyles[0].Width = colSize; 
     board.RowStyles[0].SizeType = SizeType.Percent; 
     board.RowStyles[0].Height = rowSize; 


     for (int x = 0; x < col; x++) 
      board.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, colSize)); 

     for (int x = 0; x < row; x++) { 
      board.RowStyles.Add(new RowStyle(SizeType.Percent, rowSize)); 
      for (int y = 0; y < col; y++) { 
       board.Controls.Add(CreateButton(x + "," + y), y, x); 
      } 
      Console.WriteLine(x); 
     } 

    } 

    private Button CreateButton(string text) { 
     Button a = new Button(); 
     a.Text = text; 
     a.Dock = DockStyle.Fill; 
     a.Margin = new Padding(0); 
     a.Padding = new Padding(0); 
     return a; 
    } 
} 
} 

德西gner

namespace Multiply { 
    partial class Form1 { 
     /// <summary> 
     /// Required designer variable. 
     /// </summary> 
     private System.ComponentModel.IContainer components = null; 

     /// <summary> 
     /// Clean up any resources being used. 
     /// </summary> 
     /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param> 
     protected override void Dispose(bool disposing) { 
      if (disposing && (components != null)) { 
       components.Dispose(); 
      } 
      base.Dispose(disposing); 
     } 

     #region Windows Form Designer generated code 

     /// <summary> 
     /// Required method for Designer support - do not modify 
     /// the contents of this method with the code editor. 
     /// </summary> 
     private void InitializeComponent() { 
      this.board = new System.Windows.Forms.TableLayoutPanel(); 
      this.SuspendLayout(); 
      // 
      // board 
      // 
      this.board.ColumnCount = 1; 
      this.board.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 25F)); 
      this.board.Dock = System.Windows.Forms.DockStyle.Top; 
      this.board.Location = new System.Drawing.Point(0, 0); 
      this.board.Margin = new System.Windows.Forms.Padding(0); 
      this.board.Name = "board"; 
      this.board.RowCount = 1; 
      this.board.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Absolute, 20F)); 
      this.board.Size = new System.Drawing.Size(368, 223); 
      this.board.TabIndex = 0; 
      // 
      // Form1 
      // 
      this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); 
      this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; 
      this.ClientSize = new System.Drawing.Size(368, 391); 
      this.Controls.Add(this.board); 
      this.Name = "Form1"; 
      this.Text = "Form1"; 
      this.ResumeLayout(false); 

     } 

     #endregion 

     private System.Windows.Forms.TableLayoutPanel board; 
    } 
} 

回答

3

你正在追逐錯誤的問題,這個差距是由Button控件造成的,而不是TableLayoutPanel造成的。當你用設計師在表單上放下一個按鈕時,你可以清楚地看到一些東西,注意選擇矩形和按鈕表面之間的間隙。

您需要修改按鈕才能擺脫它或使用其他類型的控件。最簡單的方法是使其變平:

private Control CreateButton(string text) { 
     var a = new Button(); 
     a.FlatStyle = FlatStyle.Flat; 
     a.FlatAppearance.BorderSize = 0; // optional 
     // etc... 
    } 

如果要查看網格,請刪除BorderSize分配。

+0

謝謝!那肯定工作得很好。 – Tim

0

我同時使用不同的控制TableLayoutPanel

你可以做到這一點

  1. 轉到設計視圖
  2. 單擊屬性
  3. 轉到列,當您單擊文本面臨着同樣的問題除了列以外,按鈕(...)出現在文本框的最右端,點擊它
  4. 出現一個彈出窗口,選擇AutoSize(而不是絕對或百分比)。
  5. Show:的同一窗口中選擇Rows並再次選擇自動調整大小。
  6. 點擊好,你就完成了。