2

リンクリストを使用しています。リストは、と呼ばれる複数行のテキストボックスに表示されるフォームの読み込み(サイズ、高さ、在庫、価格)で表示されtextBoxResultsます。First、、と呼ばれる3つのボタンがNextありLastます。命名規則は簡単です。ボタン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();
        }
    }
}
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++;
        }

次に、次のメソッドで:

    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 に答える