Click here to Skip to main content
15,663,225 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Here is my scenario...

The characters that are entered in the text box will be displayed in the label at the button click event. Label is having fixed width for ex,200px but the height is not set for the label.

By changing the characters in the text box,and firing the button click event the label will get populated with the text from the text box. At that time label width will be constant but height will change dynamically according to the no of characters entered in the textbox.....

Here I need to get the label height in pixels on the button click event???????

Thanks in Advance ?Can Anyone help me????
koool.kabeer 5-Aug-10 4:47am    
which label? Windows or Web?
Svetlin Panayotov 5-Aug-10 5:02am    
Tags say ASP.NET so I assume that it's Web. If that's really the case it probably can be done with Graphics.MeasureString, but it won't be accurate.

Use Javascript. On click event, use Javascript or jQuery whichever is easy for you to get the label object in DOM and then the specific property of the control. Show, modify the properties as per your need.
Share this answer
Thankz for the guidance....

Myself found the solution.Here it is,

Label1.Text = TextBox1.Text;
        string txt = Label1.Text;
        Font f = new Font("Times New Roman",14,GraphicsUnit.Pixel);
        Bitmap bp = new Bitmap(1, 1);
        Graphics gp = Graphics.FromImage(bp);
        int height = (int)gp.MeasureString(txt,f,260).Height;
        Response.Write("<script>alert('Height is :-'+"+Convert.ToString(height)+");</script>");

As per the svetlin comment it won't provide more accurate height.
But it mey be helpful....
Share this answer
Christian Graus 6-Aug-10 5:31am    
Yeah, this is close to useless. It may work, if the user is using standard size fonts, but if they set their web page to be bigger or smaller, it will be totally wrong.

You should also clean your memory leaks in this code, and try to use meaningful variable names.

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