显示项目在列表中的位置

时间:2012-12-11 07:24:54

标签: c#

我正在使用链接列表。该列表显示在名为textBoxResults的多行文本框中显示的表单载荷(大小,高度,库存,价格)上。我有三个名为FirstNextLast的按钮。命名约定很简单,按钮First点击显示第一个树,Next,首先通过每个按钮点击显示每个项目,Last按钮显示列表中的最后一个项目。 First工作正常但Second仅在首先显示以下项目并放弃,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();
        }
    }
}

1 个答案:

答案 0 :(得分: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();
    }