B
B
belk2011-03-10 18:32:29
.NET
belk, 2011-03-10 18:32:29

Find out estimated Label size (C# + WPF)?

We need to find out what the size of the Label in pixels will be if its content is a certain string, while not displaying the Label itself.
The code

Label label = new Label();
label.Content = root.value; //тип string
return label.ActualHeight;
returns null.

Answer the question

In order to leave comments, you need to log in

6 answer(s)
T
Thecentury, 2011-03-10
@belk

Make your label Measure(size). C size not sure, try either new Size() or new Size(Double.PositiveInfinity, Double.PositiveInfinity). Then read the desired size from the DesiredSize property.

L
Lazer1999, 2011-03-11
@Lazer1999

In such cases, I dance on the size of the text.
That is something like this: Resp. when autosizing, label size tolerances are counted as Padding. Total:
SizeF textSize = TextRenderer.MeasureText(text, label.Font);

int labelWidth = label.Padding.Horizontal + textSize.Width;
int labelHeight = label.Padding.Vertical + textSize.Height;

V
Vladimir Polishchuk, 2011-03-10
@NorthDakota

Maybe it's easier to manually set it?
// Создаем объект
Label label1 = new Label();
// Устанавливаем видимые границы
label1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
// Выравниваем нашу надпись
label1.ImageAlign = ContentAlignment.TopLeft;
// Устанавливаем значение
label1.Text = "MyFirstLabel";
// Устанавливаем размеры
label1.Size = new Size (label1.PreferredWidth, label1.PreferredHeight);

V
Vladimir Polishchuk, 2011-03-10
@NorthDakota

Try to use the name of the object not label, but for example label1, the class and the object may be confused
and in this way return the value of
return label1.Height;
P.S. There is no C# on the robot, I can’t specifically check it.

V
Vladimir Polishchuk, 2011-03-10
@NorthDakota

label1.ClientSize
try this for
more details read msdn.microsoft.com/en-us/library/system.windows.forms.label.aspx

D
Dmitry Sidorov, 2011-03-11
@Doomsday_nxt

Might be worth a try:

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question