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. Calculate length of textBox for N number of characters

Calculate length of textBox for N number of characters

Scheduled Pinned Locked Moved C#
question
5 Posts 4 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.
  • K Offline
    K Offline
    kanchoette
    wrote on last edited by
    #1

    How can I calculate the required length of a textBox to hold N characters please?

    M R A 3 Replies Last reply
    0
    • K kanchoette

      How can I calculate the required length of a textBox to hold N characters please?

      M Offline
      M Offline
      Mel Feik
      wrote on last edited by
      #2

      I dont think you can.. unless you know ahead of time what each character is going to be. A fair approximation would be 9xN since the default text is at 8.5 points. But this leave the box looking a bit "longer" than needed if it contains mostly "skinny" chars. Consider these 10 char field widths: OPOPOPOPOPO itititititi Perhaps if there is a setting that forces the character spacing to be equal - then you can certainly make a calculation.

      --------------------------------------------- Help... I'm embedded and I can't get out! If they don't get the basic research and learning skills down then they'll end up having a very hard life (Either that or they'll become managers) - Micheal P Butler

      1 Reply Last reply
      0
      • K kanchoette

        How can I calculate the required length of a textBox to hold N characters please?

        R Offline
        R Offline
        Rob Philpott
        wrote on last edited by
        #3

        Well, very often you can't depending on the font. Monospaced fonts like Courier you could, but for most you'll find a 'W' quite a bit wider than an 'I'. If you are using a monospaced font, you'd need to get a Graphics object, then call MeasureString (I think it is - or something like that). Which will tell you how wide a string is in pixels.

        Regards, Rob Philpott.

        1 Reply Last reply
        0
        • K kanchoette

          How can I calculate the required length of a textBox to hold N characters please?

          A Offline
          A Offline
          ABitSmart
          wrote on last edited by
          #4

          One way is, create a dummy label object(with AutoSize = true), assign the text to it. Use the labels width to set the TextBox's width :)

          K 1 Reply Last reply
          0
          • A ABitSmart

            One way is, create a dummy label object(with AutoSize = true), assign the text to it. Use the labels width to set the TextBox's width :)

            K Offline
            K Offline
            kanchoette
            wrote on last edited by
            #5

            Many thanks to everyone who replied. I like the dummy label idea - much appreciated.

            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