1 回答
TA贡献1864条经验 获得超2个赞
首先,您没有在任何地方设置 _imageIndex,除了在 btnPrevious_Click 中将其设置为 0。
您需要在 SelectedIndexChanged 方法中将其设置为选定索引,如下所示(注意添加到您之前代码中的最后一行):
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);
_imageIndex = listView1.SelectedIndices[0];
}
}
接下来,在 btnPreviousClick 中,您需要进行一些更改:
private void btnPrevious_Click(object sender, EventArgs e)
{
if (pictureBox1.Image != null)
{
//IF Image is less than Image list size.
if (_imageIndex >= 0 && _imageIndex < imageList1.Images.Count)
{
//De-select current item
listView1.Items[_imageIndex].Selected = false;
//GO to previous entry.
_imageIndex--;
//Select new item
if(_imageIndex>=0){
listView1.Items[_imageIndex].Selected = true;
listView1.Select();
}
}
}
else
{
MessageBox.Show("No image.");
}
}
有必要在 listView1 上调用 Select() 方法。最大的问题是你没有设置 _imageIndex 。
- 1 回答
- 0 关注
- 234 浏览
添加回答
举报