如何使用上一个,下一个按钮在Picturebox中显示图像? [英] how to display the image in Picturebox using Previous, Next buttons?

查看:83
本文介绍了如何使用上一个,下一个按钮在Picturebox中显示图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在c#中有windows应用程序。它包含一个图片框和两个按钮(按钮名称为Next,Previous)。当我点击按钮时,我需要在图片框中显示10个图像。

例如,我点击上一个按钮它将显示图片框中的上一个图像,然后我点击它显示下一个按钮的下一个按钮图片。



i尝试过以下编码,

I have windows application in c#. It contain one Picture box and two buttons(button names are Next, Previous). i need to display 10 images in picture box when i clicked the buttons.
for eg, i clicked the previous button it will display previous image in picture box, and i click the next button it display the next image.

i have tried the following codings,

private void Next_Click(object sender, EventArgs e)
       {
          int  i = 0;
           string filePath = "C:/Users/Pictures/2013-06-10";
           string[] files = Directory.GetFiles(filePath);
           i++;
           if (i >= 11)
           {
               pictureBox1.Image = null;
           }
           else
           {
               pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
               pictureBox1.Image = Image.FromFile(files[i]);
           }
       }

       private void Previous_Click(object sender, EventArgs e)
       {
           int j = 10;
           string filePath = "C:/Users/Pictures/2013-06-10";
           string[] files = Directory.GetFiles(filePath);
           j--;
           if (j <= 0 || j == 10)
           {
               pictureBox1.Image = null;
           }
           else
           {
               pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
               pictureBox1.Image = Image.FromFile(files[j]);
           }
       }

推荐答案

这里的问题是你创建了一个索引变量每按一次按钮我。它会在每次按下单击时以相同的值初始化。你需要的是一个成员变量,它属于包含方法的对象 Next_Click() Previous_Click 。像这样的事情
The problem here is that you create an index variable i on every button click. It gets initialized on every button click with the very same value. What you need is a member variable that belongs to the object that contains the methods Next_Click() and Previous_Click. Something like this
System.Windows.Form Form1
{
    private int _pictureIndex = 0;

    // Next line can be set in Designer. No need to code it.
    //pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
    
    private void Next_Click(object sender, EventArgs e)
    {
        string filePath = "C:/Users/Pictures/2013-06-10";
        string[] files = Directory.GetFiles(filePath);
        _pictureIndex++;
        if (_pictureIndex > files.Length)
        {
            _pictureIndex = 0;
        }
        pictureBox1.Image = Image.FromFile(files[_pictureIndex]);
    }
}

当然,硬编码的文件路径仍然很难看。但我想,这只是为了演示目的。

Of course, the hard-coded file path is still ugly. But I guess, that's for demonstration purposes only.


尝试添加一个jquery插件,我认为这是一个简单的方法
try to add one jquery plugin, i think its an easy way


这篇关于如何使用上一个,下一个按钮在Picturebox中显示图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆