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
  1. Home
  2. General Programming
  3. C#
  4. String Width

String Width

Scheduled Pinned Locked Moved C#
questionlounge
5 Posts 3 Posters 0 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.
  • L Offline
    L Offline
    Luther Baker
    wrote on last edited by
    #1

    Is there a simple way to explicitly get the width of text in a control? In general, I would like to dynamically size labels, buttons, listboxes, etc. depending on their contents. I don't have to get the font, draw the text somewhere else and measure it do I? Thanks, -Luther

    N 1 Reply Last reply
    0
    • L Luther Baker

      Is there a simple way to explicitly get the width of text in a control? In general, I would like to dynamically size labels, buttons, listboxes, etc. depending on their contents. I don't have to get the font, draw the text somewhere else and measure it do I? Thanks, -Luther

      N Offline
      N Offline
      ne2000
      wrote on last edited by
      #2

      I think you should check out Graphics.MeasureString Cheers, Henrik

      M L 3 Replies Last reply
      0
      • N ne2000

        I think you should check out Graphics.MeasureString Cheers, Henrik

        M Offline
        M Offline
        markdbd
        wrote on last edited by
        #3

        Thanks I was also looking for this.

        1 Reply Last reply
        0
        • N ne2000

          I think you should check out Graphics.MeasureString Cheers, Henrik

          L Offline
          L Offline
          Luther Baker
          wrote on last edited by
          #4

          That looks like its worth a shot. I guess, if text on a button spans 2 lines, I need to get the "size" of the button's print area. That may be different than the size of the button right? Maybe there is a "client area size" property or something ... In any case, that call starts me off. Thanks, -Luther

          1 Reply Last reply
          0
          • N ne2000

            I think you should check out Graphics.MeasureString Cheers, Henrik

            L Offline
            L Offline
            Luther Baker
            wrote on last edited by
            #5

            Ok - I know this is a naive question, but where do I get a graphics object. I am not in an event handler. I am in my custom control's constructor. Its not attached to anything. I just create a button and I've set the Text property, and now I want to set the Size property. It seems that I can't create an instance of the graphics object ... and this is not a static method. I'm not a GDI+ expert but I would guess that I can't just create a Graphics object because there is more to the drawing context that must be defined. Where am I drawing? Which window are we drawing to, etc. Maybe I can must measure the string size in the OnPaint handler (using the parameterized graphics object). Thanks, -Luther

            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