I got a few labels which show a entire week.
It's displaying the long date like this

LblCurrentDay.Text = Date.Now().ToString("D")
LblCurrentDay1.Text = Date.Now().AddDays(1).ToString("D")
LblCurrentDay2.Text = Date.Now().AddDays(2).ToString("D")
LblCurrentDay3.Text = Date.Now().AddDays(3).ToString("D")
LblCurrentDay4.Text = Date.Now().AddDays(4).ToString("D")
LblCurrentDay5.Text = Date.Now().AddDays(5).ToString("D")
LblCurrentDay6.Text = Date.Now().AddDays(6).ToString("D")

The labels are all inside each of their own small panel.
But since not all days day got the same length, sometimes it will be not centered.
Sometimes even partialy outside the panel and not visible.

So i would have to center them according to their size i guess.

Dani AI

Generated

Nice, the calculation approach from is the right idea and the confirmation from shows it works in practice. A few practical alternatives and gotchas follow so the long date strings never clip, disappear, or drift off-center as content or panel size changes.

When the label can occupy the whole panel, avoid manual positioning and let the control do the work: make the label non‑autosizing, dock it to the panel and center the text. This handles text changes and resizes automatically.

label.AutoSize = False
label.Dock = DockStyle.Fill
label.TextAlign = ContentAlignment.MiddleCenter

If the label must remain AutoSize (single‑line) but still be centered, measure the rendered text width and compute the left coordinate after setting Text. Run this on TextChanged and on the panel’s Resize so the position is always correct.

Dim w = TextRenderer.MeasureText(lbl.Text, lbl.Font).Width
lbl.Location = New Point((panel.ClientSize.Width - w) \ 2, lbl.Location.Y)

Additional practical tips:

  • Set AutoEllipsis = True so overly long text shows “…” instead of being partially invisible.
  • For wrapped dates, set lbl.MaximumSize = New Size(panel.ClientSize.Width, 0) and AutoSize = True so height grows instead of clipping.
  • Batch updates inside SuspendLayout/ResumeLayout to avoid flicker when updating many labels.
  • A TableLayoutPanel with percent columns gives predictable column widths if equal spacing is desired.
  • Standardizing the date format (shorter pattern) reduces width variance.
  • Remember font changes and DPI scaling affect measured widths; recenter after any font or DPI change.

Recommended Answers

All 2 Replies

To calculate the new position of the label, subtract half the width of the label from half the width of the panel as in

Label1.Location = New Point(Panel1.Width / 2 - Label1.Width / 2, Label1.Location.Y)

By default, labels are set to autosize so if you do this calculation when the text changes then the label should stay centered.

Works perfect, thanks a lot Rev Jim

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.