XMLpitstop.com   |  VBnetexpert.com   |  Community Credit  
 
 
Pitstop Search:  
in
 
Sign in | Join | Help
 
 
  Blog
    Home  
 
  Entries By Date
 
<November 2008>
SunMonTueWedThuFriSat
2627282930311
2345678
9101112131415
16171819202122
23242526272829
30123456
 
 
  Blog Categories
   
 
  Archives
    December 2009 (2)  
    October 2009 (1)  
    September 2009 (1)  
    August 2009 (2)  
    July 2009 (2)  
    June 2009 (1)  
    May 2009 (1)  
    February 2009 (1)  
    December 2008 (1)  
    November 2008 (3)  
    September 2008 (3)  
    August 2008 (3)  
    June 2008 (4)  
    May 2008 (2)  
    April 2008 (3)  
    March 2008 (3)  
    February 2008 (5)  
    December 2007 (4)  
    November 2007 (1)  
    October 2007 (3)  
 
  Syndication
    RSS  
    Atom  
    Comments RSS  
  .NET Flea Market  
 

Autosize. No, not the control, the text.

It's nice that .NET controls have an auto-size property so you don't have to worry about overflow and all.  But what about cases where you have a fixed layout?  Well, that's simple, you turn autosize off and fix the control to the size you need.

That's half the story.  What about the text that's inside it?  Now you know I'm talking to marketing people when I say that there are times you want the text to be as big as possible within that control.  But you can't just set the font to a huge size, because sometimes you'll have more text to display and the font size must sadly be reduced.

To accommodate this, I made a quick method that brute-forces the correct font size in the control.  basically, stepping down the size of the font until it fits.  I know loops like this are cheap, poor programming, and I did give consideration to doing some hard math to calculate the proper font size based on the initial size, but sometimes not getting hung up on performance can be liberating.

    Private Sub ResizeText(ByVal c As Control)
        Dim currentSize As Size
        Dim currentFont As Font

        currentFont = c.Font

        Do
            currentSize = TextRenderer.MeasureText(c.Text, currentFont, _
                c.Size, TextFormatFlags.WordBreak)

            If currentSize.Width > (c.Width - c.Margin.Horizontal) _
                OrElse currentSize.Height > (c.Height - c.Margin.Vertical) Then

                currentFont = New Font(currentFont.FontFamily, _
                    CSng(currentFont.Size - 0.5), currentFont.Style, currentFont.Unit)
            Else
                Exit Do

            End If

        Loop While currentFont.Size >= 1

        c.Font = currentFont

    End Sub
Published Nov 16 2008, 02:37 PM by anachostic
Filed under: ,
Comments

 

lvyani995 said:

shoes <a href="http://www.myshoesreview.com"> shoes </a>

shoes <a href="http://www.myshoesreview.com"> shoes </a>

Cheongsam <a href="www.myshoesreview.com/.../48"> Cheongsam </a>

Minimalist Wedding Dress <a href="www.myshoesreview.com/.../64"> Minimalist Wedding Dress </a>

fashion <a href="http://www.fashionreviewtoday.com"> fashion </a>

fashion <a href="http://www.fashionreviewtoday.com"> fashion </a>

Wedding Dress<a href="www.fashionreviewtoday.com/.../77"> Wedding Dress </a>

Short Hairstyles<a href="www.fashionreviewtoday.com/.../79"> Short Hairstyles </a>

June 6, 2010 10:38 PM
 

ebook68 said:

You can find and download some useful ebooks Microsoft ASP.NET programming from http://ebook68.com

August 24, 2010 11:01 AM

About anachostic

That's me. Seek and ye shall find.
 
 
Copyright © . All Rights Reserved.
Powered by Community Server (Commercial Edition), by Telligent Systems