2

我正在使用链接列表。该列表是在名为textBoxResults. 我有三个按钮叫做First,NextLast。命名约定很简单,按钮First单击显示第一个树, Next在第一次单击按钮后显示每个项目, Last按钮显示列表中的最后一个项目。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();
        }
    }
}
4

1 回答 1

2

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

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

在“最后”方法中:

    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();
    }
于 2012-12-11T07:30:26.287 回答