Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. problem with drawString()

problem with drawString()

Scheduled Pinned Locked Moved C#
helpquestion
5 Posts 2 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    arnoldpinto84
    wrote on last edited by
    #1

    I need drawstring() to print a trailing blank space but it seems to be trimming the string... does anyone know a workaround to this???

    L 1 Reply Last reply
    0
    • A arnoldpinto84

      I need drawstring() to print a trailing blank space but it seems to be trimming the string... does anyone know a workaround to this???

      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #2

      :confused: AFAIK Graphics.DrawString() only paints the foreground, i.e. pixels belonging to the characters in the string, so it does not paint spaces, it just skips them.

      Luc Pattyn [Forum Guidelines] [My Articles]


      this months tips: - use PRE tags to preserve formatting when showing multi-line code snippets - before you ask a question here, search CodeProject, then Google


      A 1 Reply Last reply
      0
      • L Luc Pattyn

        :confused: AFAIK Graphics.DrawString() only paints the foreground, i.e. pixels belonging to the characters in the string, so it does not paint spaces, it just skips them.

        Luc Pattyn [Forum Guidelines] [My Articles]


        this months tips: - use PRE tags to preserve formatting when showing multi-line code snippets - before you ask a question here, search CodeProject, then Google


        A Offline
        A Offline
        arnoldpinto84
        wrote on last edited by
        #3

        see the prob is that...i need to format numbers such that they are aligned. -ve numbers are denoted in brackets

        eg.
        5000
        (20)

        here the nos are not aligned...so i need to print a blank space after the +ve nos so that they align properly. eg.

        5000
        (20)

        is this possible using drawstring() ??????

        L 1 Reply Last reply
        0
        • A arnoldpinto84

          see the prob is that...i need to format numbers such that they are aligned. -ve numbers are denoted in brackets

          eg.
          5000
          (20)

          here the nos are not aligned...so i need to print a blank space after the +ve nos so that they align properly. eg.

          5000
          (20)

          is this possible using drawstring() ??????

          L Offline
          L Offline
          Luc Pattyn
          wrote on last edited by
          #4

          Hi, Graphics.DrawString() will paint a string at the given coordinates (specified by either a PointF or two floats, and indicating the top-left corner as always). So it is up to you to specify the right x-value to get the alignment you want. There are two possibilities: 1. using a non-proportional font, such as Courier New, where all symbols have the same width: you can simply keep x constant and adjust the string, i.e. add spaces to the left and/or the right, so the characters appear where you want them. 2. using an arbitrary font: since character widths may vary now, you must find out how wide a specific string is, and then adjust the x-value to get it to align properly. The width can be obtained from Graphics.MeasureString(). Be careful to specify the same parameters (text, font, brush, ...) you are going to pass to DrawString() later on. If you want the unit digits to align, you should split the string in two parts, the left one ending on the unit digit, the right one holding everything else (could be decimal point, decimals, and/or a closing parenthesis). Then calculate the width of the left part, and adjust it's x-value accordingly. The right part will have to be drawn at a constant x-value, since that one needs to be aligned. As you can see, with arbitrary fonts, spaces don't really come into the solution. :)

          Luc Pattyn [Forum Guidelines] [My Articles]


          this months tips: - use PRE tags to preserve formatting when showing multi-line code snippets - before you ask a question here, search CodeProject, then Google


          A 1 Reply Last reply
          0
          • L Luc Pattyn

            Hi, Graphics.DrawString() will paint a string at the given coordinates (specified by either a PointF or two floats, and indicating the top-left corner as always). So it is up to you to specify the right x-value to get the alignment you want. There are two possibilities: 1. using a non-proportional font, such as Courier New, where all symbols have the same width: you can simply keep x constant and adjust the string, i.e. add spaces to the left and/or the right, so the characters appear where you want them. 2. using an arbitrary font: since character widths may vary now, you must find out how wide a specific string is, and then adjust the x-value to get it to align properly. The width can be obtained from Graphics.MeasureString(). Be careful to specify the same parameters (text, font, brush, ...) you are going to pass to DrawString() later on. If you want the unit digits to align, you should split the string in two parts, the left one ending on the unit digit, the right one holding everything else (could be decimal point, decimals, and/or a closing parenthesis). Then calculate the width of the left part, and adjust it's x-value accordingly. The right part will have to be drawn at a constant x-value, since that one needs to be aligned. As you can see, with arbitrary fonts, spaces don't really come into the solution. :)

            Luc Pattyn [Forum Guidelines] [My Articles]


            this months tips: - use PRE tags to preserve formatting when showing multi-line code snippets - before you ask a question here, search CodeProject, then Google


            A Offline
            A Offline
            arnoldpinto84
            wrote on last edited by
            #5

            thanks a lot.. :-D

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups