Problem measuring a string

M

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.
 
G

Guest

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.
 
M

Martin Horn

Thankyou,

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

CMM

Remember to always call Dispose on Graphics objects that you create. They
hold on to Win32 GDI resources if you don't.
 
H

Herfried K. Wagner [MVP]

CMM said:
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

Martin Horn

Thanks guys,

checking back on my code, I had indeed forgotten to dispose of it. Now using
a 'using/end using' block as suggested by Herfried.

Martin.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top