将项目附加到列表视图的末尾 [英] Append items to the end of a listview

查看:90
本文介绍了将项目附加到列表视图的末尾的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

IndexOutOfBoundsException索引超出了数组的范围。



当我尝试将另一个项目附加到列表视图时会发生这种情况。似乎当我们添加一个新项时,数组的大小会重置为我们附加到它的任何内容。因此,计数器的大小会变得比数组大。请告诉我我做错了什么!错误发生在

 temp [_counter] = single_file; 

方法

 loadImageList()



这是我的代码。



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

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

// 在图片框中显示所选图像的更大实例。
private void lstImages_SelectedIndexChanged( object sender,EventArgs e)
{
// FOR i小于第一张图片。
for int i = 0 ; i < lstImages.SelectedItems.Count; i ++)
{
// 从listview获取文件名并存储在索引中。
_big_fileName = lstImages.SelectedItems [i] .Text;
// 创建更大的图像实例。
pictureBox1.Image = Image。 FROMFILE(_big_fileName);
// 将面板填入图片框的宽度和高度。
panel1 .AutoScrollMinSize = new 大小(pictureBox1.Image.Width,pictureBox1.Image.Height);
_imageIndex = lstImages.SelectedIndices [ 0 ];
loadImageMetaData();
}
}

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

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

oFD1.InitialDirectory = C:\\;
oFD1.Title = 打开图像文件;
oFD1.Filter = JPEGS | * .jpg | GIFS | * .gif | PNGS | * .png | BMPS | * .BMP;

// 打开对话框。
var oldResults = oFD1.ShowDialog();

if (oldResults == DialogResult.Cancel)
{
返回;
}

尝试
{
// 获取文件名数量。
int num_of_files = oFD1.FileNames.Length;
// 为解决错误而创建的临时数组。
string [] temp = new string [num_of_files];
// 以字符串数组存储文件名。
string [] arryFilePaths = new string [num_of_files];


// 文件中的FOREACH文件名。
foreach string single_file in oFD1 .FileNames)
{
// 如果我想要显示更多图像。
if (_counter > = oFD1.FileNames.Length)
{
// 将这些图像追加并渲染到图像列表的末尾。
temp [_counter] = single_file;
imageList1.Images.Add(Image.FromFile(single_file));
_counter ++;
break ;
}
// 使用_counter查找文件的ACCESS数组。
arryFilePaths [_counter] = single_file;
// 在内存中创建图像并将图像添加到图像列表。
imageList1 .Images.Add(Image.FromFile(single_file));
_counter ++;


}
// BIND图像列表到列表视图。
lstImages.LargeImageList = imageList1;


for int i = 0 ; i < _counter; i ++)
{
// 从图像索引参数中显示文件名和图像。
lstImages.Items.Add(arryFilePaths [i],i);
}
}

catch (Exception ex)
{
Debug.WriteLine( 错误 + ex.Message);
}
}

私有 void lstImages_DoubleClick( object sender,EventArgs e)
{
lstImages = abb.abbListview.Update(lstImages, 表格标题,lstImages.SelectedIndices [ 0 ]);
}
}
}





我尝试了什么:



第1步



我试过换这条线

串[] arryFilePaths =新的字符串[num_of_files];



串[] arryFilePaths =新的字符串[num_of_files + _counter];



我在这里随意编码,这只是覆盖了阵列中的任何内容并处理了之前的项目。



第2步

我用过这个。它应该在我的

 lstImages_DoubleClick 

事件中。

更新Listview项目 [ ^ ]



第3步



写的伪代码。

=====================

START

GET柜台

获得ArryTotalFiles。

DECLARE newArry。



IF(ArryTotalFiles不为空)。 />
存储在tempArray内的ArrayTotalFiles和Count。

END IF

ELSE IF(tempArry不为空)

CLEAN ArrTotalFiles 。

在StringFileArray中存储ArryTotalFiles和Count。

END IF ELSE

END。

==== =================

是的,我知道它看起来与我在代码中编写的内容完全不同。 :(

解决方案
的最简单的解决方案是不使用数组:使用代替的列表:

列表与LT;字符串>温度=新列表与LT ; string>);

List< string> arryFilePaths = new List< string>);

然后你可以这样做:

 temp.Add(single_file); 
...
arryFilePaths.Add(single_file);

系统会在幕后为您处理。



顺便说一句,ListView.Items集合也有一个AddRange方法,所以这段代码:

  for  int  i =  0 ; i < ;  _counter; i ++)
{
// DISPLAY文件名和图片中的图片索引参数。
lstImages.Items.Add(arryFilePaths [i],i);
}

可以在一个语句中完成:

 lstImages.Items.AddRange(arryFilePaths); 


IndexOutOfBoundsException "Index was outside the bounds of the array."

This occurs when I try to append another item to the listview. It seems that when we add a new item, the array’s size gets reset to whatever we appended to it. So the size of the counter then becomes bigger than the array. Please tell me what I've done wrong! The error is on this line

temp[_counter] = single_file;

in method

loadImageList()


Here is my code.

    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[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 = 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;
                //Temp array i created to solve the error.
                string[] temp = new string[num_of_files];
                //Store filenames in string array.
                string[] arryFilePaths = new string[num_of_files];


                //FOREACH filename in the file.
                foreach (string single_file in oFD1.FileNames)
                {
                    //IF there are more images I want to display.
                    if (_counter >= oFD1.FileNames.Length)
                    {
                        //Append and render those images to the end of the image list.
                        temp[_counter] = single_file;
                        imageList1.Images.Add(Image.FromFile(single_file));
                        _counter++;
                        break;
                    }
                    //ACCESS array using _counter to find file.
                    arryFilePaths[_counter] = single_file;
                        //CREATE image in memory and add image to image list.
                        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], i);
                }
            }

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

        private void lstImages_DoubleClick(object sender, EventArgs e)
        {
            lstImages = abb.abbListview.Update(lstImages, "Form Header", lstImages.SelectedIndices[0]);
        }
    }
}



What I have tried:

Step 1

I tried changing this line
string[] arryFilePaths = new string[num_of_files];
to
string[] arryFilePaths = new string[num_of_files + _counter];

I was coding hopelessly at random here, this just overrides whatever is in the array and disposes of the previous items.

Step 2
I used this. It should be in my

lstImages_DoubleClick

event.
Update Listview Items[^]

Step 3

Wrote pseudo code.
=====================
START
GET Counter
GET ArryTotalFiles.
DECLARE newArry.

IF (ArryTotalFiles isn't empty).
STORE inside tempArray ArrayTotalFiles and Count.
END IF
ELSE IF (tempArry isn't empty)
CLEAN ArrTotalFiles.
STORE in StringFileArray the ArryTotalFiles and Count.
END IF ELSE
END.
=====================
Yeah I know it looks nothing like what I wrote in my code. :(

解决方案

The simplest solution is not to use an array: use a List instead:
List<string> temp = new List<string>);
List<string> arryFilePaths = new List<string>);
You can then do this:

   temp.Add(single_file);
...
arryFilePaths.Add(single_file);

And teh system will handle it all for you behind the scenes.

By the way, The ListView.Items collection also has an AddRange method, so this code:

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

Can be done in a single statement:

lstImages.Items.AddRange(arryFilePaths);


这篇关于将项目附加到列表视图的末尾的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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