码迷,mamicode.com
首页 > Windows程序 > 详细

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

时间:2018-06-25 21:46:36      阅读:1057      评论:0      收藏:0      [点我收藏+]

标签:.sh   lis   fromfile   ast   led   ESS   pen   his   rom   

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++;
            }
        }
    }

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

标签:.sh   lis   fromfile   ast   led   ESS   pen   his   rom   

原文地址:https://www.cnblogs.com/ww7018/p/9226303.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!