How to make group box text alignment center in win forms?

后端 未结 4 1339
清歌不尽
清歌不尽 2021-01-02 15:22

I am using a group box and there are several controls inside this.

My requirement is to set the group box title to the middle of the group box instead of Left.

4条回答
  •  迷失自我
    2021-01-02 15:49

    Unfortunately, you may set the title on the right by using the RightToLeft property, but there is no property to set it in the middle.

    What you can do is to set an empty Text in your GroupBox, create a Label with the title and put that label above the GroupBox (with the same parent).

    You may do it dynamically at form initialization by calling following procedure:

    private void CenterGroupBoxTitle(GroupBox groupbox)
    {
      Label label   = new Label() ;
      label.Text    = groupbox.Text ;
      groupbox.Text = "" ;
      label.Left    = groupbox.Left+(groupbox.Width-label.Width)/2 ;
      label.Top     = groupbox.Top + 2 ; // 2 is an example : adjust the constant
      label.Parent  = groupbox.Parent ;
      label.BringToFront() ;
    }
    

提交回复
热议问题