如何在win forms中使GroupBox文本居中对齐?

7

我正在使用一个分组框,里面有几个控件。

我的要求是将分组框标题设置到分组框的中间而不是左边。

如何实现?


1
@Harry,我认为他指的是groupbox的标题?是吗? - Joel Legaspi Enriquez
是的,我的错误。我以为它涵盖了一些将控件居中对齐的材料。 - Harry
4个回答

6

不幸的是,您可以使用RightToLeft属性将标题设置在右侧,但没有属性可以将其设置在中间。

您可以在您的GroupBox中设置一个空的文本,创建一个标签来作为标题,并将该标签放置在GroupBox之上(具有相同的父级)。

您可以通过调用以下过程在窗体初始化时动态执行此操作:

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() ;
}

6
您可以像这样扩展GroupBox类。
 public class CustomGrpBox : GroupBox
    {
        private string _Text = "";
        public CustomGrpBox()
        {
            //set the base text to empty 
            //base class will draw empty string
            //in such way we see only text what we draw
            base.Text = "";
        }
        //create a new property a
        [Browsable(true)]
        [Category("Appearance")]
        [DefaultValue("GroupBoxText")]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public new string Text
        {
            get
            {

                return _Text;
            }
            set
            {

                _Text = value;
                this.Invalidate();
            }
        }
        protected override void OnPaint(PaintEventArgs e)
        {

              //first let the base class to draw the control 
              base.OnPaint(e);
              //create a brush with fore color
              SolidBrush colorBrush = new SolidBrush(this.ForeColor);
              //create a brush with back color
              var backColor = new SolidBrush(this.BackColor);
              //measure the text size
              var size = TextRenderer.MeasureText(this.Text, this.Font);
              // evaluate the postiong of text from left;
              int left = (this.Width - size.Width) / 2;
              //draw a fill rectangle in order to remove the border
              e.Graphics.FillRectangle(backColor, new Rectangle(left, 0, size.Width, size.Height));
              //draw the text Now
              e.Graphics.DrawString(this.Text, this.Font, colorBrush, new PointF(left, 0));

        }
    }

把上述类添加到你的项目中,然后在工具箱中使用“CustomGrpBox”而不是创建后会出现的“GroupBox”。你可以随时像这样设置文本。
private void Form2_Load(object sender, EventArgs e)
    {
        customGrpBox1.Text = "Hello World";
    }

在设计时的Visual Studio中会看起来像这样

在设计时的Visual Studio中会看起来像这样。


2
尝试使用Panel作为容器创建自定义控件,并在其周围绘制边框,这样您就可以完全控制标题的对齐方式。
如果您想要一个简单的方法,可以将groupbox的标题留空,然后在groupbox的中心位置放置一个label。您还可以将其定义为用户控件,这样就不需要反复操作。

将GroupBox扩展并重新绘制标签到所需位置是最简单的方法,正如其他回答者所发表的那样。 - Joel Legaspi Enriquez

0

这不是一个优雅的解决方案,但如果你有一个简单的GroupBox,它保持相同的大小,你可以在开头和结尾填充空格。

例如:GroupBox.Text = " 这是GroupBox文本 ";

填充空格的数量取决于框的长度。当然,你会失去一些重要的GroupBox顶部的开始和结束线,如果这很重要,那么答案3似乎是一个好的解决方案。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接