Home All Groups Group Topic Archive Search About

Problem measuring a string

Author
28 Feb 2006 7:47 PM
Martin Horn
Hi all,

I wonder if someone can help me out with this problem.

I want to confine input of characters in a textbox to the amount that will
fit in a given width on printed output.

For example on the form that my program prints there is a space for text
that is 100mm wide, the string for which is provided by the customer by
entering it into a textbox prior to it being output to the printer.

How can I stop the user from entering more chars than will fit. Just setting
the maxlength property of the textbox isn't a very elegant solution as the
printed font isn't fixed width.

I assume I need to be able to check the length of the string as each char is
typed and decide whether it will fit or not and therefore decide whether
further input should be allowed or not.

As I can't figure out how to obtain the graphics object for the printer
without starting a print job I am stuck.

Can anyone provide a solution or at least point me in the right direction.

Many thanks,

Martin.

Author
28 Feb 2006 9:39 PM
AMercer
> As I can't figure out how to obtain the graphics object for the printer
> without starting a print job I am stuck.

Give this a try:

    Dim pd As New Drawing.Printing.PrintDocument
    Dim g As Graphics = pd.PrinterSettings.CreateMeasurementGraphics()

It sounds like what you want.  I have no experience with it, so no guarantees.
Author
28 Feb 2006 10:09 PM
Martin Horn
Thankyou,

using your suggestion I have managed to come up with a solution that works.



Show quoteHide quote
"AMercer" <AMer***@discussions.microsoft.com> wrote in message
news:9848B471-FB1B-4536-A7A0-944E45A37179@microsoft.com...
>> As I can't figure out how to obtain the graphics object for the printer
>> without starting a print job I am stuck.
>
> Give this a try:
>
>    Dim pd As New Drawing.Printing.PrintDocument
>    Dim g As Graphics = pd.PrinterSettings.CreateMeasurementGraphics()
>
> It sounds like what you want.  I have no experience with it, so no
> guarantees.
Author
2 Mar 2006 12:57 AM
CMM
Remember to always call Dispose on Graphics objects that you create. They
hold on to Win32 GDI resources if you don't.

--
-C. Moya
www.cmoya.com
Show quoteHide quote
"AMercer" <AMer***@discussions.microsoft.com> wrote in message
news:9848B471-FB1B-4536-A7A0-944E45A37179@microsoft.com...
>> As I can't figure out how to obtain the graphics object for the printer
>> without starting a print job I am stuck.
>
> Give this a try:
>
>    Dim pd As New Drawing.Printing.PrintDocument
>    Dim g As Graphics = pd.PrinterSettings.CreateMeasurementGraphics()
>
> It sounds like what you want.  I have no experience with it, so no
> guarantees.
Author
2 Mar 2006 3:15 PM
Herfried K. Wagner [MVP]
"CMM" <cmm@nospam.com> schrieb:
> Remember to always call Dispose on Graphics objects that you create. They
> hold on to Win32 GDI resources if you don't.

That's a good idea.  In VB 2005 you could even utilize the 'Using' block
statement:

\\\
Using g As Graphics = ...
    ...
End Using
///

--
M S   Herfried K. Wagner
M V P  <URL:http://dotnet.mvps.org/>
V B   <URL:http://classicvb.org/petition/>