Problem measuring a string

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Martin Horn

    #1

    Problem measuring a string

    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.


  • AMercer

    #2
    RE: Problem measuring a string

    > As I can't figure out how to obtain the graphics object for the printer[color=blue]
    > without starting a print job I am stuck.[/color]

    Give this a try:

    Dim pd As New Drawing.Printin g.PrintDocument
    Dim g As Graphics = pd.PrinterSetti ngs.CreateMeasu rementGraphics( )

    It sounds like what you want. I have no experience with it, so no guarantees.

    Comment

    • Martin Horn

      #3
      Re: Problem measuring a string

      Thankyou,

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



      "AMercer" <AMercer@discus sions.microsoft .com> wrote in message
      news:9848B471-FB1B-4536-A7A0-944E45A37179@mi crosoft.com...[color=blue][color=green]
      >> As I can't figure out how to obtain the graphics object for the printer
      >> without starting a print job I am stuck.[/color]
      >
      > Give this a try:
      >
      > Dim pd As New Drawing.Printin g.PrintDocument
      > Dim g As Graphics = pd.PrinterSetti ngs.CreateMeasu rementGraphics( )
      >
      > It sounds like what you want. I have no experience with it, so no
      > guarantees.[/color]


      Comment

      • CMM

        #4
        Re: Problem measuring a string

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

        --
        -C. Moya

        "AMercer" <AMercer@discus sions.microsoft .com> wrote in message
        news:9848B471-FB1B-4536-A7A0-944E45A37179@mi crosoft.com...[color=blue][color=green]
        >> As I can't figure out how to obtain the graphics object for the printer
        >> without starting a print job I am stuck.[/color]
        >
        > Give this a try:
        >
        > Dim pd As New Drawing.Printin g.PrintDocument
        > Dim g As Graphics = pd.PrinterSetti ngs.CreateMeasu rementGraphics( )
        >
        > It sounds like what you want. I have no experience with it, so no
        > guarantees.[/color]


        Comment

        • Herfried K. Wagner [MVP]

          #5
          Re: Problem measuring a string

          "CMM" <cmm@nospam.com > schrieb:[color=blue]
          > Remember to always call Dispose on Graphics objects that you create. They
          > hold on to Win32 GDI resources if you don't.[/color]

          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/>

          Comment

          • Martin Horn

            #6
            Re: Problem measuring a string

            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.

            "Herfried K. Wagner [MVP]" <hirf-spam-me-here@gmx.at> wrote in message
            news:%23Jn3nwgP GHA.3976@TK2MSF TNGP10.phx.gbl. ..[color=blue]
            > "CMM" <cmm@nospam.com > schrieb:[color=green]
            >> Remember to always call Dispose on Graphics objects that you create. They
            >> hold on to Win32 GDI resources if you don't.[/color]
            >
            > 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/>[/color]


            Comment

            Working...