Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# tablelayoutpanel unable to remove padding/margin

Basically I'm facing a problem with removing the padding/margin between controls in a tablelayoutpanel.

I've set the tablelayoutpanel margin and padding to 0. Cellborderstyle is none.

Added controls are all set to 0 for margin and padding.

Yet that mysterious margin keeps appearing. Any help?

Tried this 2 solutions here and various other on the web but none could remove that spacing in between.

  • Remove spacing between cells in tablelayoutpanel in Windows form?

  • Is there any way to control thickness of cell border in TableLayoutPanel?

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

Designer

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;
    }
}
like image 784
Tim Avatar asked Sep 05 '15 18:09

Tim


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C in C?

Originally Answered: What is the full form of C ? C - Compiler . C is a general-purpose, high-level language that was originally developed by Dennis M. Ritchie to develop the UNIX operating system at Bell Labs.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.

What is C in C language?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.


1 Answers

You are chasing the wrong problem, this gap is caused by the Button control, not the TableLayoutPanel. Something you can clearly see when you drop a button on the form with the designer, note the gap between the selection rectangle and the button surface.

You'll need to tinker with the button to get rid of it or use a different kind of control. Simplest way is to make it flat:

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

Remove the BorderSize assignment if you want to see a grid.

like image 148
Hans Passant Avatar answered Sep 20 '22 04:09

Hans Passant