How do I keep a label centered in WinForms?

后端 未结 7 1037
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-07 11:21

In WinForms I am using a Label to display different messages like success, failure, etc.

I\'d like to center that label in the center form.

7条回答
  •  爱一瞬间的悲伤
    2020-12-07 12:08

    The accepted answer didn't work for me for two reasons:

    1. I had BackColor set so setting AutoSize = false and Dock = Fill causes the background color to fill the whole form
    2. I couldn't have AutoSize set to false anyway because my label text was dynamic

    Instead, I simply used the form's width and the width of the label to calculate the left offset:

    MyLabel.Left = (this.Width - MyLabel.Width) / 2;
    

提交回复
热议问题