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. How to count pixels

How to count pixels

Scheduled Pinned Locked Moved C#
helptutorial
3 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.
  • A Offline
    A Offline
    A Asif
    wrote on last edited by
    #1

    Hi All, I am adding a string value to listbox control. I need to set the width of listbox in runtime base on how many pixels string need it to display it within listbox. Idea is to set a width of listbox exactly the number of pixels required by string value. So, I can have perfact width each time base on string value. Any idea please help. Thanks, Asif.

    A.Asif

    V S 2 Replies Last reply
    0
    • A A Asif

      Hi All, I am adding a string value to listbox control. I need to set the width of listbox in runtime base on how many pixels string need it to display it within listbox. Idea is to set a width of listbox exactly the number of pixels required by string value. So, I can have perfact width each time base on string value. Any idea please help. Thanks, Asif.

      A.Asif

      V Offline
      V Offline
      V 0
      wrote on last edited by
      #2

      look into the Graphics object. I think you can calculate the width of a string there, but you'll need to pass the font properties as well.

      V. If I don't see you in this world, I'll see you in the next one... And don't be late. (Jimi Hendrix)

      1 Reply Last reply
      0
      • A A Asif

        Hi All, I am adding a string value to listbox control. I need to set the width of listbox in runtime base on how many pixels string need it to display it within listbox. Idea is to set a width of listbox exactly the number of pixels required by string value. So, I can have perfact width each time base on string value. Any idea please help. Thanks, Asif.

        A.Asif

        S Offline
        S Offline
        Skippums
        wrote on last edited by
        #3

        TextBox tb = new TextBox(); ... Graphics g = null; try { g = tb.CreateGraphics(); SizeF result = g.MeasureString(tb.Text, tb.Font); // you may need to take into account margin, padding, or border sizes tb.Width = (int)Math.Ceiling(result.Width); } finally { if (g != null) g.Dispose(); } Jeff

        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