Update image items in a listview

  • Thread starter Thread starter SlenderMan564
  • Start date Start date
S

SlenderMan564

Guest
Hello,this error occurs when I try to update the listview collection to have additional image items.

'System.ArgumentOutOfRangeException' Error Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index

So when I add a new image or images into the listview the others disapper and the new collection replaces it.

The error is on this line in a for loop:

lstImages.Items.Add(arryFilePaths,i);
It occurs in method: loadImageList()

Just to clarify I know about the .AddRange method I could use to simplify my for loop in loadImageList() but it won't except my list arryFilePaths as it's not a list view. I can live with the for loop, I just want to successfully update the collection. Please tell me what I'm doing wrong. Thank you.


public partial class Form1 : Form
{
string _big_fileName;
int _counter = 0;
int _imageIndex;

#region Initializers
public Form1()
{
InitializeComponent();
}

//Displays larger instance of selected image in picture box.
private void lstImages_SelectedIndexChanged(object sender, EventArgs e)
{
//FOR i is less than the first image.
for (int i = 0; i < lstImages.SelectedItems.Count; i++)
{
//GET filename from listview and store in index.
_big_fileName = lstImages.SelectedItems.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 = lstImages.SelectedIndices[0];
loadImageMetaData();
}
}

private void mnuOpen_Click(object sender, EventArgs e)
{
loadImageList();
_imageIndex = 0;
}

string _big_fileName;
int _counter = 0;
int _imageIndex;

#region Initializers
public Form1()
{
InitializeComponent();
}

//Displays larger instance of selected image in picture box.
private void lstImages_SelectedIndexChanged(object sender, EventArgs e)
{
//FOR i is less than the first image.
for (int i = 0; i < lstImages.SelectedItems.Count; i++)
{
//GET filename from listview and store in index.
_big_fileName = lstImages.SelectedItems.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 = lstImages.SelectedIndices[0];
loadImageMetaData();
}
}

private void mnuOpen_Click(object sender, EventArgs e)
{
loadImageList();
_imageIndex = 0;
}

private void loadImageList()
{
imageList1.Images.Clear();
lstImages.Clear();

oFD1.InitialDirectory = "C:\\";
oFD1.Title = "Open an Image File";
oFD1.Filter = "JPEGS|*.jpg|GIFS|*.gif|PNGS|*.png|BMPS|*.bmp";

//Open Dialog Box.
var oldResults = oFD1.ShowDialog();

if (oldResults == DialogResult.Cancel)
{
return;
}

try
{
//GET amount of filenames.
int num_of_files = oFD1.FileNames.Length;
//STORE filenames in string array.
List<string> arryFilePaths = new List<string>(num_of_files);

foreach (string single_file in oFD1.FileNames)
{
arryFilePaths.Add(single_file);
imageList1.Images.Add(Image.FromFile(single_file));
_counter++;
}
//BIND image list to listview.
lstImages.LargeImageList = imageList1;

for (int i = 0; i < _counter; i++)
{
//DISPLAY filename and image from image index param.
lstImages.Items.Add(arryFilePaths,i);
}
}

catch (Exception ex)
{
Debug.WriteLine("Error " + ex.Message);
}
}

Continue reading...
 

Similar threads

Back
Top