-->

编程分配保证金和/或填充到一个标签(Programmatically assigning Margi

2019-09-16 21:53发布

In trying to get some labels in a TableLayoutPanel to move from the top left of their cells to the center of the cells, I'm trying to experiment with adding padding and/or margins.

However, nothing I've tried works. Here's the code I've tried and the results:

// 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

Answer 1:

尝试:

lbl.Margin = new Padding(1);

您可能还想做的事:

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


Answer 2:

labelName.Style.Add("Margin", "10px");


文章来源: Programmatically assigning Margin and/or Padding to a Label