2

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

9
  • 4
    What function Length() does? Can you add code of it? Commented Dec 3, 2015 at 11:56
  • 1
    What do lev.getLevelID() and lev.getNumSpaces() return? Did you try adding a watch on these while debugging? Commented Dec 3, 2015 at 11:58
  • ListBox.Items.Add() takes an object. You don't have to call ToString() if lev.getNumSpaces() returns an integer value. And since the method apparently calculates the correct value, the only thing I can think of is something going wrong with the ToString() call. Commented Dec 3, 2015 at 12:06
  • Code for these functions has been edited into the question. I watched these while debugging and both contained the expected values. The value from lev.getLevelID() is displayed in its listbox but the value of lev.getNumSpaces() does not appear. @Alex Commented Dec 3, 2015 at 12:06
  • 1
    Off topic question, but you really have so many comments in your code? Commented Dec 3, 2015 at 12:37

1 Answer 1

1

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);
    }`
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for this, I have removed the Listbox.AutoSize = true execution and this has made the data visible. Thank you for your input.
Please mark this as your answer if this has helped you to come to that conclusion.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.