How to size a label taking an text and its font

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

In Win32, there is AutoSize property for a label.
How can I do the same AutoSize Task in CompactFramework ?
I don’t understand why TextBox does not have this ability either in both
WinCe and Win32, since when using a TextBox with a fixed text size, it would
be useful. Ex: a zip code field, where font can be configured and the
application have to resize the textbox to fie the zip code content.
 
Something like that (untested):


private void textBox1_TextChanged(object sender, System.EventArgs e)
{
SizeF size = this.CreateGraphics().MeasureString(textBox1.Text,
txtSell.Font);
textBox1.Width = (int)size.Width;
}
 
Windows as a platform does not use autosized entry fields. You can build
one, but the edit control is a fundamental primitive and its behaviour is
pretty much a standard. As for the label, you can create a graphics object
on the form, to which the label belongs and then use Graphics.MeasureString
to calculate the extents and resize the label
 
Yes, to call Dispose on Graphics object is a good idea. Otherwise, you'll a
memory leak.

Thank you,
Sergiy.

This posting is provided "AS IS" with no warranties, and confers no rights.
--------------------
| From: "Alex Feinman [MVP]" <[email protected]>
| References: <[email protected]>
<[email protected]>
| Subject: Re: How to size a label taking an text and its font
| Date: Tue, 28 Sep 2004 15:02:39 -0700
| Lines: 34
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.2900.2180
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2180
| X-RFC2646: Format=Flowed; Original
| Message-ID: <[email protected]>
| Newsgroups: microsoft.public.dotnet.framework.compactframework
| NNTP-Posting-Host: 204.249.181.133
| Path:
cpmsftngxa06.phx.gbl!TK2MSFTNGXA01.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP12
.phx.gbl
| Xref: cpmsftngxa06.phx.gbl
microsoft.public.dotnet.framework.compactframework:62206
| X-Tomcat-NG: microsoft.public.dotnet.framework.compactframework
|
| Don't you want to call Dispose on Graphics object at some point? ;-)
|
| --
| Alex Feinman
| ---
| Visit http://www.opennetcf.org
| | > Something like that (untested):
| >
| >
| > private void textBox1_TextChanged(object sender, System.EventArgs e)
| > {
| > SizeF size = this.CreateGraphics().MeasureString(textBox1.Text,
| > txtSell.Font);
| > textBox1.Width = (int)size.Width;
| > }
| >
| > --
| > Alex Yakhnin, .NET CF MVP
| > www.intelliprog.com | www.opennetcf.org
| >
| > "Batagin" wrote:
| >
| >> In Win32, there is AutoSize property for a label.
| >> How can I do the same AutoSize Task in CompactFramework ?
| >> I don't understand why TextBox does not have this ability either in
both
| >> WinCe and Win32, since when using a TextBox with a fixed text size, it
| >> would
| >> be useful. Ex: a zip code field, where font can be configured and the
| >> application have to resize the textbox to fie the zip code content.
| >>
|
|
|
 
Back
Top