Results 1 to 5 of 5

Thread: checking for text from a text file

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Oct 2002
    Location
    Tanzania (Arusha)
    Posts
    17

    Exclamation 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.
    Muz

  2. #2
    Frenzied Member Phill64's Avatar
    Join Date
    Jul 2005
    Location
    Queensland, Australia
    Posts
    1,201

    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:
    1. Dim stringList() As String 'will be the end resulted file
    2.         Dim fileBlog As String 'file blog (entire content in a string)
    3.  
    4.         'open file, create a filestream object
    5.         Dim fileStream As New IO.FileStream(Application.StartupPath & "\test.txt", IO.FileMode.Open)
    6.  
    7.         'prepare an array of bytes
    8.         Dim fileBytes(fileStream.Length) As Byte 'array of file bytes
    9.         fileStream.Read(fileBytes, 0, fileStream.Length) 'read bytes of files into byte array
    10.  
    11.         'close file
    12.         fileStream.Close()
    13.  
    14.         'convert file bytes into ASCII text
    15.         fileBlog = System.Text.Encoding.ASCII.GetString(fileBytes)
    16.  
    17.         'split fileblog into seperate strings of text
    18.         'a new line is ASCII characters 10 and 13
    19.         'so we will remove 10, and split at 13
    20.         fileBlog = Replace(fileBlog, Chr(10), "") 'remove 10
    21.         stringList = fileBlog.Split(Chr(13)) 'split at 13, populate string array
    22.  
    23.         'check that text from textbox1 is not in the string array
    24.         If Array.IndexOf(stringList, TextBox1.Text) = -1 Then
    25.             MsgBox("not in array")
    26.         Else
    27.             MsgBox("IN ARRAY!")
    28.         End If

  3. #3
    PowerPoster
    Join Date
    Aug 2005
    Location
    College Station, TX
    Posts
    4,521

    Re: checking for text from a text file

    Heres something a little simpler...
    VB Code:
    1. Dim strEnter As String = "hello" 'set this value equal to the string you want to check
    2. Dim strSearch As String
    3.         For Each strSearch In ListBox1.Items
    4.             If strSearch = strEnter Then 'if strings are equal
    5.                 MsgBox("already entered!")
    6.                 Exit Sub
    7.             Else
    8.                 ListBox1.Items.Add(strEnter) 'adds it, because it is different
    9.             End If
    10.         Next

  4. #4
    Frenzied Member Phill64's Avatar
    Join Date
    Jul 2005
    Location
    Queensland, Australia
    Posts
    1,201

    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:
    1. Dim strEnter As String = "hello" 'set this value equal to the string you want to check
    2.         If ListBox1.Items.Contains(strEnter) Then 'if string is in listbox
    3.             MsgBox("already entered!")
    4.         Else
    5.             ListBox1.Items.Add(strEnter) 'adds it, because it is different
    6.         End If

  5. #5
    PowerPoster
    Join Date
    Aug 2005
    Location
    College Station, TX
    Posts
    4,521

    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
  •  



Click Here to Expand Forum to Full Width