Label without Padding and Margin
Asked Answered
K

2

2

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

Instead, I get this:

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

Ketonuria answered 7/2, 2014 at 16:15 Comment(11)
May be this answer could provide some helpAeon
Check out FlowLayoutPanel here msdn.microsoft.com/en-us/library/…, seems like it has what you're looking forFixing
@Aeon Didn't like my answer there, so I deleted it. OP: try changing your myControl.FlatStyle = FlatStyle.Standard; No need for the Padding or Margins, that's for relationships with other controls.Vries
@Aeon Unfortunately that trims the last character on the text :(Ketonuria
I think you meant @VriesAeon
@Vries With myControl.FlatStyle = FlatStyle.Standard I get the TEXT centered in the label, still with space between the TEXT and the border :( @Aeon the solution you linked trims the last character (i.e.: I get TEX)Ketonuria
If you need this to be this tight, I would consider not using a label and just drawing the border and text in a paint event of the container.Vries
Have you tried to design the label in the editor and copy the generated code?Wombat
Or you could use Graphics.DrawString(), unless you want to have all the events from a label.Wombat
@Vries I tried using a different OS and it worked. I also used your solution (the one you deleted) so I could find the text and delete the extra space. Thank you! If you could, please post your solution as an answer so I can accept it.Ketonuria
@Ketonuria I'll pass. Feel free to post your own answer based on the solution you came up with.Vries
K
5

This is what solved it for me (using @LarsTech's solution):

I added

    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);
    }

to my label definition.

I also added

textLabel.FlatStyle = FlatStyle.System;

Thank you very much for the help!

Ketonuria answered 6/3, 2014 at 16:30 Comment(0)
X
2

I don't know what's going on with the FlatStyle property, except to say that FlatStyle.System has a similar effect on my system. The other FlatStyle values indicate clearly what the effect will be on the control, but FlatStyle.System is pretty nebulous.

The appearance of the control is determined by the user's operating system.

I'm not sure what in the OS plays a role in the layout of he control. LarsTech's comment about changing it to FlatStyle.Standard (or any other value for that matter) fixes the issue for me (and doesn't trim off any text, as your comment indicates is happening to you).

You can override the alignment behavior by explicitly setting it to the center:

myControl.TextAlign = ContentAlignment.MiddleCenter;

I'm not sure exactly what you're trying to achieve (since it seems you could just enter all of your text in a single Label, not multiple next to each other), but you may also want to remove the border style:

myControl.BorderStyle = BorderStyle.None;

And, similar to what Blablablaster said, consider using a FlowLayoutPanel and adding your Label controls to that. You can place the above code in a loop, adding each one to the panel, and it'll take care of laying them out next to each other for you.

for (var i = 0; i < 10; i++)
{
    Label myControl = new Label();
    myControl.Text = "TEXT";
    ...
    ...
    flowLayoutPanel1.Controls.Add(myControl);
}
Xyloid answered 7/2, 2014 at 17:17 Comment(3)
I don't get it... Using the code I posted (removing Padding and Margin definitions) and switching FlatStyle from System to Standard gives me the same result.. Can you post the code you are using to see if it works on my end, please?Ketonuria
I'm trying to get the second window in this answer (but with the border): https://mcmap.net/q/358326/-c-label-autosize-adds-padding but I seem to only be able to get the first window.. It might be OS interference..Ketonuria
It was OS interference.. I tried this in Win7 (was using XP before) and I get the behaviour that I want, using FlatStyle.Standard. Thank you!Ketonuria

© 2022 - 2024 — McMap. All rights reserved.