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.
codeorder
Postaholic
2,124 posts since Aug 2010
Reputation Points: 256
Solved Threads: 387
Skill Endorsements: 8
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.:)".
codeorder
Postaholic
2,124 posts since Aug 2010
Reputation Points: 256
Solved Threads: 387
Skill Endorsements: 8
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)).
codeorder
Postaholic
2,124 posts since Aug 2010
Reputation Points: 256
Solved Threads: 387
Skill Endorsements: 8
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.
codeorder
Postaholic
2,124 posts since Aug 2010
Reputation Points: 256
Solved Threads: 387
Skill Endorsements: 8
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.
codeorder
Postaholic
2,124 posts since Aug 2010
Reputation Points: 256
Solved Threads: 387
Skill Endorsements: 8
It probably does not work because you are Not loading the file.
codeorder
Postaholic
2,124 posts since Aug 2010
Reputation Points: 256
Solved Threads: 387
Skill Endorsements: 8