|
-
Oct 10th, 2005, 06:42 AM
#1
Thread Starter
Junior Member
checking for text from a text file
Helo Guyz,
i am kinda newbie to vb plz could somone help me on this?
I have a file for exapmle Named "test.dat" and it contains some text like
"hello"
"world"
"ugly"
"fear" and so on now on my form i have a list box and a textbox and a command button too.
when i click on the command button the text tht is contained in the textbox is added to the listbox.
wht i want to check is when i add the text to the listbox i want to check if the text in the text box is in the file "test.dat" if not then it wont b added. i hope u understand.
-
Oct 10th, 2005, 07:09 AM
#2
Re: checking for text from a text file
hi,
i have written a sample for you and added alot of commenting to explain what is going on, since this is actually more involved then you may think. It involves playing with byte arrays, file streams and encoding.
you need to read the file and store it in an array, which you then test for the text being added, i have done this and the testing for the text in one sub.. but in reality you should read the file once only and test multiple times (unless you need it to keep updating)
VB Code:
Dim stringList() As String 'will be the end resulted file
Dim fileBlog As String 'file blog (entire content in a string)
'open file, create a filestream object
Dim fileStream As New IO.FileStream(Application.StartupPath & "\test.txt", IO.FileMode.Open)
'prepare an array of bytes
Dim fileBytes(fileStream.Length) As Byte 'array of file bytes
fileStream.Read(fileBytes, 0, fileStream.Length) 'read bytes of files into byte array
'close file
fileStream.Close()
'convert file bytes into ASCII text
fileBlog = System.Text.Encoding.ASCII.GetString(fileBytes)
'split fileblog into seperate strings of text
'a new line is ASCII characters 10 and 13
'so we will remove 10, and split at 13
fileBlog = Replace(fileBlog, Chr(10), "") 'remove 10
stringList = fileBlog.Split(Chr(13)) 'split at 13, populate string array
'check that text from textbox1 is not in the string array
If Array.IndexOf(stringList, TextBox1.Text) = -1 Then
MsgBox("not in array")
Else
MsgBox("IN ARRAY!")
End If
-
Oct 10th, 2005, 01:41 PM
#3
Re: checking for text from a text file
Heres something a little simpler...
VB Code:
Dim strEnter As String = "hello" 'set this value equal to the string you want to check
Dim strSearch As String
For Each strSearch In ListBox1.Items
If strSearch = strEnter Then 'if strings are equal
MsgBox("already entered!")
Exit Sub
Else
ListBox1.Items.Add(strEnter) 'adds it, because it is different
End If
Next
-
Oct 10th, 2005, 06:16 PM
#4
Re: checking for text from a text file
actually gigemboy,
your example searhes a listbox, not a file.
also, you do not need to scroll through every item.
there are pre-existing functions for all arrays in .net
so your code would be much smaller and efficient as this
VB Code:
Dim strEnter As String = "hello" 'set this value equal to the string you want to check
If ListBox1.Items.Contains(strEnter) Then 'if string is in listbox
MsgBox("already entered!")
Else
ListBox1.Items.Add(strEnter) 'adds it, because it is different
End If
-
Oct 10th, 2005, 06:32 PM
#5
Re: checking for text from a text file
thx Phil.. If you read my signature, I'm still a lil new at this myself Never used the "contains" method before, but I know about it now
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|