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. C# ListBox Justify Text

C# ListBox Justify Text

Scheduled Pinned Locked Moved C#
questioncsharpcareer
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.
  • F Offline
    F Offline
    fjlv2005
    wrote on last edited by
    #1

    Good day, I want to justify the text in my listbox. I've try to insert items in which both have the same length however the amount area won't align considering both items have the same length. How can I make the listbox justify its items so that both amount are on the right edge? Here is the code that i've tried but items arent justified: private void Form1_Load(object sender, EventArgs e) { string stramount1 = "900.00"; string stramount2 = "3400.00"; string description1 = "Basic Salary......................"+stramount1; string description2 = "Basic Salary....................."+stramount2; listBox1.Items.Add(description1); listBox1.Items.Add(description2); } thanks.

    D L 2 Replies Last reply
    0
    • F fjlv2005

      Good day, I want to justify the text in my listbox. I've try to insert items in which both have the same length however the amount area won't align considering both items have the same length. How can I make the listbox justify its items so that both amount are on the right edge? Here is the code that i've tried but items arent justified: private void Form1_Load(object sender, EventArgs e) { string stramount1 = "900.00"; string stramount2 = "3400.00"; string description1 = "Basic Salary......................"+stramount1; string description2 = "Basic Salary....................."+stramount2; listBox1.Items.Add(description1); listBox1.Items.Add(description2); } thanks.

      D Offline
      D Offline
      DMWhiteDragon
      wrote on last edited by
      #2

      Simple answer? You dont You could try measuring the text width and calculate the remaining space and fill it with calulated amount of spaces however you wont be able to get exact alignment as spaces in each font have a set size more than one pixel. Delete your listbox and add a listview control and use that You can add columns (so each part has a header), change alignment of each column and its easy to add items: ListViewItem MyItem = new ListViewItem(new string[] {"Bob Marley","$10,000"} ); listView1.Items.Add(MyItem); or string sDescription = "Bob Marley"; string sSalary = "$10,000"; ListViewItem MyItem = new ListViewItem(new string[] { sDescription, sSalary }); listView1.Items.Add(MyItem); -- modified at 0:50 Monday 9th July, 2007 Just to be clear, this is all considering there is more than one column... if you just need the salary in there and nothing else and want it right aligned you set the RightToLeft property to Yes. Thought i should cover all bases :)

      Always more to learn, and i wouldn't have it any other way.

      1 Reply Last reply
      0
      • F fjlv2005

        Good day, I want to justify the text in my listbox. I've try to insert items in which both have the same length however the amount area won't align considering both items have the same length. How can I make the listbox justify its items so that both amount are on the right edge? Here is the code that i've tried but items arent justified: private void Form1_Load(object sender, EventArgs e) { string stramount1 = "900.00"; string stramount2 = "3400.00"; string description1 = "Basic Salary......................"+stramount1; string description2 = "Basic Salary....................."+stramount2; listBox1.Items.Add(description1); listBox1.Items.Add(description2); } thanks.

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

        Hi, if you really want to do that with a ListBox there are two ways: 1. select a "non-proportional" font (such as "Courier New") where all characters have the same width; then adjust the spaces/dots/whatever in your strings so they all have the same length. or 2. Choose to draw the ListBox items yourself, by setting DrawMode to OwnerDraw; and provide a handler for the DrawItem event (similar to a paint handler when drawing in say a Panel). You do know the items in a ListBox dont have to be strings ? They can be instances of whatever class you come up with, and your DrawItem handler could make use of that... :)

        Luc Pattyn


        try { [Search CP Articles] [Search CP Forums] [Forum Guidelines] [My Articles] } catch { [Google] }


        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