views:

27

answers:

2

Is there a peramerter or setting that I can turn on or use to rotate a label 90 degrees? I want to use it through the design panel.

I would like to avoid having to do it through code if possible.

Im currently using c# as my base

+3  A: 

There is no property to rotate your text 90 degrees. You need to write your own control.

CyberD
+2  A: 

Add a new class to your project and paste the code shown below. Compile. Drop the new control from the top of the toolbox onto your form. Beware of the less than stellar rendering quality and the normal hassle of measuring the length of a string.

using System;
using System.ComponentModel;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

class VerticalLabel : Label {
    private SizeF mSize;
    public VerticalLabel() {
        base.AutoSize = false;
    }
    [Browsable(false)]
    public override bool AutoSize {
        get { return false; }
        set { base.AutoSize = false; }
    }
    public override string Text {
        get { return base.Text; }
        set { base.Text = value; calculateSize(); }
    }
    public override Font Font {
        get { return base.Font; }
        set { base.Font = value; calculateSize(); }
    }
    protected override void OnPaint(PaintEventArgs e) {
        using (var br = new SolidBrush(this.ForeColor)) {
            e.Graphics.RotateTransform(-90);
            e.Graphics.DrawString(Text, Font, br, -mSize.Width, 0);
        }
    }
    private void calculateSize() {
        using (var gr = this.CreateGraphics()) {
            mSize = gr.MeasureString(this.Text, this.Font);
            this.Size = new Size((int)mSize.Height, (int)mSize.Width);
        }
    }
}
Hans Passant