本文介绍了如何通过searchbox在listview中搜索图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
问题出现在text_changed事件中,当用户键入字符时,结果图像仅接收文本,而图像为空白。完成搜索后,所有图像都会变为空白,同时文本将恢复。在这里,搜索框是一个文本框。
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);
}
}
}
推荐答案
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);
}
}
}
这篇关于如何通过searchbox在listview中搜索图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!