Programmatically assigning Margin and/or Padding to a Label

◇◆丶佛笑我妖孽 提交于 2019-12-10 17:21:35

问题


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

回答1:


Try:

lbl.Margin = new Padding(1);

You might also want to do:

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



回答2:


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


来源:https://stackoverflow.com/questions/10390904/programmatically-assigning-margin-and-or-padding-to-a-label

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!