Reputation: 1385
I add values to a ListBox,
for(int i = 0; i < 2; ++i)
{
lbBeamValue.Items.Add("Beam " + i.ToString() + " : " + value1[i] + " Angle " + i.ToString() + " : " + value2[i]);
}
which is displayed something like:
Beam 0: 0.12 Angle 0: 0.65
Beam 1: 10.113213 Angle 1: 0.23
Is there a way to ensure that the Angle
aligns in every row? Like:
Beam 0: 0.12 Angle 0: 0.65
Beam 1: 10.113213 Angle 1: 0.23
Upvotes: 3
Views: 1618
Reputation: 6894
A listbox will still allow the use of tabs. You can achieve it like this:
public Form1 ()
{
InitializeComponent ();
// ....
int [] MyTabs = {20,70,130};
SetListTabs (lbMessages, MyTabs);
}
private void btnAddTabbed_Click (object sender, EventArgs e)
{
lbMessages.Items.Add ("1\t2\t3\t4");
lbMessages.Items.Add ("40\t50\t60\t70");
lbMessages.Items.Add ("100\t200\t300\t400");
}
private void SetListTabs (ListBox lb, IEnumerable<int> newTabs)
{
lb.UseCustomTabOffsets = true;
ListBox.IntegerCollection lbTabs = lb.CustomTabOffsets;
lbTabs.Clear ();
foreach (int tab in newTabs)
{
lbTabs.Add (tab);
}
}
private void btnAddTabbed_Click (object sender, EventArgs e)
{
lbMessages.Items.Add ("1\t2\t3\t4");
lbMessages.Items.Add ("40\t50\t60\t70");
lbMessages.Items.Add ("100\t200\t300\t400");
}
However I'd have to ask why you would want to do this when better options like a ListView with proper header support exist. I suppose for a quick hack, or a minimal-change retrofit to an existing app, then it's kind of defensible, but a ListView would be better.
Upvotes: 1
Reputation: 54433
No simple control will allow you the use of tabs; so you either need a control, like ListView
that has columns or you need to chose a fixe-sized font (like Consolas
) and pad each item to a given length with string.PadLeft
and string.PadRight
:
lbBeamValue.Font = new Font("Consolas", 8f);
for (int i = 0; i < 2; ++i)
{
listBox2.Items.Add("Beam " + (i).ToString("#0").PadLeft(2) + " : " +
value1[i].ToString().PadRight(11) + " Angle " + i.ToString() + " : " +
value2[i]);
};
Note the String.Format
also allows you to build a complex string with padding.
Upvotes: -1
Reputation: 166336
Why not rather make use of a ListView
Represents a Windows list view control, which displays a collection of items that can be displayed using one of four different views.
There you can specify the View and Columns
Upvotes: 2