Yousha Arif
Yousha Arif

Reputation: 1588

Reading a file in listBox within columns

I am trying to read a csv file so that it is fetched in form of rows and displayed in a ListBox, But I am unable to arrange in fix columns so that it is in a readable form.

I have tried using multiple columns property but still no luck.

Here what I have done so far

I expect the output to be like this :

expected output

Edit :

Here is my code for populating listBox

foreach (Student person in _result)
            {
                _listResults.Items.Add(person.ToString());
            }

Here is the bean class :

    class Student
{
    #region FIELDS

    private String _lastName, _firstName;
    private int _exam1, _exam2, _exam3, _finalExam;

    public string LastName
    {
        get
        {
            return _lastName;
        }

        set
        {
            _lastName = value;
        }
    }

    public string FirstName
    {
        get
        {
            return _firstName;
        }

        set
        {
            _firstName = value;
        }
    }

    public int Exam1
    {
        get
        {
            return _exam1;
        }

        set
        {
            _exam1 = value;
        }
    }

    public int Exam2
    {
        get
        {
            return _exam2;
        }

        set
        {
            _exam2 = value;
        }
    }

    public int Exam3
    {
        get
        {
            return _exam3;
        }

        set
        {
            _exam3 = value;
        }
    }

    public int FinalExam
    {
        get
        {
            return _finalExam;
        }

        set
        {
            _finalExam = value;
        }
    }


    #endregion

    #region CONSTRUCTOR
    public Student()
    {

    }

    public Student(string firstName, string lastName, int exam1, int exam2, int exam3, int final)
    {
        this._firstName = firstName;
        this._lastName = lastName;
        this._exam1 = exam1;
        this._exam2 = exam2;
        this._exam3 = exam3;
        this._finalExam = final;

    }

    #endregion

    #region METHODS

    public void _CalculateAverage(out float average)
    {
        average = _exam1 + _exam2 + _exam3 + _finalExam / 400;

    }

    public void _CalculateLetAverage(out String grade, float average)
    {
        if (average > 85)
            grade = "A+";
        else if (average > 75)
            grade = "A";
        else if (average > 65)
            grade = "B+";
        else if (average > 60)
            grade = "B";
        else if (average > 55)
            grade = "C+";
        else
            grade = "F";

    }

    public override string ToString()
    {
        return "   "+_firstName+" "+_lastName 
            + "                              " + _exam1
            + "                  " + _exam2
            + "                  " + _exam3
            + "                " + _finalExam;
    }

    #endregion

}

Upvotes: 1

Views: 197

Answers (2)

Steve
Steve

Reputation: 216243

As we have already told you, a ListBox is not really the correct control to use when you want your output "aligned" in columns. There is no provision in a ListBox to have a single line of text formatted in columns. Controls like ListView and DataGridView are better for this task because they have the concept of columns and you can use them in a natural way.

However, if you really want to use a ListBox and have your columns, then you need a compromise. You cannot use fonts with a variable width because in these fonts the letter W and the space have not the same width and this makes a mess to calculate the correct column size (and we are not even starting to talk how to calculate the white space needed to have a correct left/right alignment with different texts).

The compromise requires the use of a Fixed Width font (or Monospace or Fixed Pitch). There are many to choose from. (Consolas is my preferite). After setting the Font property of your listbox to your choosen fixed width font then you need to decide how to size your columns.
In my example below I have decided to have a FirstName column with 20 characters, the LastName with 30 character and the votes column 8 character each.

So your override for the ToString method will be:

public override string ToString()
{
    return $"{_firstName,-20} {_lastName,-30} " + 
            "{_exam1,8} {_exam2,8} {_exam3,8} {_finalExam,8}";
}

Those positive and negative numbers after the variables name are the alignment values as explained in the Format String Component (Beware, it is a large topic but worth to read and understand)

Upvotes: 2

Moo-Juice
Moo-Juice

Reputation: 38825

The standard ListBox control is not designed for formatting like this, it's basically just text - and it has no idea how to line up the content of the string with your labels, and nor should it!

Instead, you need a control that is suited to this exact job. It's called the DataGridView. There are plenty of tutorials online if you search to show you how to work with it.

EDIT: Steve also mentioned ListView in his comment, which can also fulfil your needs.

Upvotes: 0

Related Questions