以编程方式将边距和/或填充分配给标签

Posted

技术标签:

【中文标题】以编程方式将边距和/或填充分配给标签【英文标题】:Programmatically assigning Margin and/or Padding to a Label 【发布时间】:2012-05-10 13:53:38 【问题描述】:

在尝试让 TableLayoutPanel 中的一些标签从其单元格的左上角移动到单元格的中心时,我正在尝试添加填充和/或边距。

但是,我没有尝试过任何工作。这是我尝试过的代码和结果:

// Setting the padding just cuts off the bottom part of the text
//lbl.Padding = new System.Windows.Forms.Padding(1);

// How to set Margin?
//lbl.Margin = new System.Windows.Forms.Margin(1); <- This mimics "Padding" but is not recognized
//lbl.Margin = new Thickness(6); <- This is the only example I could find, but it's for WPF

【问题讨论】:

【参考方案1】:

试试:

lbl.Margin = new Padding(1);

您可能还想这样做:

lbl.Dock = DockStyle.Fill;
lbl.TextAlign = ContentAlignment.MiddleCenter;
lbl.AutoSize = false;

【讨论】:

【参考方案2】:
labelName.Style.Add("Margin", "10px");

【讨论】:

问题是关于Winforms,这看起来像javascript

以上是关于以编程方式将边距和/或填充分配给标签的主要内容,如果未能解决你的问题,请参考以下文章