我正在處理列表。我已經能夠確定列表中項目的第一個和最後一個位置。我正在使用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();
}
}
}
是的,不錯。我正在嘗試移動到列表中的下一個樹形對象。從按鈕點擊我將不得不增加'當前++'?或者我該怎麼做呢? – CodingWonders90
mainlist.current ++; 之後,調用getCurrent()的下一個調用將有下一個tree_object –
好吧,我添加了增量'mainlist.current ++',然後在按鈕內部調用'getCurrent'(在我的情況下更改爲'GetNextTree'),但第一次,我只有一棵樹,沒有別的。我編輯了新的更改的代碼。請檢查我的問題。 – CodingWonders90