我正在尝试选择列表视图中的上一张图像并将其显示在右侧的图片框中。我正在使用 _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.");
}
}
慕斯王
相关分类