Changing text in label make text align not working?

Here's the things i have done with my label in runtime:(C#)

label.Text = sometext;
label.TextAlign = ContentAlignment.MiddleCenter;

label.Invalidate();
label.Update();
label.Refresh();
label.PerformLayout();

And after the window is shown, before this code runs, the default text aligns perfectly in middle center. after this code runs, the text is aligned top left.

The autosize is false for the label.

Now, where did I do wrong?

Update:

I found a partial solution with no idea why to my own question: Turning "UseCompatibleTextRendering" true would make the text align correctly without any of the 5 lines of codes after text modifying. But this makes no sense. isn't the new text rendering supposed to be better, and old rendering expose me to visual artifacts etc?


I have tried to reproduce your problem, but cannot. I suggest you modify this simple code to try to reproduce your problem:

class FormD : Form {

    Label lb = new Label { Text = "ABCDEFG", Size = new Size(200, 200), TextAlign = ContentAlignment.MiddleCenter, BackColor = Color.RosyBrown, UseCompatibleTextRendering = true };

    public FormD() {
        Controls.Add(lb);
    }

    [STAThread]
    static void Main(String[] args) {
        Application.SetCompatibleTextRenderingDefault(false);

        Form f2 = new FormD();
        Application.Run(f2);
        return;
    }
}
链接地址: http://www.djcxy.com/p/63656.html

上一篇: C#中基于布尔模式的分支?

下一篇: 更改标签中的文本使文本对齐不起作用?