5

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();
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
rangasathish
  • 595
  • 6
  • 13
  • 25
  • Possible duplicate of *[C# vertical label in a Windows Forms](http://stackoverflow.com/questions/1371943/c-sharp-vertical-label-in-a-windows-forms)*. – Peter Mortensen Jan 16 '14 at 09:24

2 Answers2

9

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Ravindra Bagale
  • 17,226
  • 9
  • 43
  • 70
  • if the label size gets too small, say 28x82, the text is generated in the top right hand side of the text area. Is there a way to make sure the text is generated in the bottom left. This with a -90 angle. – Lee Jul 03 '15 at 05:38
0

So...You can do this way:

1.Download this dll file : http://www.mediafire.com/download/hc16qezty0k6qnv/RotateLabel.dll

2.Go on your Visual Studio and open your solution

3.Now you need to go on Projects tab -> Add references... -> Then browse the file you downloaded and simply add that file

4.Next step is to right click on ToolBox

5.After you done that you need to click on Choose Items

6.Again browse your downloaded file and add VerticalLabel

7.Then you can drag VerticalLabel from the Toolbox to your form.

That is it, its simple.

Hope that helped you i just translated this answer and made it simpler :)

Good luck, Stralz

Community
  • 1
  • 1
Stralz
  • 19
  • 8