c#实现上一页和下一页功能

public partial class Form1 : Form
    {
        private string[] fileLists = null;//定义FileList集合用来存储当前文件中的所有文件
        private string fileName = "";//用户弹出对话框后选择的图片名称
        public Form1()
        {
            InitializeComponent();
        }

      
        /// <summary>
        /// 从本地磁盘中获取图片资源
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BtnOpenImg_Click(object sender, EventArgs e)
        {
            OpenFileDialog openDlg = new OpenFileDialog();
            openDlg.Filter = "图片资源(*.png)|*.png|图片资源(*.jpg)|*.jpg|所有文件(*.*)|*.*";
            openDlg.ShowDialog();
            fileName = openDlg.FileName;
            //MessageBox.Show("fileName=" + fileName);

            string path = Path.GetDirectoryName(fileName);
            //MessageBox.Show(path);
            fileLists = Directory.GetFiles(path);//获取该路径下的所有文件

            this.pictureBox1.Image = Image.FromFile(@fileName);
        }


        private void Form1_Load(object sender, EventArgs e)
        {
            this.pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;//设置图片的显示模式

        }

        int index = 0;
        bool isHasIndex = false;
        private void BtnLastImage_Click(object sender, EventArgs e)
        {
            if (fileLists == null)
            {
                MessageBox.Show("请选择一张图片");
            }
            else
            {
                if (!isHasIndex)
                {
                    for (int i = 0; i < fileLists.Length; i++)
                    {
                        if (fileName == fileLists[i])
                        {
                            index = i;//给索引赋值
                            // MessageBox.Show("index=" + index);
                            isHasIndex = true;
                        }
                    }
                }

                if (index == 1)
                {
                    index = fileLists.Length;
                }
                

                this.pictureBox1.Image = Image.FromFile(fileLists[index-1]);
                index--;
            }
        }

        private void BtnNextImage_Click(object sender, EventArgs e)
        {
            if (fileLists == null)
            {
                MessageBox.Show("请选择一张图片");
            }
            else
            {
                if (!isHasIndex)
                {
                    for (int i = 0; i < fileLists.Length; i++)
                    {
                        if (fileName == fileLists[i])
                        {
                            index = i;//给索引赋值
                            // MessageBox.Show("index=" + index);
                            isHasIndex = true;
                        }
                    }
                }

                if (index == fileLists.Length-1)
                {
                    index = 1;
                }


                this.pictureBox1.Image = Image.FromFile(fileLists[index+1]);
                index++;
            }
        }
    }

猜你喜欢

转载自www.cnblogs.com/ww7018/p/9226303.html