gpt4 book ai didi

c# - 显示项目在列表中的位置

转载 作者:太空宇宙 更新时间:2023-11-03 11:11:37 24 4
gpt4 key购买 nike

我正在使用链接列表。该列表在名为 textBoxResults 的多行文本框中显示的表单加载(尺寸、高度、库存、价格)上出现。我有三个按钮,分别是 FirstNextLast。命名约定很简单,单击按钮First 显示第一棵树,Next 显示第一次单击每个按钮后的每个项目,Last 按钮显示最后一个 list 上的项目。 First 工作正常,但 Second 仅在 first 之后显示以下项目并放弃,Last 显示奇怪的结果 WindowsFormsApplication1.Form1+Fruit Trees 。如何通过单击按钮根据其位置显示正确的树名?

代码

namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}



public class ListOfTrees
{
private int size;

public ListOfTrees()
{
size = 0;
}

public int Count
{
get { return size; }
}

public FruitTrees First;
public FruitTrees Last;




}


public void ShowTrees()
{

}



public void Current_Tree()
{
labelSpecificTree.Text = Trees.First.Type.ToString();
}

private void Form1_Load_1(object sender, EventArgs e)
{

}

private void buttonFirst_Click(object sender, EventArgs e)
{
Current_Tree();
}

private void buttonNext_Click(object sender, EventArgs e)
{
Current = Trees.First;
labelSpecificTree.Text = Current.Next.Type.ToString();
}

private void buttonLast_Click(object sender, EventArgs e)
{
Current = Trees.Last;
labelSpecificTree.Text = Trees.Last.ToString();
}
}
}

最佳答案

您的代码中存在一些问题(请参阅评论以进行更正):

        public int Add(FruitTrees NewItem)
{
FruitTrees Sample = new FruitTrees();
Sample = NewItem;
Sample.Next = First;
First = Sample;
//Last = First.Next;
// Since Add is an operation that prepends to the list - only update
// Last for the first time:
if (Last == null){
Last = First;
}

return size++;
}

其次在您的 Next 方法中:

    private void buttonNext_Click(object sender, EventArgs e)
{
// In order to advance you need to take into account the current position
// and not set it to first...
//Current = Trees.First;
Current = Current.Next != null ? Current.Next : Current;
labelSpecificTree.Text = Current.Type.ToString();
}

在“last”方法中:

    private void buttonLast_Click(object sender, EventArgs e)
{
Current = Trees.Last;
// show the data, not the name of the Type
labelSpecificTree.Text = Current.Type.ToString();
}

当然,当前的方法也被破坏了,还有...

    public void Current_Tree()
{
Current = Trees.First;
labelSpecificTree.Text = Current.Type.ToString();
}

关于c# - 显示项目在列表中的位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13815487/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com