1.11M Members

Search Textbox, Listbox, RichTextbox, and files

 
0
 

Good Day everybody,
I am wanting to make a program in VB.NET that has many files.doc in a specific folder.
This program has on top a textbox(txtSearch) with a button(btnSearch)
under that this has two listbox(lstOption, lstResult), that are floating a left,
one richtextbox(rtbDoc) that is floating a right.
I want that when the user writes in txtSearch and click on button then the application will read all files, and find for this string.
When it complete:
the first listbox's item are:
All words matches,
Some words matches
initial letter of words matches
Then when the user click on one of this items, then will load all lines where were finds txtsearch.text.
Now when the user clicks on one of this items, then will load the document in the rtbDoc, and if possible thant the rtbDoc(RichtextBox) scroll position on the searched line.
Thank you so much, Have a nice day!

 
0
 

if you have the source in C#, or in C++, I will re-design the program in the that languages... Thanks you a lot

 
0
 

See if this helps to get you started.
1.TextBox,1.Button,1.ListBox,1.ProgressBar

Imports System.IO
Public Class Form1
    Private myCoolFilesFolder As String = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) & "\vb.samples\"
    Private sT As String = Nothing

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        searchFilesForCoolWords(myCoolFilesFolder, TextBox1.Text, ListBox1)
    End Sub
    Private Sub searchFilesForCoolWords(ByVal selCoolFolder As String, ByVal selSearchString As String, ByVal selListBoxForResults As ListBox)
        If Directory.Exists(selCoolFolder) Then
            With ProgressBar1
                .Maximum = Directory.GetFiles(selCoolFolder).Length
                .Value = 0
            End With
            selListBoxForResults.Items.Clear() '// .Clear for new.input.
            For Each itm As String In Directory.GetFiles(selCoolFolder, "*", SearchOption.TopDirectoryOnly)
                sT = File.ReadAllText(itm) '// Read File.
                If sT.Contains(selSearchString) Then
                    ' selListBoxForResults.Items.Add(itm) '// Full.Path of File.
                    selListBoxForResults.Items.Add(Path.GetFileNameWithoutExtension(itm))
                End If
                ProgressBar1.Value += 1
            Next
        End If
    End Sub
End Class

I added a ProgressBar, just because it's "kewwl":D; though you can always comment out the ProgressBar code.lines.

 
0
 

Thank you so much, nice to meet you, i am very thankful to you, thanks for the help you give me,
and now I have just created the source for loading the doc in the Richtextbox.
We have to add a textbox(txtHidden) and edit his property "Visibility = "False".
now duble click on listbox1
listbox1.indexchanged
txtHidden.text = listbox1.selecteditem
Add on ther button named btnConfirm, and his text may be Confirm or Open
and his source:
Dim DocumentDir As String = My.Computer.FileSystem.SpecialDirectories.Desktop + "\vb.samples\"
Dim DOCUMENTobjReader As New System.IO.StreamReader(DocumentDir)
richtextbox1.Text = DOCUMENTobjReader.ReadToEnd
DOCUMENTobjReader.Close()

and this worked fine!! (Thank you so much, i am to much happy...)
now we have to add that the richtextbox loads the document at the line position, it autoscrolls to the line... how we can do this? i will search on google

 
0
 

To make things easier, I changed line.20 .From: selListBoxForResults.Items.Add(Path.GetFileNameWithoutExtension(itm)) .To: selListBoxForResults.Items.Add(Path.[B]GetFileName[/B](itm)) .

This will add the .FileName "with" the .FileExtension to your ListBox when loading the search.result Files.

Regarding the ListBox.SelectedIndex and loading the .File with the line selected, see if this helps.:)

Imports System.IO
Public Class Form1
    Private myCoolFilesFolder As String = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) & "\vb.samples\"
    Private sT As String = Nothing

    Private Sub _Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        searchFilesForCoolWords(myCoolFilesFolder, TextBox1.Text, ListBox1)
    End Sub

    Private Sub _ListBox1_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
        With ListBox1
            If Not .SelectedIndex = -1 Then '// check if .Item Selected.
                Select Case Path.GetExtension(.SelectedItem).ToLower
                    Case ".rtf", ".etc" '// load.File with color synthax If a WordPad/etc. type.Doc.
                        RichTextBox1.LoadFile(myCoolFilesFolder & .SelectedItem, RichTextBoxStreamType.RichText)
                    Case Else '// load.File as simple .txt File.
                        RichTextBox1.LoadFile(myCoolFilesFolder & .SelectedItem, RichTextBoxStreamType.PlainText)
                End Select
                locateAndHighlightCoolWordFromSearch(TextBox1.Text, RichTextBox1)
            End If
        End With
    End Sub

#Region "-----===-----===-----=== SEARCH AND HIGHLIGHT ===-----===-----===-----"

    Private Sub searchFilesForCoolWords(ByVal selCoolFolder As String, ByVal selSearchString As String, ByVal selListBoxForResults As ListBox)
        If Directory.Exists(selCoolFolder) Then
            With ProgressBar1
                .Maximum = Directory.GetFiles(selCoolFolder).Length
                .Value = 0
            End With
            selListBoxForResults.Items.Clear() '// .Clear for new.input.
            For Each itm As String In Directory.GetFiles(selCoolFolder, "*", SearchOption.TopDirectoryOnly)
                sT = File.ReadAllText(itm) '// Read File.
                If sT.Contains(selSearchString) Then
                    ' selListBoxForResults.Items.Add(itm) '// Full.Path of File.
                    selListBoxForResults.Items.Add(Path.GetFileName(itm))
                End If
                ProgressBar1.Value += 1
            Next
        End If
    End Sub

    Private Sub locateAndHighlightCoolWordFromSearch(ByVal selCoolWord As String, ByVal selCoolRTB As RichTextBox)
        '//--- RTB.HighLight code boosted from:
        '// http://www.vbdotnetforums.com/editors/27186-find-select-multiple-words-richtextbox.html#post81507
        '---\\
        With selCoolRTB
            Dim iIndexLocation As Integer = 0
            While iIndexLocation < .Text.LastIndexOf(selCoolWord)
                .Find(selCoolWord, iIndexLocation, .TextLength, RichTextBoxFinds.None)
                .SelectionBackColor = Color.Orange
                iIndexLocation = .Text.IndexOf(selCoolWord, iIndexLocation) + 1
            End While
            .Select()
        End With
    End Sub
#End Region '===-----===-----===-----'

#Region "-----===-----===-----=== MISC. ===-----===-----===-----"
    '// Change Cursor to .Hand if Item.Hovered.
    Private Sub _ListBox1_MouseMove(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles ListBox1.MouseMove
        With CType(sender, ListBox)
            If Not .IndexFromPoint(.PointToClient(MousePosition)) = -1 Then
                .Cursor = Cursors.Hand
                ' Me.Text = .Items.Item(iT) '// get.Value If needed. :)
            Else
                .Cursor = Cursors.Default
            End If
        End With
    End Sub
#End Region '===-----===-----===-----'
End Class

+= I added another goodie from my goodie.bag :D, AndAlso "Glad I could be of help.:)".

 
0
 

Ok, i don't understand what do this:
Private Sub _ListBox1_MouseMove(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles ListBox1.MouseMove
With CType(sender, ListBox)
If Not .IndexFromPoint(.PointToClient(MousePosition)) = -1 Then
.Cursor = Cursors.Hand
' Me.Text = .Items.Item(iT) '// get.Value If needed. :)
Else
.Cursor = Cursors.Default
End If
End With
End Sub
and when i debug the program, try to cheng the listbox inedx it gives me a error, and specify this string:
RichTextBox1.LoadFile(myCoolFilesFolder & .SelectedItem, RichTextBoxStreamType.PlainText)

 
0
 

how i can add Search in SubFolders?

 
0
 

If you did not use the "most".recent code I posted and you just edited the previous.code posted, re.read this:
<<To make things easier, I changed line.20 .From: selListBoxForResults.Items.Add(Path.GetFileNameWithoutExtension(itm)) .To: selListBoxForResults.Items.Add(Path.[B]GetFileName[/B](itm)) .

This will add the .FileName "with" the .FileExtension to your ListBox when loading the search.result Files.

SubFolder?
Change this: For Each itm As String In Directory.GetFiles(selCoolFolder, "*", SearchOption.[B]TopDirectoryOnly[/B]) To: For Each itm As String In Directory.GetFiles(selCoolFolder, "*", SearchOption.[B]AllDirectories[/B]) Just a note; Most of the time you can delete an option as "" all the way up to the "dot" and you will get a list of other possible options from IntelliSense. I learned a great deal after figuring that out.:)

>>Ok, i don't understand what do this:
It just changes the .Cursor from your Default.Cursor, to a cute little "Hand".Cursor.:D
If you don't like my goodies, just say so; don't throw sh.t in my face!!! F.CK!!!:D (just.kidding,(wink)).

 
0
 

ok i have retried, and it continus to give me errors, so i did:

Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
        Dim dir As String = ListBox1.SelectedItem
        Dim DocumentDir As String = My.Computer.FileSystem.SpecialDirectories.MyDocuments & "\CODTech\Documents\" + dir
        Dim DOCUMENTobjReader As New System.IO.StreamReader(DocumentDir)
        RichTextBox1.Text = DOCUMENTobjReader.ReadToEnd
        locateAndHighlightCoolWordFromSearch(TextBox1.Text, RichTextBox1)
    End Sub

before this when i clicked on the items, it gives me an error, now it works fine, it loads the document, but i have a problem with

Private Sub locateAndHighlightCoolWordFromSearch(ByVal selCoolWord As String, ByVal selCoolRTB As RichTextBox)
        With selCoolRTB
            Dim iIndexLocation As Integer = 0
            While iIndexLocation < .Text.LastIndexOf(selCoolWord)
                .Find(selCoolWord, iIndexLocation, .TextLength, RichTextBoxFinds.None)
                .SelectionBackColor = Color.Orange
                iIndexLocation = .Text.IndexOf(selCoolWord, iIndexLocation) + 1
            End While
            .Select()
        End With
    End Sub

because it doesn't display the string highlighted, it deletes from the text, so i was thinking about this:

Richtextbox1.SelectionStart = Richtextbox.Find(Textbox1.text)
richtextbox1.SelectionBackColor = Color.Yellow

i tried, but it doesn't worked it gives me errors, so we have to find a new solution, and make that richtextbox1 autoscrolls to the line

 
0
 

i tried without

Private Sub locateAndHighlightCoolWordFromSearch(ByVal selCoolWord As String, ByVal selCoolRTB As RichTextBox)
        With selCoolRTB
            Dim iIndexLocation As Integer = 0
            While iIndexLocation < .Text.LastIndexOf(selCoolWord)
                .Find(selCoolWord, iIndexLocation, .TextLength, RichTextBoxFinds.None)
                .SelectionBackColor = Color.Orange
                iIndexLocation = .Text.IndexOf(selCoolWord, iIndexLocation) + 1
            End While
            .Select()
        End With
    End Sub

but it hide the string, i opened the text document, and the document was not modified, but richtextbox1 doesn't display the string

 
0
 

If you were able to load a File into the rtb(RichTextBox), Then .Replace the current.Sub that locates and highlights, w/this one.

Private Sub locateAndHighlightCoolWordFromSearch(ByVal selCoolWord As String, ByVal selCoolRTB As RichTextBox)
        With selCoolRTB
            If .HideSelection = True Then .HideSelection = False '// Display Highlighted.Text.
            .Find(selCoolWord, 0, .TextLength, RichTextBoxFinds.None)
        End With
    End Sub

The "HideSelection" should be set in the rtb's.Properties and Not constantly checked If ='s False.

 
0
 

oke, how i have to add this? button1.click whats the source to apply this? (sorry i am not very advanced, just more the beginner)

 
0
 
locateAndHighlightCoolWordFromSearch(TextBox1.Text, RichTextBox1)

That should trigger that "Sub" and fire off the code w/in it.
I would add it to your ListBox.SelectedIndexChanged, though adding it to a Button should do the job just nicely.

 
0
 

so:

Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
        Dim Text As String = ListBox1.SelectedItem
        With ListBox1
            If Not .SelectedIndex = -1 Then
                locateAndHighlightCoolWordFromSearch(TextBox1.Text, RichTextBox1)
            End If
        End With
    End Sub
Private Sub locateAndHighlightCoolWordFromSearch(ByVal selCoolWord As String, ByVal selCoolRTB As RichTextBox)
        With selCoolRTB
            If .HideSelection = True Then .HideSelection = False '// Display Highlighted.Text.
            .Find(selCoolWord, 0, .TextLength, RichTextBoxFinds.None)
        End With
    End Sub

but it doesn't worked, sorry if am wasting your time....

 
0
 

It probably does not work because you are Not loading the file.

You
This article has been dead for over six months: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: