YES NO 上一个 下一个

https://files-cdn.cnblogs.com/files/xe2011/ListBoxDemo202013181554.rar


image


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

        ListStringArray list = new ListStringArray();
        private void Form1_Load(object sender, EventArgs e)
        {
            foreach (var item in listBox1.Items) {
                list.Add(item.ToString());
            }

            updateEnabled();
            Text = ( "" + list.Count );
        }

        public void updateEnabled() {
            buttonFirst.Enabled = list.CanFirstEnabled;
            buttonLast.Enabled = list.CanLastEnabled;
            buttonNext.Enabled = list.CanNextEnabled;
            buttonPrev.Enabled = list.CanPrevEnabled;
            buttonAutoNext.Enabled = list.CanAutoEnabled;
            buttonAutoPrev.Enabled = list.CanAutoEnabled;
            buttonDelete.Enabled = list.CanAutoEnabled;
            buttonYes.Enabled = list.CanAutoEnabled;
            buttonNo.Enabled = list.CanAutoEnabled;
        }

        private void button9_Click(object sender, EventArgs e) {
            //第一个
            label1.Text = list.getFirst();
            updateEnabled();
        }

        private void button7_Click(object sender, EventArgs e) {
            //最后一个
            label1.Text = list.getLast();
            updateEnabled();
        }

        private void button4_Click(object sender, EventArgs e) {
            MessageBox.Show(list.getCurrent());
        }

        private void button6_Click(object sender, EventArgs e) {
            //下一个
            label1.Text = list.getNext();
            updateEnabled();
        }

        private void button8_Click(object sender, EventArgs e) {
            //上一个
            label1.Text = list.getPrevious();
            updateEnabled();
        }

       //delete
        private void button5_Click(object sender, EventArgs e) {
            list.deleteCurrent();
            label1.Text = list.getCurrent();
            listBox1.Items.Clear();
            foreach (var item in list) {
                listBox1.Items.Add(item);
            }
            updateEnabled();
        }

        private void button11_Click(object sender, EventArgs e) {
            //自动下一个
            label1.Text = list.getAutoNext();
            updateEnabled();
        }

        private void button10_Click(object sender, EventArgs e) {
            //自动上一个
            label1.Text = list.getAutoPrevious();
            updateEnabled();
        }

        private void buttonInit_Click(object sender, EventArgs e) {
            list.Initialize(3);
            //初始化
            //index = 3;
            label1.Text = list.getCurrent();
        }

        private void buttonNo_Click(object sender, EventArgs e) {
            //不记得
            label1.Text = list.getAutoNext();
            updateEnabled();
        }

        private void buttonYes_Click(object sender, EventArgs e) {
            //记得
            list.deleteCurrent();
            label1.Text = list.getCurrent();
            if (list.Count == 0)
            {
                MessageBox.Show("Finished");
            }

            updateEnabled();
        }
    }


using System;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections.Generic;

public class ListStringArray: List<string> {
    public ListStringArray() {
        _array = this;
    }

    List<string> _array;
    public int Index {
        get {
            return _index;
        }
    }
    private int _index = -1;

    /// <summary>
    /// 初次加载使用
    /// </summary>
    /// <param name="i">索引从0开始</param>
    public void Initialize(int i) {
        if (i == -1) {
            MessageBox.Show($"索引不能 小于 0,当前索引值为: {i}","错误",MessageBoxButtons.OK,MessageBoxIcon.Error);
            return;
        }
        if (i > _array.Count) {
            MessageBox.Show($"索引不能大于{_array.Count},当前索引值为: {i}","错误",MessageBoxButtons.OK,MessageBoxIcon.Error);
            return;
        }
        _index = i >= 0 && i < _array.Count ? i : -1;
        getCurrent();
    }

    #region Can Enabled

    public bool CanFirstEnabled {
        get {
            return _index != 0 && _array.Count > 0;
        }
    }

    public bool CanLastEnabled {
        get {
            return _index != _array.Count - 1 && _array.Count > 0;
        }
    }

    public bool CanNextEnabled {
        get {
            return _index + 1 < _array.Count && _array.Count > 0;
        }
    }

    public bool CanPrevEnabled {
        get {
            return _index - 1 >= 0 && _array.Count > 0;
        }
    }
    public bool CanAutoEnabled {
        get {
            return _array.Count > 0;
        }
    }
    #endregion

    public string getCurrent() {

        return _index >= 0 && _index < _array.Count ? _array[_index] : null;
    }

    public string getFirst() {
        _index = _array.Count >= 0 ? 0 : -1;
        return getCurrent();
    }

    public string getLast() {
        _index = _array.Count >= 0 ? _array.Count - 1 : -1;
        return getCurrent();
    }

    public string getNext() {
        if (_index == -1) {
            _index = 0;
            return getFirst();
        }
        if (_index + 1 < _array.Count) {
            _index++;
            return getCurrent();
        } else {
            //index = array.Count - 1;
            return getLast();
        }
    }

    public string getPrevious() {
        if (_index == -1) {
            _index = 0;
            return getFirst();
        }
        if (_index - 1 >= 0) {
            _index--;
            return getCurrent();
        } else {
            return getLast();
        }
    }

    public string getAutoNext() {
        if (_index == -1)
            return getFirst();
        if (_index + 1 == _array.Count && _array.Count > 0) {
            _index = 0;
            return getFirst();
        } else {
            return getNext();
        }
    }

    public string getAutoPrevious() {
        if (_index == -1)
            return getFirst();
        if (_index - 1 == 0 && _array.Count > 0) {
            _index = 0;
            return getFirst();
        } else {
            return getPrevious();
        }
    }

    public void deleteCurrent() {
        #region MyRegion
        /*
        第一条
        index = first; indext =0
        删除之后 count>=0
        index=0

        最后一条
        index = last  ;index = count-1
        删除之后 count>=0
        index=count-1;

        中间

        删除之后 count>=0
        tmp = index;
        index =tmp;-
        */
        #endregion
        int x = _index;

        if (_array.Count > 0) {
            if (x == -1) {
                _index = 0;
            }
            if (x == 0) {  //第一条
                _array.RemoveAt(_index);
                _index = 0;
            } else if (_index == _array.Count - 1) {
                _array.RemoveAt(_index);
                _index = _array.Count - 1;
            } else {
                int z = _index;
                _array.RemoveAt(_index);
                _index = z;
            }
            getCurrent();
        }
        //updateEnabled();
    }
}

猜你喜欢

转载自www.cnblogs.com/xe2011/p/12146275.html