首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何创建“下一步”和“上一步”按钮来导航列表?

如何创建“下一步”和“上一步”按钮来导航列表?
EN

Stack Overflow用户
提问于 2019-04-12 03:38:44
回答 2查看 496关注 0票数 0

我正在做这个项目,我被卡住了。我有一个表单,在表单上,我有一个下一步按钮和上一步按钮:

在我的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;
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-04-12 03:51:05

您需要有一个索引来告诉您哪个索引是当前索引。最小索引的索引将被比较为0,而最大索引的索引将被比较为would List.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
EN

Stack Overflow用户

发布于 2019-04-12 03:50:07

这是下一项:

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

这将在到达最后一项时循环。如果你想让它停止,改变逻辑。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55640003

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档