8 Replies - 605 Views - Last Post: 16 May 2012 - 04:44 PM Rate Topic: -----

#1 Quizton  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 49
  • Joined: 18-April 12

searching a large text document issue

Posted 15 May 2012 - 01:57 PM

Hey fellas I am working on a little winformsapp and am a bit stuck

I have a form w a text box to search a text document for matches

and button to activate search

and a richtextbox to display the find..

well finaly got it returning the word or words i typed and found to the rich textbox but wanted to return the

top ten closest matches and the whole line of text it found them on.


 private void richTextBox1_TextChanged(object sender, EventArgs e)
        {
            richTextBox1.Find(textBox1.Text); //<<< so not sure how to make this do as needed!
        }
        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }
    }
}


any suggestions would be a large help thanks!! ;)

Is This A Good Question/Topic? 0
  • +

Replies To: searching a large text document issue

#2 Ryano121  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1362
  • View blog
  • Posts: 3,002
  • Joined: 30-January 11

Re: searching a large text document issue

Posted 15 May 2012 - 02:00 PM

I don't really understand whats going on. Do you have your text loaded into your textbox and then want to display matches in the richtextbox - in which case the .Find method would be of no real use.

Can you explain your question a little better?
Was This Post Helpful? 0
  • +
  • -

#3 Quizton  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 49
  • Joined: 18-April 12

Re: searching a large text document issue

Posted 15 May 2012 - 02:13 PM

yea, I have a large text document well not to large but it is written line for line

when i type into my textbox search for example Tree or trees, it then reads the document and if tree exists in the document it then places the found word tree in the richtextbox.

Im not quite sure how to instead of returning the word only.. to return the whole line that word tree is on to the rich textbox.

thanks for the help
Was This Post Helpful? 0
  • +
  • -

#4 Momerath  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1010
  • View blog
  • Posts: 2,444
  • Joined: 04-October 09

Re: searching a large text document issue

Posted 15 May 2012 - 04:18 PM

There are a few ways to do it, but if you are using Find you can take the index it returns and move forward and back through the text until you find the line breaks. You then have the start and end of that line. Continue the Find searching using the end point of the previous string as the starting point for the new search. Do this however many times you want to return lines.
Was This Post Helpful? 0
  • +
  • -

#5 Quizton  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 49
  • Joined: 18-April 12

Re: searching a large text document issue

Posted 15 May 2012 - 04:44 PM

I was trying a few diff ways to bad it wasnt as simple as

richTextBox1.Find(textBox1.Text + textline);

This post has been edited by Quizton: 15 May 2012 - 04:45 PM

Was This Post Helpful? 0
  • +
  • -

#6 bluesuus  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 2
  • View blog
  • Posts: 65
  • Joined: 26-December 05

Re: searching a large text document issue

Posted 15 May 2012 - 07:01 PM

Your problem can be solved in many ways, but i choose to talk about two solutions in which one has already been mentioned above.
The first solution i will suggest is, if memory is not an issue, then you can use the ReadAllLines method from the File class to read all the contents of the file into an array.
 string lines[] = System.IO.File.ReadAllLines(@"location of file"); 


You can then search for substrings on each line to find your search term.Using this approach will enable you to find the line number and closest matches but it is a lazy and inefficient.
Was This Post Helpful? 0
  • +
  • -

#7 Quizton  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 49
  • Joined: 18-April 12

Re: searching a large text document issue

Posted 15 May 2012 - 09:46 PM

hmm I think In a way it is lazy, I could put a bit more effort in it

I am using

private void searchFile(String file, String searchText)
        {
            System.IO.StreamReader reader = new System.IO.StreamReader(file);   
            String text = reader.ReadToEnd();
            if (System.Text.RegularExpressions.Regex.IsMatch(text, searchText))
            {
               richTextBox1.SelectedText = searchText;
            }
            else
            {
                MessageBox.Show("Sorry, but " + searchText + " could not be found", "No Results");
            }


this for reading it. In this particular case the textdocument will be ok but If I cant get it to do how id like then ill probably use mysql.


Also I am wondering if your mthod would be better or can i get it done with this way lazily ;)

thanks again
Was This Post Helpful? 0
  • +
  • -

#8 Quizton  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 49
  • Joined: 18-April 12

Re: searching a large text document issue

Posted 16 May 2012 - 09:49 AM

Well I found something kinda like what I wanted to acomplish here.>> retrieve line



Ok but I also think I might recieve a better explanation by posting my whole formcode then maybe I can finish this and on to next project. On a side note I am confused by different methods sometimes so If you could reply with that in mind It would be great thanks again
and here is my form code

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;


namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        } 
        private void button1_Click(object sender, EventArgs e)
        {
            searchFile("basicHistory.txt", textBox1.Text);
        }
        private void searchFile(String file, String searchText)
        {
            System.IO.StreamReader reader = new System.IO.StreamReader(file);   
            String text = reader.ReadToEnd();
            if (System.Text.RegularExpressions.Regex.IsMatch(text, searchText))
            {
               richTextBox1.SelectedText = searchText;
            }
            else
            {
                MessageBox.Show("Sorry, but " + searchText + " could not be found", "No Results");
            }
        }
        private void richTextBox1_TextChanged(object sender, EventArgs e)
        {
            richTextBox1.Find(textBox1.Text);
        }
        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }
    }
}



Was This Post Helpful? 0
  • +
  • -

#9 bluesuus  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 2
  • View blog
  • Posts: 65
  • Joined: 26-December 05

Re: searching a large text document issue

Posted 16 May 2012 - 04:44 PM

@quizton,at first i mentoned that the method of readAllLines above was inefficieng actually it is not that bad after running some tests.I recommend you still use that method to read all the lines into the array, then call your searchFile method like this.
 searchFile(lines, searchText); 


Note. you are no longer passing the file name in the function but rather the lines array.Remember, this lines array was gotten by calling the readAllLines method on the file.

The implementation of your searchFile function should ofcourse be modified a little as noted below.
1) remove the line that reads from the file using the StreamReader class.You no longer need it as you have previously read the file.
2) you need to surround your regular expression statements in a while loop that will go through the lines array and append evey match it finds on a line to the richtextbox.for example, To search on line[0], you call the regex isMatch method like this
 ressions.Regex.IsMatch(line[0],searchTerm);

3)keep a bool variable so that after the loop finishes execution you can decipher whether atleast a match has been found or not, in order to show appropriate message.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1