Click here to Skip to main content
15,897,334 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I want to rotate a label by button click
I found this code
here is a newLabel class
C#
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);
        }
    }

and that's use of label
C#
newlbl.AutoSize = false;
newlbl.NewText = "Heba<b";
newlbl.ForeColor = Color.Green;

my problem is
C#
newlbl.AutoSize = false;

I want To make autoSize = true but when make it the label disappear from a Form
and I can't resize label Font because it doesn't appear completely because it has a fixed height and width
any help ?

What I have tried:

I tried the code I submitted above
Posted
Updated 31-May-16 4:19am
Comments
Philippe Mori 1-Jun-16 7:19am    
Would embedding a WPF control help? At least, it is easier to do rotation in WPF...

When you use AutoSize, it assumes a horizontal orientation - and autosizes on that basis. It then Paints the control text using the size and horizontal text. But since you have applied a rotate / transform to the graphics object it will draw with, it erases your text, and draws it's own - at an angle which means it is no longer visible within the bounds of the actual control.

If you want an autosize, rotatable control, you are going to have to dump Label completely and create your own alternative based on a UserControl, and handle it all yourself, via MeasureString. Do note that the size of the control will have to change as the rotation does, as the control is rectangular:
 ____
|1234|
 ----
is around 25 pixel high, by 75 wide:
 _
|4|
|3|
|2|
|1|
 - 
Is probably about 75 pixels high, by 30 or so wide (especially is the characters are rotated though 90 degrees as well)
 
Share this answer
 
Create a class
newLable
which can rotate it's Text on any angle specified by you.

You can use it by code or simply dragging from ToolBox


C#
using System.Drawing;

class newLabel:System.Windows.Forms.Label
{
    public int RotateAngle { get; set; }  // to rotate your text
    public string NewText { get; set; }   // to draw text
    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 this custom control is used into your form.

You have to set below properties

C#
1. nwlbl.Text = "";             //which can be changed by NewText property
2. nwlbl.AutoSize = false;      // adjust according to your text
3. nwlbl.NewText = "Hello";     // whatever you want to display
4. nwlbl.ForeColor = Color.Red;  // color to display
5. nwlbl.RotateAngle = -90;     //angle to rotate</pre>
 
Share this answer
 
v5
Comments
Philippe Mori 31-May-16 22:20pm    
It does not seems much different than the question itself...
Santosh Kokatnur 1-Jun-16 1:52am    
I have changed my answer according to the above question. only need to set the properties correctly.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900