Hi
I'm creating
a Form withlistViewthat canbe searched.
Findsome exampleson MSDN.
I builtmy solution.
The followingcode:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Teste_Código5
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private ListView textListView = new ListView();
private TextBox searchBox = new TextBox();
void InitializeTextSearchListView()
{
searchBox.Location = new Point(10, 30);
searchBox.Height = 30;
searchBox.Width = 300;
textListView.Location = new Point(10, 60);
textListView.Width = 300;
textListView.Height = 300;
textListView.AutoArrange = true;
// Set the View to list to use the FindItemWithText method.
listView1.View = View.List;
// Populate the ListViewWithItems
textListView.Items.AddRange(new ListViewItem[]{
new ListViewItem (" 01.01.001 RETIRANDO A VEGETACAO, TRONCOS ATE 5CM DE DIAMETRO E RASPAGEM. "),
new ListViewItem (" 01.01.010 CORTE, RECORTE E REMOCAO DE ARVORES INCL RAIZES DIAM>5<15CM "),
new ListViewItem (" 01.01.011 CORTE, RECORTE E REMOCAO DE ARVORES INCL RAIZES DIAM>15<30CM "),
new ListViewItem (" 01.01.014 CORTE, RECORTE E REMOÇAO DE ARVORES INCL. RAIZES DIAM>30<45CM "),
new ListViewItem (" 01.01.015 CORTE, RECORTE E REMOÇAO DE ARVORES INCL. RAIZES DIAM>45<60CM "),
new ListViewItem (" 01.01.016 CORTE, RECORTE E REMOÇAO DE ARVORES INCL. RAIZES DIAM>60<100CM "),
new ListViewItem (" 01.01.017 CORTE, RECORTE E REMOÇAO DE ARVORES INCL. RAIZES DIAM>100CM "),
new ListViewItem (" 01.01.099 LIMPEZAS DO TERRENO "),
new ListViewItem (" 01.02.001 CORTE E ATERRO DENTRO DA OBRA COM TRANSPORTE INTERNO "),
new ListViewItem (" 01.02.002 CORTE COM RETIRADA POR CAMINHAO NOS PRIMEIROS 100 M "),
new ListViewItem (" 01.02.003 ATERRO COM TRANSPORTE POR CAMINHAO NOS PRIMEIROS 100 M "),
new ListViewItem (" 01.02.004 TRANSPORTE POR CAMINHAO
M3X "),
new ListViewItem (" 01.02.099 MOVIMENTOS DE TERRA MANUAL "),
new ListViewItem (" 01.03.001 CORTE E ATERRO DENTRO DA OBRA COM TRANSPORTE INTERNO "),
new ListViewItem (" 01.03.002 CORTE COM RETIRADA POR CAMINHAO NOS PRIMEIROS 100 M "),
new ListViewItem (" 01.03.004 ATERRO COM TRANSPORTE POR CAMINHAO NOS PRIMEIROS 100 M "),
new ListViewItem (" 01.03.005 TRANSPORTE POR CAMINHAO
M3X "),
new ListViewItem (" 01.03.099 MOVIMENTOS DE TERRA MECANIZADOS "),
new ListViewItem (" 01.04.006 PONTALETADO "),
new ListViewItem (" 01.04.010 ESCORAMENTO DE VALAS CONTINUO ATé 2,00M "),
new ListViewItem (" 01.04.015 ESCORAMENTO DE VALAS DESCONTINUO ATé 2,00M "),
new ListViewItem (" 01.04.099 ESCORAMENTOS DE TERRA "),
new ListViewItem (" 01.05.001 ESCAVACAO MANUAL - PROFUNDIDADE ATE 1.80 M "),
new ListViewItem (" 01.05.002 ESCAVACAO MANUAL - PROFUNDIDADE ALEM DE 1.80 M "), });
// Handle the TextChanged to get the text for our search.
searchBox.TextChanged += new EventHandler(searchBox_TextChanged);
// Add the controls to the form.
this.Controls.Add(listView1);
this.Controls.Add(searchBox);
}
private void searchBox_TextChanged(object sender, EventArgs e)
{
// Call FindItemWithText with the contents of the textbox.
ListViewItem foundItem =
listView1.FindItemWithText(searchBox.Text, false, 0, true);
if (foundItem != null)
{
listView1.TopItem = foundItem;
}
}
}
ButthelistViewis empty
and thesearch boxdoes not appear.
Thanks for any help.