我正在处理列表。我已经能够确定列表中项目的第一个和最后一个位置。我正在getPostion
通过Label
. 我的表单中的三个按钮:(ShowFirstItem
ShowNextItem
不起作用)并ShowLastItem
在标签中显示相应的项目。我的下一个项目有问题。我没有持有当前fruit_tree
. 所以我不确定如何添加一个int
成员或另一个fruit_tree
名为current
. 我怎样才能找到下一个(第一个之后的项目)并显示结果?
public class ListForTrees
{
public fruit_trees GetNextTree()
{
current = 0;
fruit_trees ft = first_tree;
int i = 0;
while (i != current)
{
ft = ft.next_tree;
i++;
}
return ft;
}
}
ListForTrees mainlist = new ListForTrees();
private void BtnGo_Click(object sender, EventArgs e)
{
fruit_trees[] ar_items = { new fruit_trees("cherry", 48, 12.95, 3),
new fruit_trees("pine", 36, 9.95, 8),
new fruit_trees("oak", 60, 14.95, 2),
new fruit_trees("peach", 54, 19.95, 3),
new fruit_trees("pear", 36, 11.85, 2),
new fruit_trees("apple", 62, 13.45, 5)
};
mainlist = new ListForTrees(ar_items);
fruit_trees current = mainlist.first_tree;
while (current != null)
{
TxtOutput.AppendText(current.ToString() + Environment.NewLine);
current = current.next_tree;
}
}
private void ShowFirstItem_Click_1(object sender, EventArgs e)
{
// Show First Item
labelSpecificTree.Text = mainlist.first_tree.GetTreeType;
}
private void ShowLastItem_Click(object sender, EventArgs e)
{
//Show Last Item
labelSpecificTree.Text = mainlist.last_tree.GetTreeType;
}
private void ShowNextItem_Click(object sender, EventArgs e)
{
//Show Next Item
fruit_trees obj = mainlist.GetNextTree();
if (obj == null)
{
labelSpecificTree.Text = "No more trees!";
}
else
{
mainlist.current++;
labelSpecificTree.Text = obj.next_tree.GetTreeType.ToString();
}
}
}