标签无填充和边距 [英] Label without Padding and Margin
本文介绍了标签无填充和边距的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在C#(.NET Framework 3.5)中具有以下代码
I have the following code in C# (.NET Framework 3.5)
public partial class MainForm : Form
{
public MainForm()
{
//
// The InitializeComponent() call is required for Windows Forms designer support.
//
Label myControl = new Label();
myControl.Text = "TEXT";
myControl.FlatStyle = FlatStyle.System;
myControl.AutoSize = true;
myControl.BorderStyle = BorderStyle.FixedSingle;
myControl.Padding = new Padding(0);
myControl.Margin = new Padding(0);
this.Controls.Add(myControl);
InitializeComponent();
}
}
应显示带有边框的文本标签,如下所示:
Which should display a label with the text enclose by a border, like this:
------
|TEXT|
------
相反,我得到了:
--------
|TEXT |
--------
我不知道为什么...我的目标是能够有多个标签,而标签之间没有空格,就像这样:
And I don't know why... My objective is to be able to have multiple labels without space between them, like this:
-----------
|TEXT|TEXT|
-----------
我错过了什么吗?预先感谢!
Am I missing something? Thanks in advance!
为澄清起见,我需要在文本和边框之间没有空格.
For clarification, I need to have NO SPACE between the text and the border.
推荐答案
这是为我解决的问题(使用@LarsTech的解决方案):
This is what solved it for me (using @LarsTech's solution):
我添加了
protected override void OnHandleCreated(EventArgs e) {
base.OnHandleCreated(e);
this.AutoSize = false;
}
protected override void OnFontChanged(EventArgs e) {
base.OnFontChanged(e);
this.Size = GetTextSize();
}
protected override void OnResize(EventArgs e) {
base.OnResize(e);
this.Size = GetTextSize();
}
protected override void OnTextChanged(EventArgs e) {
base.OnTextChanged(e);
this.Size = GetTextSize();
}
private Size GetTextSize() {
Size padSize = TextRenderer.MeasureText(".", this.Font);
Size textSize = TextRenderer.MeasureText(this.Text + ".", this.Font);
return new Size(textSize.Width - padSize.Width, textSize.Height);
}
我的标签定义.
我还添加了
textLabel.FlatStyle = FlatStyle.System;
非常感谢您的帮助!
这篇关于标签无填充和边距的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文