tags:

views:

84

answers:

2
+2  Q: 

Measure label size

I'm implementing a custom control with some labels on it and I need to measure the size of those labels to have an optimal layout. In this way I can properly show the control for each font and font size.
Could you tell me how can I do that, please?
Thank you.

A: 

you might want to look up ev.Graphics.MeasureString(str,font)

dar7yl
+1  A: 

The correct way to size and arrange your custom control according to the size of its sub-elements is to override MeasureOverride and ArrangeOverride.

See the links for details, but in a nutshell, your control is supposed to (in MeasureOverride):

  1. call UIElement.Measure on all children (which will include your labels), which will return the size that each of your children would like to have,
  2. calculate your own desired size and
  3. return this size to the framework (by using it as the return value of MeasureOverride).

Afterwards, in ArrangeOverride, you get the size allocated to your control by the framework as a parameter and you

  1. determine how much of the space you want to allocate to each of your child elements and
  2. call UIElement.Arrange on each child element.
Heinzi