marcuthh
marcuthh

Reputation: 596

C# - Listbox not displaying items

I'm working on a program that functions as a carpark simulator. I want the user interface to display how many spaces are available on each level at any given time. I've attempted to do this using listboxes (one for level number, one for number of spaces on that level), but can't get it to work. For some reason, the listbox that displays the spaces available (listboxSA) always comes up blank and I have no idea why.

The code that creates the listboxes is shown below:

    public void updateLevelLabels(Simulator simulator)
    {
        //constant integers used for label positioning
        //y coordinate for first label
        const int YSTARTPOINT = 12;
        //x coordinate for all labels
        const int XSTARTPOINT = 104;

        //create new listbox to show level IDs
        ListBox listboxLevels = new ListBox();
        //position listbox on form
        //constant x-coordinate
        listboxLevels.Left = XSTARTPOINT;
        //constant y-coordinate
        listboxLevels.Top = YSTARTPOINT;
        //auto-assumes size depending on content
        listboxLevels.AutoSize = true;

        //create new listbox to show spaces available
        ListBox listboxSA = new ListBox();
        //set x and y coordinates
        //constant x coordinate
        listboxSA.Left = XSTARTPOINT + 38;
        //constant y coordinate
        listboxSA.Top = YSTARTPOINT;
        //auto-resizes depending on content
        listboxSA.AutoSize = true;

        //populate listboxes
        for (int i = 0; i < Length(); i++)
        {
            //identify level at current index
            Level lev = At(i);

            //add level unique ID to list
            listboxLevels.Items.Add(lev.getLevelID());
            //add number of spaces (available) on level to list
            listboxSA.Items.Add(lev.getNumSpaces().ToString());
        }

        //place listboxes on form
        simulator.Controls.Add(listboxLevels);
        simulator.Controls.Add(listboxSA);
    }

I've debugged the code and the value for the lev.numSpaces variable is what I'd expect it to be. I've also tried to select the indices of the listboxSA after it's creation and the created indices are selectable (listbox item becomes highlighted), but there is still no text in them.

I honestly have no idea what could be causing this to happen, especially weird considering the same procedure is essentially carried out on both listboxes with a differe get() function.

If anyone can spot what might be throwing it off, I'd really appreciate any advice!

Code of called functions called shown below:

    //from `Levels` class
    //Levels acts as a public interface for a `List<Level>`
    public int Length()
    {
        //return number of `Level` instances in collection (int)
        return levelList.Count;
    }

    //from `Level` class
    //obtain unique identifer of level
    public string getLevelID()
    {
        //return unique Level name
        return levelID;
    }

    //from `Level` class
    //obtain number of spaces on level
    //all spaces assumed to be free
    public int getNumSpaces()
    {
        //should = value of Levels.Length()
        return numSpaces;
    }

Thanks in advance,

Mark

Upvotes: 2

Views: 2977

Answers (1)

iceberg
iceberg

Reputation: 596

You should better check data which you are trying to apply to listbox or seek problem in other place. I made test which is doing the same as your code and there is no problem.

    string[] stringArray = new string[] { "one", "two", "three", "four" };
    int[] intArray = new int[] { 1, 2, 3, 4 };

    private void Addlistboxes()
    {
        ListBox lb1 = new ListBox();
        ListBox lb2 = new ListBox();

        lb1.Left = 10;
        lb1.Top = 60;
        lb1.AutoSize = true;

        lb2.Left = 15 + lb1.Width;
        lb2.Top = 60;
        lb2.AutoSize = true;

        for (int i = 0; i < 4; i++)
        {
            lb1.Items.Add(stringArray[i]);
            lb2.Items.Add(intArray[i]);
        }

        this.Controls.Add(lb1);
        this.Controls.Add(lb2);
    }`

Upvotes: 1

Related Questions