Click here to Skip to main content
15,881,516 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm trying to align strings using string.format but it doesn't work as I want.
What it looks like:
Programmering     P
Matematik       U
Engelska      V


This is the code:
C#
public partial class Form1 : Form
    {
        List<string> kurs = new List<string>();
        List<string> lärare = new List<string>();
        public Form1()
        {
            InitializeComponent();
        }

        private void buttonSpara_Click(object sender, EventArgs e)
        {
            kurs.Add(textBoxKurs.Text);
            lärare.Add(textBoxLärare.Text);
            textBoxKurs.Text = string.Empty;
            textBoxLärare.Text = string.Empty;
            textBoxKurs.Focus();
        }

        private void buttonVisa_Click(object sender, EventArgs e)
        {
            listBoxOutput.Items.Add(string.Format("{0,-25} {1,-25}", "Kurs:", "Lärare:"));
            for (int i = 0; i < kurs.Count; i++)
            {
                listBoxOutput.Items.Add(string.Format("{0,-25} {1,-25}", kurs[i], lärare[i]));
            }
        }
    }


What I have tried:

I have tried different word for the strings and different values.
Posted
Updated 24-May-20 21:40pm
Comments
George Swan 25-May-20 3:36am    
Try using a ListView instead of a ListBox and put the text in separate columns

It's because the font that the ListBox is using to display the strings is a proportional font: not all the characters are the same width. For example, "i" is much narrower than "W", and "." is really narrow while "#" is a lot wider.

Change the Font property of your ListBox to "Courier New" and you'll get the text to line up the way you want. (You won't like it though!)
 
Share this answer
 
To display data in a tabular form, use DataGridView[^] control.

You'll need to make some changes in your code:
1. Add new class:
C#
public class Föremål
{
	public string Kurs { get; set; }
	public string Lärare { get; set; }
	
	public Föremål(string _kurs, string _lärare)
	{
		Kurs = _kurs;
		Lärare = _lärare;
	}
}


2. Add DataGridView control on your form and change it name to: DgvFöremåls

3. Then in your form:
C#
public partial class Form1 : Form
    {
        List<Föremål> föremåls = new List<Föremål>();
        BindingSource bs = new BindingSource();
        public Form1()
        {
            InitializeComponent();
        }

        private void buttonSpara_Click(object sender, EventArgs e)
        {
            Föremål f = new Föremål(textBoxKurs.Text, textBoxLärare.Text)
            föremåls.Add(f);
            textBoxKurs.Text = string.Empty;
            textBoxLärare.Text = string.Empty;
            textBoxKurs.Focus();
            bs.DataSource = föremåls;
            DgvFöremåls.DataSource = bs;
        }

    }


For further details, please see: Bind data to DataGridView Control - Windows Forms | Microsoft Docs[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900