Note that there are some explanatory texts on larger screens.

plurals
  1. POOverride AutoSize for derived Label Control in C#
    primarykey
    data
    text
    <p>I am trying to extend the System.Windows.Forms.Label class to support vertically drawn text. I do this by creating a new property called MyLabelOrientation that the user can set to Horizontal or Vertical. When the user changes this setting, the values for width and height are swapped to resize the control to its new orientation. Finally, I override the OnPaint function to draw my Label.</p> <p>I would like to extend the AutoSize property for this control as well so that my Label will auto-size to the text it contains. For the horizontal orientation, the base functionality implements this for me. For the vertical orientation, I create a Graphics object and set the height of the control to the width of the SizeF object returned from Graphics.MeasureString(Text, Font). You can see an example of the code I'm using below.</p> <pre><code>using System; using System.ComponentModel; using System.Drawing; using System.Windows.Forms; using System.ComponentModel.Design; using System.Windows.Forms.Design; public class MyLabel : Label { public enum MyLabelOrientation {Horizontal, Vertical}; protected MyLabelOrientation m_orientation = MyLabelOrientation.Horizontal; [Category("Appearance")] public virtual MyLabelOrientation Orientation { get { return m_orientation; } set { m_orientation = value; int temp = Height; Width = Height; Height = temp; Refresh(); } } private Size ResizeLabel() { Graphics g = Graphics.FromHwnd(this.Handle); SizeF newSize = g.MeasureString(Text, Font); if (m_orientation == MyLabelOrientation.Horizontal) Width = (int)newSize.Width; else Height = (int)newSize.Width; } protected override void OnPaint(PaintEventArgs e) { Brush textBrush = new SolidBrush(this.ForeColor); if (m_orientation == LabelOrientation.Vertical) { e.Graphics.TranslateTransform(Width, 0); e.Graphics.RotateTransform(90); e.Graphics.DrawString(Text, Font, textBrush, Padding.Left, Padding.Top); } else { base.OnPaint(e); } } } </code></pre> <p>However, setting AutoSize to true seems to prevent and/or override any changes to the size of the control. This means that I can't change the width or height when I want to change the Label's orientation. I'm wondering if this behavior can be overridden, so that I can test whether AutoSize is set, and then adjust the size of the control according to it's orientation.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload