PC Review


Reply
Thread Tools Rate Thread

Determining max. length of text that can be entered for another de

 
 
=?Utf-8?B?Sm9obiBL?=
Guest
Posts: n/a
 
      15th Nov 2006
We have a Windows CE device that runs a .NET 1.1 application, which displays
text from a database field in a fixed (no autoresize) label. We use a
separate PC program to populate the database text field with the text to
display, which the CE device reads and displays. Thus, I need to limit what
the user can enter on the PC application so it will fit within the label that
is used on the CE device. The CE device label is using Microsoft Sans Serif
18 pt font (normal) and the label is 584 by 96 pixels. The PC application
will be using a different sized text box. How do I programmatically
determine in my PC application if the text the user entered in the text box
will display on the CE device without clipping? The CE code cannot change,
thus I need to determine as the user enters text on the PC app., if the text
will fit and if not, then prevent the user from entering any characters that
would cause the CE device to clip the text. I have tried using the
MeasureString method, but this does not take into account the height of the
text and also, the label/textbox dimensions determine if the text wraps to
another line. Any ideas?
--
Thank you.
 
Reply With Quote
 
 
 
 
=?Utf-8?B?Q2lhcmFuIE8nJ0Rvbm5lbGw=?=
Guest
Posts: n/a
 
      15th Nov 2006
You have to pass font to MeasureString so you should make a font with emSize
= 18. You can then pass the width of the textbox in pixels and a StringFormat
which tells it to wrap. Then you just need to check if the height returned
has exceeded the height of the textbox.

Ciaran O'Donnell

"John K" wrote:

> We have a Windows CE device that runs a .NET 1.1 application, which displays
> text from a database field in a fixed (no autoresize) label. We use a
> separate PC program to populate the database text field with the text to
> display, which the CE device reads and displays. Thus, I need to limit what
> the user can enter on the PC application so it will fit within the label that
> is used on the CE device. The CE device label is using Microsoft Sans Serif
> 18 pt font (normal) and the label is 584 by 96 pixels. The PC application
> will be using a different sized text box. How do I programmatically
> determine in my PC application if the text the user entered in the text box
> will display on the CE device without clipping? The CE code cannot change,
> thus I need to determine as the user enters text on the PC app., if the text
> will fit and if not, then prevent the user from entering any characters that
> would cause the CE device to clip the text. I have tried using the
> MeasureString method, but this does not take into account the height of the
> text and also, the label/textbox dimensions determine if the text wraps to
> another line. Any ideas?
> --
> Thank you.

 
Reply With Quote
 
Linda Liu [MSFT]
Guest
Posts: n/a
 
      16th Nov 2006
Hi John,

I agree what Ciaran has suggested.

The Graghics.MeasureString method has several overloads. To determine
whether a specified string when drawn with the specified Font within a
limited width is clipped, we could use the certain overload
Graphics.MeasureString(String, Font, Int32).

Use the above method in your PC application. In your scenario, pass the
string to be checked, the font("Microsoft Sans Serif", 18pt) and the width
value of the label in your device application to the method.

To obtain a Graphics, you could call the form's CreateGraphics method.

The following is a sample.

Function MeasureStringHeight(ByVal text As String, ByVal font As Font,
ByVal maxwidth As Int32) As Single
' Me refers to the form
Dim g As Graphics = Me.CreateGraphics
Dim textsize As SizeF
textsize = g.MeasureString(text, font, maxwidth)
Return textsize.Height
End Function

You could call this function before saving the data into DB. If the
returned height value is greater than the height value of the label in your
device application, set the focus back to the textbox and show an alert
text with an error provider besides the textbox.

You could also handle the Validating event of the textbox and call the
above MeasureStringHeight in the event handler. If the result is greater
than the height value of the label in your device application, set the
Cancel property of the System.ComponentModel.CancelEventArgs class to false.

Hope this helps.
If you have anything unclear, please feel free to let me know.


Sincerely,
Linda Liu
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscripti...t/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.

 
Reply With Quote
 
=?Utf-8?B?Sm9obiBL?=
Guest
Posts: n/a
 
      16th Nov 2006
My label is multiline capable on the CE device. Thus, wordwrapping and other
issues related to multi-line will be different on the CE device compared to a
different sized text box in my PC application. I need to pay attention to
both length and height. Thus, how do I determine that some text will fit,
without clipping in either the X or Y axis, in a label (i.e. before I place
it in the label per my application.)?
--
Thank you.


"Linda Liu [MSFT]" wrote:

> Hi John,
>
> I agree what Ciaran has suggested.
>
> The Graghics.MeasureString method has several overloads. To determine
> whether a specified string when drawn with the specified Font within a
> limited width is clipped, we could use the certain overload
> Graphics.MeasureString(String, Font, Int32).
>
> Use the above method in your PC application. In your scenario, pass the
> string to be checked, the font("Microsoft Sans Serif", 18pt) and the width
> value of the label in your device application to the method.
>
> To obtain a Graphics, you could call the form's CreateGraphics method.
>
> The following is a sample.
>
> Function MeasureStringHeight(ByVal text As String, ByVal font As Font,
> ByVal maxwidth As Int32) As Single
> ' Me refers to the form
> Dim g As Graphics = Me.CreateGraphics
> Dim textsize As SizeF
> textsize = g.MeasureString(text, font, maxwidth)
> Return textsize.Height
> End Function
>
> You could call this function before saving the data into DB. If the
> returned height value is greater than the height value of the label in your
> device application, set the focus back to the textbox and show an alert
> text with an error provider besides the textbox.
>
> You could also handle the Validating event of the textbox and call the
> above MeasureStringHeight in the event handler. If the result is greater
> than the height value of the label in your device application, set the
> Cancel property of the System.ComponentModel.CancelEventArgs class to false.
>
> Hope this helps.
> If you have anything unclear, please feel free to let me know.
>
>
> Sincerely,
> Linda Liu
> Microsoft Online Community Support
>
> ==================================================
> Get notification to my posts through email? Please refer to
> http://msdn.microsoft.com/subscripti...ult.aspx#notif
> ications.
>
> Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
> where an initial response from the community or a Microsoft Support
> Engineer within 1 business day is acceptable. Please note that each follow
> up response may take approximately 2 business days as the support
> professional working with you may need further investigation to reach the
> most efficient resolution. The offering is not appropriate for situations
> that require urgent, real-time or phone-based interactions or complex
> project analysis and dump analysis issues. Issues of this nature are best
> handled working with a dedicated Microsoft Support Engineer by contacting
> Microsoft Customer Support Services (CSS) at
> http://msdn.microsoft.com/subscripti...t/default.aspx.
> ==================================================
>
> This posting is provided "AS IS" with no warranties, and confers no rights.
>
>

 
Reply With Quote
 
=?Utf-8?B?Sm9obiBL?=
Guest
Posts: n/a
 
      16th Nov 2006
I am using the MeasureString with no success. For example, if I just hold
the "w" key down in my text box and then measure the size of the resulting
string; the size I get back implies that all of the text in the text box will
fit into the label box w/o clipping, but it does clip. Here is the code I am
using in the "Keypress" event for the text box:
' text box size is 876,90; label is 584 by 96
' this is the font used to display the text in the label
Dim stringFont As New Font("Microsoft Sans Serif", 18)
' this is the size of the label (actually it is 584 by 96)
Dim layoutSize As New SizeF(583.0F, 95.0F)
Dim stringSize As New SizeF
Dim g As Graphics = Me.lblTest.CreateGraphics()
' don't do any special processing for backspace-just do it
If e.KeyChar <> Microsoft.VisualBasic.ChrW(Keys.Back) Then
Dim newStringFormat As New StringFormat
newStringFormat.FormatFlags =
StringFormatFlags.MeasureTrailingSpaces And StringFormatFlags.NoClip

Dim cf, lf As Int32
' measure the string and tell MeasureString the size of the
layout (label)
stringSize = g.MeasureString(TextBox1.Text & e.KeyChar,
stringFont, layoutSize, newStringFormat, cf, lf)
' if the string height or width is too big; then cancel the
keypress
' via the handled property
If stringSize.Width >= 583 Or stringSize.Height >= 95 Then
e.Handled = True ' use to cancel
Beep()
Exit Sub
End If
End If

This problem seems so fundamental, so apparently I am missing something.
NOTE: the label will have no scroll capability; so it must fit in the label.
--
Thank you.


"John K" wrote:

> My label is multiline capable on the CE device. Thus, wordwrapping and other
> issues related to multi-line will be different on the CE device compared to a
> different sized text box in my PC application. I need to pay attention to
> both length and height. Thus, how do I determine that some text will fit,
> without clipping in either the X or Y axis, in a label (i.e. before I place
> it in the label per my application.)?
> --
> Thank you.
>
>
> "Linda Liu [MSFT]" wrote:
>
> > Hi John,
> >
> > I agree what Ciaran has suggested.
> >
> > The Graghics.MeasureString method has several overloads. To determine
> > whether a specified string when drawn with the specified Font within a
> > limited width is clipped, we could use the certain overload
> > Graphics.MeasureString(String, Font, Int32).
> >
> > Use the above method in your PC application. In your scenario, pass the
> > string to be checked, the font("Microsoft Sans Serif", 18pt) and the width
> > value of the label in your device application to the method.
> >
> > To obtain a Graphics, you could call the form's CreateGraphics method.
> >
> > The following is a sample.
> >
> > Function MeasureStringHeight(ByVal text As String, ByVal font As Font,
> > ByVal maxwidth As Int32) As Single
> > ' Me refers to the form
> > Dim g As Graphics = Me.CreateGraphics
> > Dim textsize As SizeF
> > textsize = g.MeasureString(text, font, maxwidth)
> > Return textsize.Height
> > End Function
> >
> > You could call this function before saving the data into DB. If the
> > returned height value is greater than the height value of the label in your
> > device application, set the focus back to the textbox and show an alert
> > text with an error provider besides the textbox.
> >
> > You could also handle the Validating event of the textbox and call the
> > above MeasureStringHeight in the event handler. If the result is greater
> > than the height value of the label in your device application, set the
> > Cancel property of the System.ComponentModel.CancelEventArgs class to false.
> >
> > Hope this helps.
> > If you have anything unclear, please feel free to let me know.
> >
> >
> > Sincerely,
> > Linda Liu
> > Microsoft Online Community Support
> >
> > ==================================================
> > Get notification to my posts through email? Please refer to
> > http://msdn.microsoft.com/subscripti...ult.aspx#notif
> > ications.
> >
> > Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
> > where an initial response from the community or a Microsoft Support
> > Engineer within 1 business day is acceptable. Please note that each follow
> > up response may take approximately 2 business days as the support
> > professional working with you may need further investigation to reach the
> > most efficient resolution. The offering is not appropriate for situations
> > that require urgent, real-time or phone-based interactions or complex
> > project analysis and dump analysis issues. Issues of this nature are best
> > handled working with a dedicated Microsoft Support Engineer by contacting
> > Microsoft Customer Support Services (CSS) at
> > http://msdn.microsoft.com/subscripti...t/default.aspx.
> > ==================================================
> >
> > This posting is provided "AS IS" with no warranties, and confers no rights.
> >
> >

 
Reply With Quote
 
Linda Liu [MSFT]
Guest
Posts: n/a
 
      17th Nov 2006
Hi John,

Thank you for your reply.

Firstly, I'm sorry that I missed telling you in my first reply that issues
related to .NET Compact Framework are out of our support range at present.
To get more professional assistance on problems about .NET Compact
Framework, you may post your questions at
microsoft.public.dotnet.framework.compactframework newsgroup later.

As for this particular issue, it is related to both .NET Compact Framework
and .NET Framework. So my reply will mainly focus on the .NET Framework
aspect.

When we caculate the size of a text in .NET program, we are using the
graphics object associated with the form, not the textbox. Although there
will be a little difference about graphics' caculating in .NET program and
NET Compact program, the difference is not big.

It seems that you're using the overload method
"Graphics.MeasureString(String, Font, SizeF, StringFormat, out Int32, out
Int32)" in your test project, which set the maximum size of both width and
height. To get the correct value of the text's height, I recommend you to
use another overload method "Graphics.MeasureString(String, Font, Int 32)",
which only set a maximum width to the text to be caculated. You could get
the value of the height from the Height property of the returned size.

Hope this helps.
If you have anything unclear, please feel free to let me know.


Sincerely,
Linda Liu
Microsoft Online Community Support

 
Reply With Quote
 
=?Utf-8?B?Sm9obiBL?=
Guest
Posts: n/a
 
      17th Nov 2006
Actually my problem occurs on a PC (not CE) as I type in a text box and then
display the text in a label that will be the same size on the CE device; so
for now the problem doesn't deal with a CE device. On the PC, I called the
Graphics.MeasureString(String, Font, Int 32) function and passed in the
maximum length as 584 pixels and I look at the returned height value to make
sure it is under the maximum height of the label (95 pixels). Most of the
time it works, but in the case where I just hold down the "w" key and
populate the text box with just a constant "w"; the returned values are 579 x
84 so the text should fit in the label box (584 x 96); but it doesn't - it
clips. So I type in something like wwwwww....www! and I am not seeing the
"!" in th label. It is clipping. What else can I do?
--
Thank you.


"Linda Liu [MSFT]" wrote:

> Hi John,
>
> Thank you for your reply.
>
> Firstly, I'm sorry that I missed telling you in my first reply that issues
> related to .NET Compact Framework are out of our support range at present.
> To get more professional assistance on problems about .NET Compact
> Framework, you may post your questions at
> microsoft.public.dotnet.framework.compactframework newsgroup later.
>
> As for this particular issue, it is related to both .NET Compact Framework
> and .NET Framework. So my reply will mainly focus on the .NET Framework
> aspect.
>
> When we caculate the size of a text in .NET program, we are using the
> graphics object associated with the form, not the textbox. Although there
> will be a little difference about graphics' caculating in .NET program and
> .NET Compact program, the difference is not big.
>
> It seems that you're using the overload method
> "Graphics.MeasureString(String, Font, SizeF, StringFormat, out Int32, out
> Int32)" in your test project, which set the maximum size of both width and
> height. To get the correct value of the text's height, I recommend you to
> use another overload method "Graphics.MeasureString(String, Font, Int 32)",
> which only set a maximum width to the text to be caculated. You could get
> the value of the height from the Height property of the returned size.
>
> Hope this helps.
> If you have anything unclear, please feel free to let me know.
>
>
> Sincerely,
> Linda Liu
> Microsoft Online Community Support
>
>

 
Reply With Quote
 
Linda Liu [MSFT]
Guest
Posts: n/a
 
      20th Nov 2006
Hi John,

Thank you for your reply.

I performed a test based on your description and did see the same thing as
you did.

I think it is because the text displayed in the label has some space from
the left border of the label, however, the Graphics.MeasureString method
doesn't take this space into account.

To solve this problem, I think we could set the maximum width passed to the
MeasureString method to be smaller, e.g. 10 pixels smaller than the width
of the label.

Hope this helps.
If you have anything unclear, please feel free to let me know.


Sincerely,
Linda Liu
Microsoft Online Community Support

 
Reply With Quote
 
=?Utf-8?B?Sm9obiBL?=
Guest
Posts: n/a
 
      20th Nov 2006
I tried what you suggested (passing in a value that 10 pixels smaller in
widt) with the same problem. I am quite surpised that something as
fundamental as determining if some text will fit in a label without clipping
can be so difficult. Any other suggestions? I would like as much as
possible to avoid "fudging" the calculations (i.e. reducing by 10 pixels) and
get as close as possible as we want to maximize the number of characters the
user can enter.
--
Thank you.


"Linda Liu [MSFT]" wrote:

> Hi John,
>
> Thank you for your reply.
>
> I performed a test based on your description and did see the same thing as
> you did.
>
> I think it is because the text displayed in the label has some space from
> the left border of the label, however, the Graphics.MeasureString method
> doesn't take this space into account.
>
> To solve this problem, I think we could set the maximum width passed to the
> MeasureString method to be smaller, e.g. 10 pixels smaller than the width
> of the label.
>
> Hope this helps.
> If you have anything unclear, please feel free to let me know.
>
>
> Sincerely,
> Linda Liu
> Microsoft Online Community Support
>
>

 
Reply With Quote
 
Linda Liu [MSFT]
Guest
Posts: n/a
 
      27th Nov 2006
Hi John,

I think we may go a little far from the original question. Even if we could
work out whether a specified text will be clipped or not within a label in
WinForms application very accurately, the result maybe not accurate in
Device application as well.

I mean we may not have an accurate method to solve this problem.

I suggest that you calculate a specified text in WinForms application using
Graphics.MeasureString method and then display the specified text within a
label in Device application to see if the text fits in the label. If not,
adjust value of the "width" parameter passed to the Graphics.MeasureString
method, until the calculating is accurate for the Device application.

Hope this helps.
If you have any concerns, please feel free to let me know.


Sincerely,
Linda Liu
Microsoft Online Community Support

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Determining the length of a presentation =?Utf-8?B?RG9uIEQ=?= Microsoft Powerpoint 1 11th May 2007 10:33 PM
Determining When Table last had data entered Gary Townsend \(Spatial Mapping Ltd.\) Microsoft Access 13 2nd Jun 2006 06:38 PM
Determining length of a string in pixels =?Utf-8?B?SGF5U2VlZA==?= Microsoft Dot NET Framework Forms 1 8th Dec 2005 10:49 PM
How to limit the length of text entered into an item in a ListView? Pep Microsoft C# .NET 1 15th Jun 2005 11:54 AM
Determining the length of text blue Microsoft ASP .NET 1 20th Nov 2003 07:29 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 03:55 AM.