如何创建下一个和上一个按钮来浏览列表?

时间:2019-04-11 19:38:44

标签: c# list winforms button

我正在从事这个项目,但被困住了。我有一个表单,在表单上,​​我有一个“下一个”按钮和“上一个”按钮:

Form with the buttons and other functionality

在我的HouseList类中,我有这段代码,我想要做的是创建一个方法来获得下一所房子,并创建另一种方法来获得上一所房子。我已经尝试了很久了。

public HouseList()
{
    housesList = new List<House>();
    housesList.Add(new House()
    {
        Address = "35 Twin Oaks",
        City = "Glendale",
        Zip = "MDN6L3",
        AskingPrice = "328,743.00",
        PictureFile = "",
        AveragePrice = "490,747.80",
        Total = "2,453,739.00"
    });


    housesList.Add(new House()
    {
        Address = "35 Helen Drive",
        City = "OakDale",
        Zip = "G6L5M4",
        AskingPrice = "455,899.00",
        PictureFile = "",
        AveragePrice = "490,747.80",
        Total = "2,453,739,00"
    });

    housesList.Add(new House()
    {
        Address = "4 RiverBank Rd",
        City = "Brampton",
        Zip = "L9H4L2",
        AskingPrice = "699,999.00",
        PictureFile = "",
        AveragePrice = "490,747.80",
        Total = "2,453,739,00"
    });
}

public List<string> NextHouse()
{
    List<string> house = new List<string>();
    foreach (House h in housesList)
    {
        int index = 0;
        string conv = Convert.ToString(index);
        if (conv == house[1])
        {
            conv = house[1];
        }  
    }
    return house;
}

2 个答案:

答案 0 :(得分:1)

您需要有一个索引,该索引可以告诉您哪个索引是最新的。对于最小索引,索引将与0进行比较;对于最大索引,将与houseList.Count-1进行比较。考虑到这一点,NextHousePreviousHouse应该返回House而不是List<House>

public House NextHouse(){
    if(currentIndex + 1 != houseList.Count)
        currentIndex++;
    return houseList[currentIndex]
}

public House PreviousHouse(){
    if(currentIndex -1 >= 0)
        currentIndex--;
    return houseList[currentIndex];
}

因此,如果您已经在列表中的最后一个房子里时要求下一个房子,它将只返回最后一个房子。如果您在列表中的第一间时要求前一所房子,它将返回第一所房子。

您必须将currentIndex初始化为类成员才能执行此操作。

答案 1 :(得分:0)

这是下一个项目:

public List<House> housesList;
    int currentIndex = 0;

    public HouseList()
    {
        housesList = new List<House>();
        housesList.Add(new House()
        {
            Address = "35 Twin Oaks",
            City = "Glendale",
            Zip = "MDN6L3",
            AskingPrice = "328,743.00",
            PictureFile = "",
            AveragePrice = "490,747.80",
            Total = "2,453,739.00"
        });


        housesList.Add(new House()
        {
            Address = "35 Helen Drive",
            City = "OakDale",
            Zip = "G6L5M4",
            AskingPrice = "455,899.00",
            PictureFile = "",
            AveragePrice = "490,747.80",
            Total = "2,453,739,00"
        });

        housesList.Add(new House()
        {
            Address = "4 RiverBank Rd",
            City = "Brampton",
            Zip = "L9H4L2",
            AskingPrice = "699,999.00",
            PictureFile = "",
            AveragePrice = "490,747.80",
            Total = "2,453,739,00"
        });
    }

    public House NextHouse()
    {
        return housesList[(++currentIndex)%housesList.Count()];
    }

这将在到达最后一个项目时循环。如果您希望它停止运行,请更改逻辑。