Hello,
how to set label location in top middle at run time according to the text length. For example i have one panel control of size 316,162 and one label at top it displays the security que from the database like what is your pet's name? Or who is your favourite childhood hero? So according to the text length it should come in the middle. For long text no probs but for small it display in left only.

Dani AI

Generated

This thread shows the two common patterns that solve the problem: have the label occupy the horizontal span and center the text (the route took, and the approach suggested by ), or compute and set the label’s position so the text is pixel‑centered (the programmatic option shown by ). Both work; choice depends on layout constraints, localization and whether the label must retain a tight bounding box.

When the label can safely span its parent, that approach is simplest and robust across different answer lengths or font sizes. When the label must stay tightly sized (for overlapping controls, animations, or complex layouts), measure the rendered text and place the control so its visual center aligns with the parent’s center. Whichever method is used, the placement needs recalculation when the parent resizes, the text changes, or the font changes.

A short WinForms pattern for the measurement approach (call from parent Resize and label Text/Font change handlers):

private void CenterLabelIn(Control parent, Label lbl)
{
    var size = TextRenderer.MeasureText(lbl.Text, lbl.Font);
    lbl.Left = (parent.ClientSize.Width - size.Width) / 2;
    // keep lbl.Top at the desired top offset
}

Additional practical notes: handle multi‑line results (wrap or limit lines), enable ellipsis or a tooltip for truncated text, watch parent padding/margins and RightToLeft settings for localization, and test under different DPI/fonts so centering remains correct. For web UIs the equivalent is simpler (text-align:center or a flexbox-centered container). These considerations provide a durable solution beyond the quick fixes already posted.

Recommended Answers

All 6 Replies

So what exactly are you trying to do? Move the label around so that the text always appears in the middle?

Could you not just make the label the maximum size it could be and set the text align to middle?

I don't want alignment of a text. I want that whole lable should move in a that way that whatevev the text small or long it should appear in tha top middle. For example if i will put label im the left side ane text is long than it look in the center bt if text is small than it display in left only.

Forgive me if im wrong for arguing though, if you made your label span from one side of the form to the other, and did text align center, the text (regardless of length) will always sit within the center or the form.

From what your saying thats the simplest way to gain your outcome. Unless your wanting something else and im misunderstanding in which case clarify further.

I got the solution. Thanks mikey

I feel like the solution was not posted in this thread and I think I shall add it for all of those who will future reference this post. The way you center any type of text on a forms application, or an XNA game screen is to get the screen size divided by two and subtract the text size divided by two from that. For example:

label1.Location = new Point((this.Width / 2) - (label1.Width / 2), yLocation);

As for containing this in a panel or group box or any other type of container control in visual studio, it is usually just making sure that the coordinates are inside the container.

The solution was i have set labels auto size property to false. Than i have stretched the label till both the ends and than i have set text align property to center. Khatam. Now whatever be the text it will display in center only.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.