how to search certain word in text file and display in textbox/listbox?
for example
btnSearch click event
Get the name of the file(quarry) to look for (pop up an input box)
Set blnfound to false
If the file exists,
open it in the opentext mode
do until end of file for blnfound = true
Read the four fields from the file
If the quarry.toupper = video_file_name.toupper then
Set the blnfound to true
Endif
Loop
Close the text file
If blnfound = true then
Display each field in the text boxes
Else
Display a message box that the text record was not found
End if
Else
Display a message that says you cannot open the file because it doesn’t exist
End if
search text from saved txt filedispaly in text box
Page 1 of 1
12 Replies - 6642 Views - Last Post: 05 May 2010 - 08:41 AM
Replies To: search text from saved txt file
#2
Re: search text from saved txt file
Posted 01 May 2010 - 02:46 AM
#3
Re: search text from saved txt file
Posted 01 May 2010 - 07:20 PM
sorry to say that , that code doesnt help me
#4
Re: search text from saved txt file
Posted 02 May 2010 - 03:01 AM
if you put some effort in it you can make it to do what you want
#5
Re: search text from saved txt file
Posted 02 May 2010 - 08:10 AM
Private Sub mnuSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuSearch.Click
Dim FF As Short
Dim strTemp, strSearch As String
Dim intLine As Short
intLine = 0
FF = FreeFile()
strSearch = Trim(LCase(InputBox("Enter Video Name or Year or Time or Rating you want to search")))
FileOpen(FF, My.Application.Info.DirectoryPath & "\record.txt", OpenMode.Input)
While Not EOF(FF)
strTemp = LineInput(FF)
intLine = intLine + 1
If InStr(1, LCase(strTemp), strSearch) <> 0 Then
txtVideoName.Text = strTemp.ToString
txtYear.Text = intLine
txtRunningTime.Text = intLine
txtRating.Text = intLine
Exit Sub
Else
MessageBox.Show("not found")
End If
End While
FileClose(FF)
End Sub
i have 4 line in record.txt
Name
Year
Time
Rating
i am getting Name correctly in txtVideoName
but i am getting 1 in other txt boxes
what i am doing wrong
#6
Re: search text from saved txt file
Posted 02 May 2010 - 09:47 AM
#7
Re: search text from saved txt file
Posted 02 May 2010 - 10:15 AM
not working :s
my code
my code
Dim FILE_NAME As String = "C:\record.txt"
Dim TextLine As String
If System.IO.File.Exists(FILE_NAME) = True Then
Dim objReader As New System.IO.StreamReader(FILE_NAME)
Do While objReader.Peek() <> -1
TextLine = InputBox("Enter") & objReader.ReadLine() & vbNewLine
Loop
txtVideoName.Text = TextLine
txtRunningTime.Text = TextLine
Else
MsgBox("File Does Not Exist")
End If
#8
Re: search text from saved txt file
Posted 02 May 2010 - 10:39 AM
ok so i see it is hard for you here is what i did:
i put lots of comments but feel free to ask if you dont understand something
good luck
Public Class Form1
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
'write some info in a file'
Dim objWriter As New System.IO.StreamWriter("C:\Test.txt")
objWriter.WriteLine("Line1")
objWriter.WriteLine("Line2")
objWriter.WriteLine("Line3")
objWriter.Close()
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'read info from the file'
Dim objReader As New System.IO.StreamReader("C:\Test.txt")
Dim strTextFileInfo() As String 'an array of strings for each line'
Dim arrCounter As Integer = 0 'counter for the array'
Dim strTextToSearch As String = String.Empty 'text to saerch'
'read all lines'
Do While objReader.Peek <> -1
'redim the array with the needet elements'
ReDim Preserve strTextFileInfo(arrCounter)
'input the info'
strTextFileInfo(arrCounter) = objReader.ReadLine
'increase the elements'
arrCounter = arrCounter + 1
Loop
'enter the text to search'
strTextToSearch = InputBox("Enter text to search")
'search all elements for the string'
For i As Integer = 0 To arrCounter - 1
If strTextFileInfo(i).ToLower.Contains(strTextToSearch.ToLower) Then
'display the found elements'
MessageBox.Show(strTextFileInfo(i))
End If
Next
End Sub
End Class
i put lots of comments but feel free to ask if you dont understand something
good luck
#9
Re: search text from saved txt file
Posted 02 May 2010 - 08:25 PM
thanx a lot my fren,
it really help full but onething, if i have
Name
Time
Year
Rating
Name2
Time2
Year2
Raiting2
in my text.txt
and when i input "Name" for search it should display Name, Time, Year, Rating in related textboxes
is it possible ?
sorry i am giving you hard time
it really help full but onething, if i have
Name
Time
Year
Rating
Name2
Time2
Year2
Raiting2
in my text.txt
and when i input "Name" for search it should display Name, Time, Year, Rating in related textboxes
is it possible ?
sorry i am giving you hard time
#10
Re: search text from saved txt file
Posted 03 May 2010 - 02:57 AM
yes it is if the format of your text file is like so :
Name
Time
Year
Rating
then if you find the name the next 3 elements in the array must be the Time Year and Rating. but your format must be the same. i suggest you use a database not text files it is easy and faster
Name
Time
Year
Rating
then if you find the name the next 3 elements in the array must be the Time Year and Rating. but your format must be the same. i suggest you use a database not text files it is easy and faster
This post has been edited by NoBrain: 03 May 2010 - 02:57 AM
#11
Re: search text from saved txt file
Posted 03 May 2010 - 09:21 AM
how to do with text file any idea ?
i am using array structure
here are my entire code
i am using array structure
here are my entire code
Option Strict On
Imports System.IO
Public Class Form1
'Class level variables
'The document file name.
Dim strReportName As String = String.Empty
'blnIsChanged equals True when the document has
' been changed since it was last saved. it equals
' false when the document has not been changed since it was last saved.
Dim blnIsChanged As Boolean = False
Structure videoData
Dim strVideoName As String
Dim strYearProduced As String
Dim strRunningTime As String
Dim strRating As String
End Structure
Private Sub SaveRecord()
'Save the current document under the name stored in documentName
Dim videoRecord As videoData
Dim inputRecord As StreamWriter
videoRecord.strVideoName = txtVideoName.Text
videoRecord.strYearProduced = txtYear.Text
videoRecord.strRunningTime = txtRunningTime.Text
videoRecord.strRating = txtRating.Text
inputRecord = File.AppendText(strReportName)
inputRecord.WriteLine("Video Name: " & (videoRecord.strVideoName))
inputRecord.WriteLine("Year Produced: " & (videoRecord.strYearProduced))
inputRecord.WriteLine("Runing Time: " & videoRecord.strRunningTime)
inputRecord.WriteLine("Rating:" & videoRecord.strRating)
inputRecord.Close()
'update blnIschange variable
blnIsChanged = False
End Sub
Private Sub SearchRecord()
' Use the openFileDialog control to get a file name
'Dim videoRecord As videoData
''Dim inputRecord As StreamReader
'videoRecord.strVideoName = txtVideoName.Text
'videoRecord.strYearProduced = txtYear.Text
'videoRecord.strRunningTime = txtRunningTime.Text
'videoRecord.strRating = txtRating.Text
' If ofdOpenFile.ShowDialog = Windows.Forms.DialogResult.OK Then
' strReportName = ofdOpenFile.FileName
' inputRecord = File.OpenText(strReportName)
' videoRecord.strVideoName = inputRecord.ReadToEnd()
' videoRecord.strYearProduced = inputRecord.ReadToEnd()
' videoRecord.strRunningTime = inputRecord.ReadToEnd()
' videoRecord.strRating = inputRecord.ReadToEnd()
' inputRecord.Close()
' End If
End Sub
Private Sub mnuFileSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuFileSave.Click
'Save the Records of Video Collection
If sfdSaveFile.ShowDialog = _
Windows.Forms.DialogResult.OK Then
strReportName = sfdSaveFile.FileName
SaveRecord()
End If
End Sub
[b] Private Sub mnuSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuSearch.Click
Dim strVideoName As String
Dim strYearProduced As String
Dim strRunningTime As String
Dim strRating As String
Dim inputRecord As StreamReader
Dim strInput As String
Dim blnFound As Boolean
strInput = InputBox("Enter Name")
'Do While inputRecord.Peek = -1
If File.Exists("c:\Records.txt") Then
inputRecord = File.OpenText("C:\RECORDS.txt")
blnFound = True
strInput = inputRecord.ReadLine()[/b]
For intCount = 1 To intNUM_RECORDS
'Read records from the file
strVideoName = inputRecord.ReadLine()
strYearProduced = inputRecord.ReadLine()
strRunningTime = txtRunningTime.Text
strRating = txtRating.Text
'Display the data in the text box using vbTab.
txtVideoName.Text = strVideoName
txtYearProduce.text=
Next intCount
Else
blnFound = False
MessageBox.Show("Record not found")
End If
'Loop
End Sub
Private Sub mnuReport_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuReport.Click
'Print
End Sub
End Class
#12
Re: search text from saved txt file
Posted 03 May 2010 - 10:46 AM
try using classes and array of classes like so:
here is the code in the class:
it is easy if you make a new class for each element and the search is much more clean. it looks to me any way. i dont know how efficient is.
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'fill data in array of classes structures'
Dim arrClass1() As Class1 'create array of classes'
'make 11 elements'
ReDim arrClass1(10)
'create new elements 11 time'
For i As Int16 = 0 To 10
'create element'
arrClass1(i) = New Class1
'set values'
arrClass1(i).m_.myStr1 = i.ToString
arrClass1(i).m_.myStr2 = (i + 2).ToString
'save the info'
arrClass1(i).SaveInfo("C:\TestDir\Info" & i & ".txt")
Next
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
'search for info'
Dim arrClass1() As Class1
Dim strTextSearch As String = String.Empty
ReDim arrClass1(10)
strTextSearch = InputBox("Enter Text")
For i As Int16 = 0 To 10
'make new class'
arrClass1(i) = New Class1
'fill the data'
arrClass1(i).GetInfo("C:\TestDir\Info" & i & ".txt")
'search the data'
arrClass1(i).SearchInfo(strTextSearch)
Next
End Sub
End Class
here is the code in the class:
Public Class Class1
Public Structure MyStruct
Dim myStr1 As String
Dim myStr2 As String
End Structure
Public m_ As MyStruct
Public Sub SaveInfo(ByVal strPath As String)
'write the info to the file'
Dim objWriter As New System.IO.StreamWriter(strPath)
objWriter.WriteLine(m_.myStr1)
objWriter.WriteLine(m_.myStr2)
objWriter.Close()
End Sub
Public Sub GetInfo(ByVal strPath As String)
'read the info from the file'
Dim objReader As New System.IO.StreamReader(strPath)
m_.myStr1 = objReader.ReadLine
m_.myStr2 = objReader.ReadLine
objReader.Close()
End Sub
Public Sub SearchInfo(ByVal strTextToSearch As String)
'search in the 2 elements in the structure'
Debug.Print(m_.myStr2)
If m_.myStr2.ToLower.Contains(strTextToSearch.ToLower) Then
MessageBox.Show(m_.myStr2)
End If
If m_.myStr1.ToLower.Contains(strTextToSearch.ToLower) Then
MessageBox.Show(m_.myStr1)
End If
End Sub
End Class
it is easy if you make a new class for each element and the search is much more clean. it looks to me any way. i dont know how efficient is.
This post has been edited by NoBrain: 03 May 2010 - 10:48 AM
#13
Re: search text from saved txt file
Posted 05 May 2010 - 08:41 AM
i am trying to print from txt file
i am getting "Index (zero based) must be greater than or equal to zero and less than the size of the argument list." error at (bold text above)
wht i am doin wrong
Private Sub pdDoc_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles pdDoc.PrintPage
Dim fmt1 As String = "{0, 20}{1, 20}{2, 20} {3, 20}"
Dim intVertPosition As Integer
Dim videoRecord As StreamReader
If File.Exists("Record.txt") Then
'open file
videoRecord = File.OpenText("Record.txt")
'Print Report Header
e.Graphics.DrawString("Mulmiz Video Collection Report", _
New Font("Courier New", 12, FontStyle.Bold), Brushes.Black, 150, 10)
e.Graphics.DrawString("Date & Time:" & Now.ToString(), _
New Font("Courier New", 10, FontStyle.Regular), Brushes.Black, 10, 38)
'Print Column Heading
e.Graphics.DrawString(String.Format(fmt1, "Video Name", "Year Produced", _
"Running Time", "Rating"), New Font _
("Courier New", 10, FontStyle.Regular), _
Brushes.Black, 10, 66)
'Print contains
intVertPosition = 50
[b] Do While videoRecord.Peek <> -1
e.Graphics.DrawString(String.Format(fmt1, videoRecord.ReadLine), New Font _
("Courier New", 10, FontStyle.Regular), _
Brushes.Black, 10, intVertPosition)
intVertPosition += 14
Loop[/b]
videoRecord.Close()
Else
MessageBox.Show("File does not exits.")
End If
End Sub
i am getting "Index (zero based) must be greater than or equal to zero and less than the size of the argument list." error at (bold text above)
wht i am doin wrong
Page 1 of 1
|
|

New Topic/Question
Reply




MultiQuote



|