Click here to Skip to main content
15,889,931 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
The problem occurs in text_changed event, when the user types a character, the resultant image is received with only text, while the image is blank. After finishing the search, all images become blank while the text restores back. Here , the searchbox is a textbox.

using MetroFramework.Forms;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Windows.Forms;

namespace DocumentViewer
{
    public partial class ImageForm : MetroForm
    {
        private string[] files = new string[] { };
        private TreeForm dialog;
        private List<string> filePath = new List<string>();
        private ListViewItem lstItem;
        private int index;

        public ImageForm()
        {
            InitializeComponent();
        }

        private void browseButton_Click(object sender, System.EventArgs e)
        {
            dialog = new TreeForm();

            dialog.FileFilter = "IMG";
            if (dialog.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    listView1.Cursor = Cursors.WaitCursor;
                    clear();
                    searchBox.Enabled = true;
                    files = dialog.SelectedPath;

                    foreach (string temp in files)
                    {
                        Image image = Image.FromFile(temp);
                        Image thumbNail = image.GetThumbnailImage(100, 100,      
                                                           null, new IntPtr());
                        filePath.Add(Path.GetFileNameWithoutExtension(temp));
                        imageList1.Images.Add(thumbNail);
                        fileLabel.Text = "Files : " + imageList1.Images.Count;
                    }
                    loadCaption();
                    listView1.LargeImageList = imageList1;
                    
                    listView1.Cursor = Cursors.Hand;
                }
                catch (Exception error)
                {
                    MessageBox.Show(error.Message);
                }
            }
        }

        private void loadCaption()
        {
            for (int j = 0; j < imageList1.Images.Count; j++)
            {
                lstItem = new ListViewItem();
                lstItem.ImageIndex = j;
                lstItem.Text = Path.GetFileNameWithoutExtension(filePath[j]);
                listView1.Items.Add(lstItem);
            }
        }
        private void clear()
        {
            listView1.Clear();
            lstItem = null;
            imageList1.Images.Clear();
            fileLabel.ResetText();
            pictureBox1.Image = null;
            searchBox.ResetText();
            filePath.Clear();
        }

        private void listView1_ItemChecked(object sender, ItemCheckedEventArgs e)
        {
            try
            {
                if (e.Item.Checked == true)
                {
                    index = Array.FindIndex(files, files => 
                                            files.Contains(e.Item.Text));
                    pictureBox1.Image = Image.FromFile(files[index]);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void searchBox_TextChanged(object sender, EventArgs e)
        {
            listView1.Items.Clear();

            foreach (string str in files)
            {
                    if (str.ToUpper().Contains(searchBox.Text.ToUpper()) == true)
                    {

                        listView1.Items.Add(Path.GetFileName(str));
                    }
            }
        }

        private void toolStripMenuItem1_Click(object sender, EventArgs e)
        {
            listView1.View = View.LargeIcon;
        }

        private void toolStripMenuItem2_Click(object sender, EventArgs e)
        {
            listView1.View = View.SmallIcon;
        }
    }
}


What I have tried:

 private void searchBox_TextChanged(object sender, EventArgs e)
       {
           listView1.Items.Clear();

           foreach (string str in files)
           {
                   if (str.ToUpper().Contains(searchBox.Text.ToUpper()) == true)
                   {
// This assigns only the last image index to the searched image as well as rest of the images
                       listView1.Items.Add(Path.GetFileName(str),
                                           lstItem.ImageIndex);
                   }
           }
       }
Posted
Updated 23-Jun-18 21:59pm

1 solution

private void searchBox_TextChanged(object sender, EventArgs e)
{
    listView1.Items.Clear();
    foreach (string str in files)
    {
        if (str.ToUpper().Contains(searchBox.Text.ToUpper()) == true)
        {
            int x = Array.FindIndex(files, files => files.Contains(str));
            listView1.Items.Add(Path.GetFileName(str), x);
        }
    }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900