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

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

Guest

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?
 
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
 
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/subscriptions/managednewsgroups/default.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/subscriptions/support/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
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.)?
 
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.
 
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
 
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?
 
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
 
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.
 
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
 
Hello Linda,

I ran into a new problem that is related to this issue. A label on a CE.NET
1.1 application does not wrap text if there are no breaks in the text. For
example if I have a label that is 584, 96 pixesl in size and I set the text
property to 100 1's i.e. 111111111 (etc.) with no spaces; the text clips on
the screen on the CE device. The label has enough space for 3 lines (Sans
Serif 18 pt normal font). Apparently a label on a CE device by default does
not wrap the text to the next line. Thus all you see is part of the text,
all on one line, that is clipped on both ends. However, in the Development
environment, the same text displays correctly as it wraps to the next two
lines. Is there a way to force the .NET CF framework to split up a long
continuous text entry into multiple lines; not requiring spaces in the text?
Thus the CE device displays the label like:

.1111111111111111111111111111111111111111111.

and in the development environment it looks like:

1111111111111111111111111111111111111111111
1111111111111111111111111111111111111111111
11111111111
 
Hi,

I could reproduce the problem on my side. I searched in our inner database
and found that it is a known bug of the Label in Compact Framework.

This bug may be fixed in the next version of Compact Framework. As for the
workaround, you may use TextBox instead of Label to display the text.

Hope this helps.

Sincerely,
Linda Liu
Microsoft Online Community Support
 
In what version(s) of the framework does the bug exist? In version 1.1 and
2.0? In my original message I mentioned I am using CF 1.1, but we could go
to the 2.0 if it fixes this problem.
 
Hi,

The bug exists in Compact Framework 1.1 and 2.0, but it has been fixed in
Windows CE .NET 4.2. The document I found in our inner database says that
this bug may not be fixed at present in CF1.1 and 2.0.

I still suggest that you use TextBox instead of Label in your project.

Sorry for the inconvenience!


Sincerely,
Linda Liu
Microsoft Online Community Support
 
Hello.

Thank you. I plan on using a text box. However, I am confused. We are
using a CE 4.2 device, yet the bug still exists. Perhaps only certain
editions of a CE 4.2 device implement this fix? I also assume you mean that
a CE 4.2 device has the compact framework already built in, so that's why you
are saying its fixed in CE 4.2?
 
Hi,

Thank you for your response.

The document I found in our inner database just says that this bug has been
fixed in CE .NET 4.2. Maybe this is a cerain edition of a CE 4.2 device. I
consider CE .NET 4.2 has the compact framwork already built in.


Sincerely,
Linda Liu
Microsoft Online Community Support
 
Back
Top