How do I keep a label centered in WinForms?

后端 未结 7 1013
爱一瞬间的悲伤
爱一瞬间的悲伤 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:01

    If you don't want to dock label in whole available area, just set SizeChanged event instead of TextChanged. Changing each letter will change the width property of label as well as its text when autosize property set to True. So, by the way you can use any formula to keep label centered in form.

    private void lblReport_SizeChanged(object sender, EventArgs e)
    {
        lblReport.Left = (this.ClientSize.Width - lblReport.Size.Width) / 2;
    }
    

提交回复
热议问题