Hi

I've have a Windows form application, that provieds users to add/see and search new items, items are saved into a text file with a help of streamreader/streamwriter.

My application is almost finished, but i dont know how to make a search system.


This is my application:

http://www.youtube.com/watch?v=Idgw52iXLRk&feature=plcp&context=C352918eUDOEgsToPDskIF-q5mZ0uP5UIRlBTolHbg

Code:

This is the code for the display the data that is saved into .txt file

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Collections;


namespace Lekarna
{
    public partial class Izpis_zravil : Form
    {
        int vrstice;

        public Izpis_zravil()
        {
            InitializeComponent();

            Vel();

            string[] polje = new string[100];

            ListViewItem item1 = new ListViewItem();

            string filename = "zdravila.txt";
            if (File.Exists(filename))
            {
                string[] lines = File.ReadAllLines(filename);
                for (int j = 0; j < vrstice; j++)
                {
                    polje[j] = lines[j];

                    if (j % 5 == 0)
                    {
                        item1 = new ListViewItem(polje[j]);
                        listView1.Items.AddRange(new ListViewItem[] { item1 });
                    }
                    else
                        item1.SubItems.Add(polje[j]);

                }
            }
        }

        public string Vel()
        {
            StreamReader beri = File.OpenText("zdravila.txt");
            string input = null;
            while ((input = beri.ReadLine()) != null)
            {
                vrstice++;
            }
            beri.Close();
            this.Invalidate();
            return "";
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void listView1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {

        }

        private void button2_Click_1(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}

This is the code for saving the items

private void button1_Click_1(object sender, EventArgs e)
        {
            
            FileInfo fi = new FileInfo("zdravila.txt");
            using (StreamWriter sw = fi.AppendText())
            {
                sw.WriteLine(textBox1.Text);
                sw.WriteLine(textBox2.Text);
                sw.WriteLine(textBox5.Text);
                sw.WriteLine(textBox3.Text);
                sw.WriteLine(textBox4.Text);
            }
            MessageBox.Show("Zdravilo shranjeno!");
        
        }

Sorry for my bad english.Thanks for helping :)

Dani AI

Generated

For : the simplest, robust way to add search is to centralize file reading into one routine that rebuilds the ListView and accepts an optional filter string. The routine below assumes the same five-lines-per-record layout used in the existing save code, does a case-insensitive partial match across all fields, skips incomplete trailing records, and uses BeginUpdate/EndUpdate to avoid flicker.

private void PopulateListView(string filter = null)
{
    const string filename = "zdravila.txt";
    if (!File.Exists(filename)) return;

    listView1.BeginUpdate();
    listView1.Items.Clear();

    string[] lines = File.ReadAllLines(filename);
    for (int i = 0; i + 4 < lines.Length; i += 5)
    {
        string[] fields = new string[5];
        for (int k = 0; k < 5; k++) fields[k] = lines[i + k].Trim();

        bool match = string.IsNullOrEmpty(filter) ||
                     fields.Any(f => f.IndexOf(filter, StringComparison.OrdinalIgnoreCase) >= 0);

        if (match)
        {
            var lvi = new ListViewItem(fields[0]);
            for (int k = 1; k < fields.Length; k++) lvi.SubItems.Add(fields[k]);
            listView1.Items.Add(lvi);
        }
    }

    listView1.EndUpdate();
}

private void btnSearch_Click(object sender, EventArgs e)
{
    PopulateListView(textBoxSearch.Text.Trim());
}

Notes and quick troubleshooting:

  • Call PopulateListView() on form load and after the append/save operation so the UI stays in sync.
  • If the file can be edited manually, detect and log incomplete records (lines.Length % 5 != 0).
  • For exact-word matches use a Regex with word boundaries; for very large data move to CSV/SQLite for reliable searching and indexing.
  • If multiple parts of the app write the file concurrently, protect writes with a lock or use a transactional store.

Anybody? :S

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.