【问题标题】:How do I create next and previous buttons to navigate a list?如何创建下一个和上一个按钮来导航列表?
【发布时间】:2019-09-02 12:47:03
【问题描述】:

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

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

【问题讨论】:

标签: c# list winforms button


【解决方案1】:

您需要有一个索引来告诉您哪个索引是当前的。该索引将与最小索引的 0 和最大索引的 houseList.Count-1 进行比较。考虑到这一点,NextHousePreviousHouse 应该返回 House 而不是 List&lt;House&gt;

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

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

因此,如果您已经在列表中的最后一所房子上时要求下一所房子,它只会返回最后一所房子。如果您在列表中的第一个位置询问上一个房子,它将返回第一个房子。

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

【讨论】:

  • 我试图将它传递给 Form.cs 并没有发生任何事情。private void btnNext_Click(object sender, EventArgs e) { hList.NextHouse(); } 私有无效 btnPrevious_Click(对象发送者,EventArgs e) { hList.PreviousHouse(); }
  • @Mohamed 你从这些函数中获得了House 引用,你需要使用这个检索到的值来展示它。 house = hList.NextHouse(); //display house on form;
  • 我还是不明白。但我不知道这是不是你的意思?房子房子=新房子();房子 = hList.NextHouse();
  • @Mohamed 房屋在 HouseList, once you have them, you can retrieve them using NextHouse` 和 PreviousHouse 函数中初始化。检索它们后,您需要使用它们的信息在屏幕上显示它们。
【解决方案2】:

这是下一个项目:

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

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

【讨论】:

    猜你喜欢
    • 2018-12-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-27
    • 1970-01-01
    • 1970-01-01
    • 2011-02-19
    相关资源
    最近更新 更多