C# ListBox Justify Text
-
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. -
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.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);
orstring 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.
-
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.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] }