C# - 列表框不显示项目

时间:2015-12-03 11:52:40

标签: c# winforms listbox listboxitem

我正在开发一个充当停车场模拟器的程序。我希望用户界面能够在任何给定时间显示每个级别上可用的空间数。我试图使用列表框(一个用于级别编号,一个用于该级别上的空格数)来执行此操作,但无法使其工作。出于某种原因,显示可用空格(listboxSA)的列表框总是空白,我不知道为什么。

创建列表框的代码如下所示:

    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);
    }

我调试了代码,lev.numSpaces变量的值就是我所期望的。我还尝试在创建后选择listboxSA的索引,并且创建的索引是可选的(列表框项目会突出显示),但它们中仍然没有文本。

老实说,我不知道是什么导致这种情况发生,特别奇怪的是考虑到同样的程序基本上是在带有差异get()函数的两个列表框上进行的。

如果有人能发现可能会抛弃它的东西,我真的很感激任何建议!

调用的被调用函数代码如下所示:

    //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;
    }

提前致谢,

标记

1 个答案:

答案 0 :(得分:1)

您应该更好地检查您尝试应用于列表框的数据或在其他地方寻找问题。我做了与你的代码一样的测试,没有问题。

    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);
    }`