Domanda

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.

È stato utile?

Soluzione

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!

Altri suggerimenti

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);
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top