How can I flip/rotate the label in C#/Windows Forms?

rangasathish picture rangasathish · Sep 26, 2012 · Viewed 15.7k times · Source

How can I flip/rotate the label in C# Windows Forms?

I set the background image to my label.

At every time interval it moves three pixels to the right side. When it reaches the form end position I need the label to be flipped and turned back.

I have tried the following way, but I didn't get the solution.

private void timer1_Tick(object sender, EventArgs e){

    if (label2.Location.X < this.Width)
        label2.Location = new Point(label2.Location.X + incr, label2.Location.Y);
    else
    {
        incr = -2;
        label2.Location = new Point(label2.Location.X - 50, label2.Location.Y);
        label1.Image.RotateFlip();
    }
    this.Refresh();
}

Answer

Ravindra Bagale picture Ravindra Bagale · Sep 26, 2012

Create a class, newlabel, which can rotate its Text on any angle specified by the user.

extend label class& override paint method

You can use it by code or simply dragging from the ToolBox.

using System.Drawing;

class newLabel : System.Windows.Forms.Label
{
    public int RotateAngle { get; set; }  
    public string NewText { get; set; }   
    protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
    {
        Brush b =new SolidBrush(this.ForeColor);           
        e.Graphics.TranslateTransform(this.Width / 2, this.Height / 2);
        e.Graphics.RotateTransform(this.RotateAngle);
        e.Graphics.DrawString(this.NewText, this.Font,b , 0f, 0f);
        base.OnPaint(e);
    }
}

Now drag this custom control to be used into your form.

You have to set the below properties.

newlbl.Text = "";           
newlbl.AutoSize = false;      
newlbl.NewText = "ravindra";     
newlbl.ForeColor = Color.Green;  
newlbl.RotateAngle = -90; 

Change angle as required by simply changing the RotateAngle property.