C#tablelayoutpanel无法删除填充/边距

时间:2015-09-05 18:48:45

标签: c# winforms margin padding tablelayoutpanel

基本上我在删除tablelayoutpanel中控件之间的填充/边距时遇到问题。

我已将tablelayoutpanel边距和填充设置为0。 Cellborderstyle没有。

对于边距和填充,添加的控件都设置为0。

然而,这种神秘的边缘不断出现。有什么帮助吗?

在这里尝试了这两个解决方案以及网络上的其他各种解决方案,但没有一个可以消除它们之间的间距。

在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;
    }
}
}

设计

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;
    }
}

2 个答案:

答案 0 :(得分:4)

你正在追逐错误的问题,这个差距是由Button控件引起的,而不是TableLayoutPanel引起的。当您使用设计师在表单上拖放按钮时可以清楚地看到的东西,请注意选择矩形和按钮表面之间的间隙。

您需要修改按钮以摆脱它或使用其他类型的控件。最简单的方法就是让它平坦:

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

如果要查看网格,请删除BorderSize分配。

答案 1 :(得分:0)

我在TableLayoutPanel

中使用不同控件时遇到了同样的问题

你可以这样做

  1. 转到设计视图
  2. 点击属性
  3. GoTo Columns,当你点击Columns旁边的文本框时,文本框的最右边会出现一个按钮(...),点击它
  4. 出现一个弹出窗口,选择AutoSize(而不是Absolute或Percentage)。
  5. Show:的同一窗口中选择Rows,然后再次选择自动调整大小。
  6. 点击即可,您已完成。