RE: Determining max. length of text that can be entered for anothe
- From: John K <KTJ@xxxxxxxxxxxxxxxxx>
- Date: Thu, 16 Nov 2006 10:09:02 -0800
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/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.
- Follow-Ups:
- RE: Determining max. length of text that can be entered for anothe
- From: Linda Liu [MSFT]
- RE: Determining max. length of text that can be entered for anothe
- References:
- RE: Determining max. length of text that can be entered for another de
- From: Linda Liu [MSFT]
- RE: Determining max. length of text that can be entered for anothe
- From: John K
- RE: Determining max. length of text that can be entered for another de
- Prev by Date: RE: Determining max. length of text that can be entered for anothe
- Next by Date: Re: public variables vs properties
- Previous by thread: RE: Determining max. length of text that can be entered for anothe
- Next by thread: RE: Determining max. length of text that can be entered for anothe
- Index(es):
Relevant Pages
|
Loading