How to display RabLabel in the center (in a WinForm environment)

1 Answer 11 Views
Label
yw
Top achievements
Rank 2
Iron
Iron
yw asked on 16 Sep 2025, 03:21 PM

I want to use RadLabel _lblPreview as an information display control. When the text display width is less than the width of the Panel, I hope the content is displayed in the middle of the Panel.
_lblPreview.Height = 100;
_lblPreview.Font = new Font(fontName, size, FontStyle.Regular);
lblPreview.TextAlignment = ContentAlignment.MiddleCenter;
_lblPreview.Text = "样本文本 abc123.,";
_lblPreview.Dock = DockStyle.Top;
_lblPreview.AutoSize = true,
When gradually increasing the font size, the display is correct. However, when AutoSize is set to false, after the font size reaches 46, the displayed content gets truncated, and when it reaches 52, there is no display at all. What is the reason?

 

1 Answer, 1 is accepted

Sort by
0
Accepted
Dinko | Tech Support Engineer
Telerik team
answered on 19 Sep 2025, 09:55 AM

Hi yw scr,

I appreciate your interest in our RadLabel control.

What I can suggest instead is to place the RadLabel in the Top RadPanel with Dock set to Fill (of that RadPanel). Then you can use a code to position the label in the center:

public Form1()
{
    InitializeComponent();
    this.SizeChanged += Form1_SizeChanged;
    CenterLabel();
}

private void Form1_SizeChanged(object sender, EventArgs e)
{
    CenterLabel();
}
void CenterLabel()
{
    radLabel1.Location = new Point((radPanel1.Width - radLabel1.Width) / 2, (radPanel1.Height - radLabel1.Height) / 2 );
}

Regards,
Dinko | Tech Support Engineer
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

yw
Top achievements
Rank 2
Iron
Iron
commented on 19 Sep 2025, 11:01 AM

Thank you for your reply.
    Based on your sample code, I built a test form, but it still cannot be displayed centered, and when it exceeds the parent container, the text cannot wrap.
My code is as follows:

public partial class FrmRadLabelMidderCenter : Form
{
    RadLabel _lblPreview = new RadLabel()
    {
        Text = "_lblPreview.Text = \"样本文本 abc123.,\";",
        //AutoSize = true,
        TextWrap = true,
        Font = new Font("微软雅黑", 16),
    };

    public FrmRadLabelMidderCenter()
    {
        InitializeComponent();
        radPanel1.Controls.Clear();
        radPanel1.Controls.Add(_lblPreview);
        _lblPreview.Dock = DockStyle.Fill;
    }

    private void FrmRadLabelMidderCenter_SizeChanged(object sender, EventArgs e)
    {
        CenterLabel();
    }

    void CenterLabel()
    {
        _lblPreview.Location = new Point((radPanel1.Width - _lblPreview.Width) / 2,
            (radPanel1.Height - _lblPreview.Height) / 2);
    }

    private void FrmRadLabelMidderCenter_Load(object sender, EventArgs e)
    {
        CenterLabel();
    }


    private void button1_Click(object sender, EventArgs e)
    {
        _lblPreview.Font = new Font(_lblPreview.Font.FontFamily, _lblPreview.Font.Size + 2);
        CenterLabel();
    }

    private void button2_Click(object sender, EventArgs e)
    {
        _lblPreview.Font = new Font(_lblPreview.Font.FontFamily, _lblPreview.Font.Size - 2);
        CenterLabel();
    }
}

 

Dinko | Tech Support Engineer
Telerik team
commented on 19 Sep 2025, 02:13 PM

I understand. The AutoSize is set to true, and the label will try to resize itself to one line. I have tried different approaches and what you can do is avoid using the above code and use this one instead:

radLabel1.AutoSize = false; // important, otherwise it won’t resize
radLabel1.Dock = DockStyle.Fill;
radLabel1.TextAlignment = ContentAlignment.MiddleCenter;
radLabel1.TextWrap = true;

Give the above properties a try and share the outcome.

 

yw
Top achievements
Rank 2
Iron
Iron
commented on 19 Sep 2025, 02:40 PM

thanks!
Tags
Label
Asked by
yw
Top achievements
Rank 2
Iron
Iron
Answers by
Dinko | Tech Support Engineer
Telerik team
Share this question
or