C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Communities   Interview   Jobs   Projects   Offshore Development    
Silverlight Tutorials | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Revenue Sharing |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...

New Feature: Community Sites: Create your own .NET community website and start earning from Google AdSense ! It's Free !






Auto resizing the text to a fixed cell width in Asp.net


Posted Date: 07 May 2008    Resource Type: Articles    Category: Web Applications

Posted By: vijay       Member Level: Gold
Rating:     Points: 15



Hi,
This is my first article in the forum. If there is no clarity in the article, let me know. Recently i have a requirement for resizing the text to a fixed cell width in a web application. Clearly, When a user enters his Name in a text box, we need to display that name in header as large as possible depend on the length of his name.

I achieved this by making use of Graphics class in C#.net. For this i used a single function getFontSize.

Name spaces required
System.Drawing,System.Drawing.Imaging,System.Drawing.Drawing2D

public string getfontsize(string userName)
{
string fntval = "";
for (int i = 48; i >= 10; i--)
{
Bitmap b = new Bitmap(1, 1);
Font fnt = new Font("Arial", i);
Graphics graphics = Graphics.FromImage(b);

int width = (int)graphics.MeasureString(userName, fnt).Width;

//Checking the width of the string with td width
if (width < int.Parse(tdrun.Width.ToString()))
{
fntval = i.ToString();
break;
}
}
return fntval;
}

MeasureString() takes two arguments string and font size. This method belongs to graphics class. By making use of width property we will get the width of the string. After this we compare the calculated width with td width. If the Calculated width for the given string is less than the td width we will return that font value.Finally, For loop is used to limit the fontsize of the string between 48 and 10.
This getfontsize() function will return larger fontsize value of the given username string that will fit into table cell.




Responses

Author: Sebastian    13 Jun 2008Member Level: Gold   Points : 1
This is very informative. Thanks for sharing the details.


Feedbacks      
Popular Tags   What are tags ?   Search Tags  
(No tags found.)

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Donwload mail in asp.net from pop3 client
Previous Resource: Exploring XHTML, the dotnet way!
Return to Discussion Resource Index
Post New Resource
Category: Web Applications


Post resources and earn money!
 
Related Resources



dotNet Slackers   BizTalk Adaptors    Web Design

internet fax

Contact Us    Privacy Policy    Terms Of Use