天道酬勤,学无止境

Cannot remove spacing between controls in TableLayoutPanel?

There's some spacing between the Buttons I add to my TableLayoutPanel. I removed the border in the Button and set the Margin and Padding to 0 in the Panel. But I continue getting that spacing.

tableLayoutPanel.RowCount is set to 8 and the Rows collection I've added 8 rows with Size Type Absolute.

Am I missing something? Here's the code:

    private void FillSelectLayout()
    {
        tableLayoutPanelSelect.Controls.Clear();
        tableLayoutPanelSelect.RowStyles.Clear();

        tableLayoutPanelSelect.RowCount = 8;

        for (int i = 0; i < 8; i++)
        {
            Button buttonSelector = new Button();
            buttonSelector.Height = 64;
            buttonSelector.Width = 100;
            buttonSelector.FlatStyle = FlatStyle.Flat;
            buttonSelector.FlatAppearance.BorderSize = 0;
            buttonSelector.BackColor = Color.Yellow;
            tableLayoutPanelSelect.Controls.Add(buttonSelector, 0, i);
        }
    }

Here's how it's displayed:

评论

To remove the space between buttons in cells, it's enough to set dock property of them to fill and then remove default margins of buttons:

var b = new Button();
b.Dock = DockStyle.Fill;
b.Margin = new Padding(0);

Note:

  • Usually it's better to set Dock property of controls which you host in cells to Fill. This way your controls will follow TableLayouPanel sizing rules which you set for columns and rows.

  • TableLayoutPanel use Margin property of control to set the location of control in cell. So If you don'n want to set Dock and you prefer to set the Size manually, it's enough to set the Margin only.

I .. set the Margin and Padding to 0 in the Panel.

Why didn't you remove the Margin in the Buttons instead:

buttonSelector.Margin = new Padding(0);

MSDN:

The Margin property defines the space around the control that keeps other controls a specified distance from the control's borders.

I faced the same problem while using different control in TableLayoutPanel

You can do this

  1. Go to Design View
  2. Click on the properties
  3. GoTo Columns, When you click text box besides Columns, a button (...) appears on extreme right, click it
  4. A pop up window appears, Select AutoSize (Instead of Absolute or Percentage).
  5. In the same window in Show: select Rows and again select Autosize.
  6. Click okay and you are done.

受限制的 HTML

  • 允许的HTML标签:<a href hreflang> <em> <strong> <cite> <blockquote cite> <code> <ul type> <ol start type> <li> <dl> <dt> <dd> <h2 id> <h3 id> <h4 id> <h5 id> <h6 id>
  • 自动断行和分段。
  • 网页和电子邮件地址自动转换为链接。

相关推荐
  • Remove Row inside TableLayoutPanel makes a layout problem
    I have a WinForms application that has a TableLayoutPanel; this is the definition code: tableLayoutPanel1 = new TableLayoutPanel(); tableLayoutPanel1.Dock = DockStyle.Fill; tableLayoutPanel1.AutoScroll = true; tableLayoutPanel1.RowCount = users.Count + 1; tableLayoutPanel1.ColumnCount = 1; tableLayoutPanel1.GrowStyle = TableLayoutPanelGrowStyle.FixedSize; tableLayoutPanel1.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 100F)); foreach (String user in users) { tableLayoutPanel1.RowStyles.Add(new RowStyle(SizeType.Absolute, 600F)); } tableLayoutPanel1.RowStyles.Add(new RowStyle(SizeType
  • TableLayoutPanel显示垂直滚动(TableLayoutPanel displays vertical scroll)
    问题 我有TableLayoutPanel用于使用AutoScroll = true动态创建控件。 当我添加新控件时,它工作正常。 但是,当我删除并且所有控件都可见时,垂直滚动是可见的。 一些截图在这里: 预期/正确的滚动可见性: 可见性不正确: 有任何想法吗? 更新:这是一些代码 tableLayoutPanel1.SuspendLayout(); tableLayoutPanel1.RowCount = 0; tableLayoutPanel1.RowStyles.Clear(); tableLayoutPanel1.AutoScroll = true; tableLayoutPanel1.Padding = new Padding(0, 0, SystemInformation.VerticalScrollBarWidth, 0); foreach (var item in objects) { tableLayoutPanel1.RowCount++; tableLayoutPanel1.RowStyles.Add(new RowStyle(SizeType.AutoSize)); tableLayoutPanel1.Controls.Add(CreateNewItem(item)); } tableLayoutPanel1.RowCount++
  • Winforms TableLayoutPanel以编程方式添加行(Winforms TableLayoutPanel adding rows programmatically)
    问题 我已经为此进行了一段时间的战斗,并且发现许多其他人也为TableLayoutPanel(.net 2.0 Winforms)感到挣扎。 问题 我正在尝试采用“空白” tablelayoutpanel,其中定义了10列,然后在运行时以编程方式添加控件行(即,每个单元格一个控件)。 可能有人以为它应该像 myTableLayoutPanel.Controls.Add(myControl, 0 /* Column Index */, 0 /* Row index */); 但这(对我而言)不会添加行。 所以也许添加一个行样式 myTableLayoutPanel.RowStyles.Clear(); myTableLayoutPanel.RowStyles.Add(new RowStyle(SizeType.Absolute, 30F)); 但这也不起作用。 我进行了挖掘,发现myTableLayoutPanel.RowCount用法在设计时间与运行时间之间发生了变化,因此进行了myTableLayoutPanel.RowCount++; 实际上并没有添加另一行,甚至没有为它添加RowStyle条目之前/之后! 我遇到的另一个相关问题是控件将被添加到显示中,但是它们都只是在TableLayoutPanel的0,0点处呈现,此外
  • TableLayoutPanel displays vertical scroll
    I have TableLayoutPanel for dynamic creation of controls with AutoScroll = true. It's work fine when I add new controls. But when I remove and all controls are visible, vertical scroll is visible. Some screenshots here: Expected/correct scroll visibility: Incorrect visibility: Any ideas? Update: Here is some code tableLayoutPanel1.SuspendLayout(); tableLayoutPanel1.RowCount = 0; tableLayoutPanel1.RowStyles.Clear(); tableLayoutPanel1.AutoScroll = true; tableLayoutPanel1.Padding = new Padding(0, 0, SystemInformation.VerticalScrollBarWidth, 0); foreach (var item in objects) { tableLayoutPanel1
  • 如何在Winforms设计器中访问继承的控件(How to access inherited controls in the winforms designer)
    问题 我正在制作一些控件,这些控件必须具有相同的外观和一些共同的行为,尽管它们是为不同种类的输入而设计的。 因此,我做了一个继承自UserControl的BaseClass,而我所有的控件都继承自BaseClass。 但是,如果我在设计器中添加了针对BaseClass的控件(如TableLayoutPanel),则在设计继承的类时将无法访问它们。 我看到了TableLayoutPanel,但是即使他是“受保护的”,我也无法通过设计器修改它或将控件放入其中。 我可以通过代码轻松访问它,但是我不想失去使用设计器的能力。 现在,我只是从BaseClass中删除了所有控件,在每个继承的类中添加了布局和所有公共控件,然后使用引用在BaseClass中对其进行操作。 但这一点都不令我满意。 有没有一种方法可以使设计器与继承的受保护成员控件一起使用? 环境:C#、. NET 3.5,Visual Studio 2008 编辑以回答SLaks的建议。 我尝试设置一个属性,尽管我不习惯使用它们,但它似乎不起作用。 这是我尝试的代码: public partial class UserControl1 : UserControl { public UserControl1() { InitializeComponent(); } public TableLayoutPanel
  • 在网格布局中创建动态按钮-创建魔术正方形UI(Create dynamic buttons in a grid layout - Create a magic square UI)
    问题 我应该使用Windows Forms Application在2D中创建一个魔术方块。 它看起来应该像这样: 但是,用户应该能够决定正方形的大小(3x3、5x5、7x7等)。 我已经在控制台应用程序中编写了代码,但是我不知道如何添加2D图形。 有人已经问过这个问题(如何将结果放入GUI中?),答案之一是使用DataGridView ,但是我不确定那不是我要的内容,因为我做不到看起来像图片。 有什么想法或建议吗? 回答1 您可以使用TableLayoutPanel并向面板动态添加按钮。 如果不需要与按钮进行交互,则可以添加Label 。 动态创建正方形: public void CreateSquare(int size) { //Remove previously created controls and free resources foreach (Control item in this.Controls) { this.Controls.Remove(item); item.Dispose(); } //Create TableLayoutPanel var panel = new TableLayoutPanel(); panel.RowCount = size; panel.ColumnCount = size; panel.BackColor = Color
  • How to access inherited controls in the winforms designer
    I'm making some controls which all have to share the same look and some common behavior, although they are meant for different kind of inputs. So I made a BaseClass which inherit from UserControl, and all my controls inherit from BaseClass. However, if i add controls for BaseClass in the designer, such as a TableLayoutPanel, i can't access them when I'm designing the inherited classes. I see the TableLayoutPanel, but even though he is "protected", i can't modify it or put controls in it through the designer. I've no trouble accesing it by code, but i don't want to lose the ability to use the
  • C#WinForms垂直对齐的文本框等(C# WinForms Vertical Alignment for TextBox, etc)
    问题 我正在一个项目中,更新其WinForms应用程序UI以使其与尺寸更加一致。 默认情况下,即使使用相同的字体,TextBox和ComboBox控件的高度也不同。 我已经能够通过关闭AutoSize来调整文本框的大小,但是文本仍然紧靠控件的顶部,在下面留有空隙。 有什么办法可以使文本在控件中垂直居中吗? 回答1 如果要在AutoSize上关闭AutoSize ,则它必须是Label ,因为TextBox没有AutoSize属性。 Label的TextAlign属性的类型为ContentAligment ,因此您可以设置水平和垂直对齐方式。 由于各种无聊的原因,Windows中的TextBoxes旨在自动将其高度调整为所使用的字体。 要控制文本的高度并使其垂直居中,您可以快速创建一个自定义UserControl ,您可以使用该UserControl替换所有TextBoxes 。 在您的UserControl ,将BorderStyle设置为Fixed3D ,将BackColor为System.Window 。 添加一个TextBox并将其BorderStyle设置为None 。 在控件的Resize事件中,添加代码,使TextBox与用户控件的客户区具有相同的宽度(考虑边框像素),并将其左对齐(即textBox1.Left = 0; )并将其垂直居中(例如textBox1.Top
  • 从VSTO Outlook外接程序中打开WPF表单(Open WPF form from a VSTO outlook addin)
    问题 我在Thisaddin.cs中有此代码 public void Search(string input) { ServerList listofservers = new ServerList(); listofservers.Visibility; } ServerList是带有列表框的简单WPF表单,但是如何显示listofservers ? 我找不到listofserver.show(); 回答1 因此,首先没有称为WPF表单的项目,只有WPF的用户控件。 因此,一旦在XAML中创建了WPF UserControl,您就会注意到这是代码 <UserControl x:Class="SQL_openertak2.ServerList" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" mc:Ignorable="d" d
  • TableLayoutPanel GetCellPosition and GetPositionFromControl, how do they differ?
    This may seem a trite question but I find the Microsoft documentation on these methods lacking in any detail. What is the difference between TablelLayoutPanel.GetCellPosition(Control control) and TableLayoutPanel.GetPositionFromControl(Control control) ? I'm using .NET Framework 4
  • 在TableLayoutPanel中动态添加行(Add Row Dynamically in TableLayoutPanel)
    问题 我想在c#的Windows窗体的TableLayoutPanel中逐行动态添加这些条目 我怎样才能做到这一点? 回答1 试试下面的代码, // TableLayoutPanel Initialization TableLayoutPanel panel = new TableLayoutPanel(); panel.ColumnCount = 3; panel.RowCount = 1; panel.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 40F)); panel.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 30F)); panel.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 30F)); panel.RowStyles.Add(new RowStyle(SizeType.Absolute, 50F)); panel.Controls.Add(new Label() { Text = "Address" }, 1, 0); panel.Controls.Add(new Label() { Text = "Contact No" }, 2, 0); panel.Controls.Add(new
  • How do I feed values to the statusStrip from a form control?
    This is the context of my controls: /* Form StatusStrip ToolStripStatusLabel TableLayoutPanel MyGenioView */ So, MyGenioView is intercepting the MouseMove event handler. The code that is already there is for a rubber band rectangle. So I have: public void MyMouseMove(Object sender, MouseEventArgs e) { Point ptCurrent = new Point(e.X, e.Y); // If we "have the mouse", then we draw our lines. if (m_bHaveMouse) { // If we have drawn previously, draw again in // that spot to remove the lines. if (m_ptLast.X != -1) { MyDrawReversibleRectangle(m_ptOriginal, m_ptLast); } // Update last point. m_ptLast
  • Find row index of control added to TableLayoutPanel
    I need to take an action when a control is added to a TableLayoutPanel in Windows Forms. I am handling ParentChanged event of the control to find out if the control is added to a parent(here TableLayoutPanel), but the index I receive is -1. TableLayoutPanel t; private void button1_Click(object sender, EventArgs e) { // this.Text = tableLayoutPanel1.Height.ToString(); t = new TableLayoutPanel(); t.Dock = DockStyle.Fill; //t.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink; t.AutoSize = true; //t.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink; t.RowStyles.Add(new
  • WinForms Designer and TableLayoutPanel SmartTag Customization
    I am trying to customize the existing Smart Tag content for a TableLayoutPanel Windows Forms control for use in the Windows Forms designer (I implemented a designer that leverages the WinForms designer features exposed by the System.ComponentModel.Design and System.Windows.Forms.Design namespaces). Whatever approach is offered as a solution, it's got to also work when my control is added to the Visual Studio toolbox and when my control is placed on a WinForm surface in design mode while in the Visual Studio IDE. Here's what I'm trying to do: In design mode, a TableLayoutPanel control instance
  • Center multiple rows of controls in a FlowLayoutPanel
    I'm trying to make a panel that would host dynamically added controls. There are two caveats: There are going to be a lot of controls, so the panel should wrap the elements into new rows as it reaches its width limits and scroll vertically. Controls can change in size, which would change the number of elements that can fit into a single row. I've seen a couple proposed solutions to center dynamic controls in a Form and rejected those for following reasons: TableLayoutPanel - main issue I have with using this are the events when elements grown and have to shift from 3-2 grid to 2-4, as
  • 在运行时动态地将控件添加到TableLayoutPanel(Adding controls to TableLayoutPanel dynamically during runtime)
    问题 我有一个从两列零行开始的TableLayoutPanel。 我需要做的是,动态添加一行,并用不同的控件填充这两个列(将是面板)。 在Form1中,我以这种方式创建TableLayout: TableLayoutPanel Table = new TableLayoutPanel(); Table.Location = new Point(10, 40); Table.Size = new Size(620,100); Table.AutoSize = true; Table.Name = "Desk"; Table.ColumnCount = 2; Table.RowCount = 0; Table.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink; Table.GrowStyle = System.Windows.Forms.TableLayoutPanelGrowStyle.AddRows; this.Controls.Add(Table); 之后,在运行时,我需要多少行,以及是否用面板或某些标签填充它们。 可能在同一行中的左侧是“面板”,右侧是“标签”等。 回答1 使用这样的东西: Table.Controls.Add(new Label { Text = "Type:", Anchor =
  • 如何在WinForms中禁用表格面板的水平滚动条(How to disable horizontal scrollbar for table panel in winforms)
    问题 嗨,我有一个tablelayoutpanel,我正在动态地将控件绑定到它。 当项目数超过面板高度时,显然会出现垂直滚动条,这没有问题。 但是同时,即使项目的宽度小于面板的宽度,也会出现水平滚动条。 我该如何预防呢? 回答1 int vertScrollWidth = SystemInformation.VerticalScrollBarWidth; tableLayoutPanel1.Padding = new Padding(0, 0, vertScrollWidth, 0); 回答2 停靠的TableLayoutPanel包含停靠的GroupBoxes和设置为100%宽度的单个Column时,我遇到了这个问题。 我不想为这些设置手动大小-我希望它们随表格一起调整大小。 奇怪的是,将TableLayoutPanel的Right Padding设置为1(而不是滚动条的宽度-如您所料,会留下滚动条大小的间隙)完全解决了该问题。 这是在C#2010 Express,.NET 4,Windows 8中。不知道此合并是否可以在其他变体上使用。 将padding设置为0似乎可以解决IDE中的问题,但是在实际运行时问题仍然存在。 给我闻起来像是TableLayoutPanel中的某种错误……或者也许这只是我拥有的控件和属性的特定组合(这是一个非常复杂的布局)。 回答3
  • TableLayoutPanel responds very slowly to events
    In my application I really needed to place a lot of controls (label, textbox, domainupdown) in a nice order. So I went ahead and used some nested TableLayoutPanel. The problem now is, this form responds very slow to most of events (resize, maximize, minimize and ...) it takes really up to 5 seconds for controls within the tables to get resized, redrawed to the new size of form. I am putting a finger in my eye now! If this form is that slow on my home PC (i7@4GHz and a good graphic card) what it will do tommorow on the old P4 computer at work? I even tried to use the code below but it does
  • Adding controls to TableLayoutPanel dynamically during runtime
    I have a TableLayoutPanel starting with two columns and 0 rows. What I need to do is, dynamically adding a row and filling both of the columns with different controls (it will be panels). In Form1 I am creating the TableLayout this way: TableLayoutPanel Table = new TableLayoutPanel(); Table.Location = new Point(10, 40); Table.Size = new Size(620,100); Table.AutoSize = true; Table.Name = "Desk"; Table.ColumnCount = 2; Table.RowCount = 0; Table.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink; Table.GrowStyle = System.Windows.Forms.TableLayoutPanelGrowStyle.AddRows; this.Controls
  • 如何为控件及其子控件暂停绘画?(How do I suspend painting for a control and its children?)
    问题 我有一个控件,我必须对其进行较大的修改。 我想完全避免重绘,而SuspendLayout和ResumeLayout不够。 如何为控件及其子控件暂停绘画? 回答1 在我之前的工作中,我们一直在努力获取丰富的UI应用程序以立即流畅地进行绘制。 我们正在使用标准的.Net控件,自定义控件和devexpress控件。 经过大量的谷歌搜索和反射器使用后,我遇到了WM_SETREDRAW win32消息。 当您更新控件时,这实际上会停止控件图形,并且可以将IIRC应用于父/包含面板。 这是一个非常简单的类,演示如何使用此消息: class DrawingControl { [DllImport("user32.dll")] public static extern int SendMessage(IntPtr hWnd, Int32 wMsg, bool wParam, Int32 lParam); private const int WM_SETREDRAW = 11; public static void SuspendDrawing( Control parent ) { SendMessage(parent.Handle, WM_SETREDRAW, false, 0); } public static void ResumeDrawing( Control parent ) {