猿问

C# 返回到列表视图中的上一个图像并将其显示在图片框中

我正在尝试选择列表视图中的上一张图像并将其显示在右侧的图片框中。我正在使用 _imageIndex 来跟踪当前图片框中的内容,如果它小于图像列表长度,则我在列表视图中选择图像并使用 _imageIndex 计数器访问图像。


但这就像根本没有获得图像索引。它只是继续在 0 处读取它并将其递减为负 1 并在我再次单击 btnPrevious 时返回错误。


System.ArgumentOutOfRangeException: 'InvalidArgument=Value of '-1' is not valid for 'index'.

Parameter name: index'

我知道我在 mnuOpen_Click 事件中将其设置为 _imageIndex 为 0。除此之外我不知道如何解决这个问题。为清楚起见,这里是GUI的图像。请问有什么想法吗?


public partial class Form1 : Form

{

    string _big_fileName;

    int _counter = 0;

    int _imageIndex;


    public Form1()

    {            

        InitializeComponent();

    }


    //Displays larger instance of selected image in picture box.

    private void listView1_SelectedIndexChanged(object sender, EventArgs e)

    {

        //FOR i is less than the first image.

        for (int i = 0; i < listView1.SelectedItems.Count;i++)

        {                

            //GET filename from listview and store in index.

            _big_fileName = listView1.SelectedItems[i].Text;

            //Create larger instance of image.

            pictureBox1.Image = Image.FromFile(_big_fileName);

            //Fill panel to the width and height of picture box.

            panel1.AutoScrollMinSize = new Size(pictureBox1.Image.Width, pictureBox1.Image.Height);

        }

    }


    private void mnuOpen_Click(object sender, EventArgs e)

    {            

        loadImageList();

        _imageIndex = 0;

    }


    private void btnPrevious_Click(object sender, EventArgs e)

    {

        if (pictureBox1.Image != null)

        {                

            //IF Image is less than Image list size.

            if (_imageIndex < imageList1.Images.Count)

            {

                //SELECT  listview items with counter.

                listView1.Items[_imageIndex].Selected = true;

                //GO to previous entry.

                _imageIndex--;

            }

        }


        else

        {

            MessageBox.Show("No image.");

        }

    }


繁星点点滴滴
浏览 235回答 1
1回答

慕斯王

首先,您没有在任何地方设置 _imageIndex,除了在 btnPrevious_Click 中将其设置为 0。您需要在 SelectedIndexChanged 方法中将其设置为选定索引,如下所示(注意添加到您之前代码中的最后一行):private void listView1_SelectedIndexChanged(object sender, EventArgs e)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; //FOR i is less than the first image.&nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i < listView1.SelectedItems.Count; i++)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //GET filename from listview and store in index.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; _big_fileName = listView1.SelectedItems[i].Text;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //Create larger instance of image.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pictureBox1.Image = Image.FromFile(_big_fileName);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //Fill panel to the width and height of picture box.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; panel1.AutoScrollMinSize = new Size(pictureBox1.Image.Width, pictureBox1.Image.Height);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; _imageIndex = listView1.SelectedIndices[0];&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }接下来,在 btnPreviousClick 中,您需要进行一些更改:private void btnPrevious_Click(object sender, EventArgs e)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; if (pictureBox1.Image != null)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //IF Image is less than Image list size.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (_imageIndex >= 0 && _imageIndex < imageList1.Images.Count)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //De-select current item&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; listView1.Items[_imageIndex].Selected = false;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //GO to previous entry.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; _imageIndex--;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //Select new item&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(_imageIndex>=0){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;listView1.Items[_imageIndex].Selected = true;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;listView1.Select();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; else&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; MessageBox.Show("No image.");&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }有必要在 listView1 上调用 Select() 方法。最大的问题是你没有设置 _imageIndex 。
随时随地看视频慕课网APP
我要回答