Answer the question
In order to leave comments, you need to log in
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
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.
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;
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);
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.
label1.ClientSize
try this for
more details read msdn.microsoft.com/en-us/library/system.windows.forms.label.aspx
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question