Results 1 to 5 of 5

Thread: delete from textbox

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jul 2006
    Posts
    131

    delete from textbox

    how can i keep the first word in a textbox and delete all other words
    then search a .txt file for that word
    if found add "*" befor it

    simple but how?

  2. #2
    "Digital Revolution"
    Join Date
    Mar 2005
    Posts
    4,471

    Re: delete from textbox

    Here's one way of doing it using the Replace() function.

    VB Code:
    1. Option Explicit
    2.  
    3. Private Sub ReplaceInFile(ByVal FilePath As String, _
    4.     ByVal ReplaceThis As String, ByVal WithThis As String, _
    5.     Optional ByVal CompareMethod As VbCompareMethod = vbTextCompare)
    6.    
    7.     Dim strData As String, lonFF As Long
    8.     Dim strPath As String
    9.    
    10.     'Get available file handle.
    11.     lonFF = FreeFile
    12.    
    13.     'Open file.
    14.     Open FilePath For Input As #lonFF
    15.         'Load all contents of file into strData.
    16.         strData = Input(LOF(lonFF), lonFF)
    17.     Close #lonFF 'Close file.
    18.    
    19.     'Replace the words.
    20.     strData = Replace$(strData, ReplaceThis, WithThis, , , CompareMethod)
    21.    
    22.     lonFF = FreeFile
    23.     'Create a 2nd file.
    24.     strPath = FilePath & "(2).txt"
    25.    
    26.     Open strPath For Binary Access Write As #lonFF
    27.         Put #lonFF, , strData
    28.     Close #lonFF
    29.    
    30. End Sub
    31.  
    32. 'Test the code.
    33. Private Sub Command1_Click()
    34.     'Keep first word.
    35.     Text1.Text = Left$(Text1.Text, (InStr(1, Text1.Text, " ") - 1))
    36.    
    37.     'Replace all words.
    38.     'vbTextCompare so it's not case-sensitive.
    39.     ReplaceInFile "C:\file.txt", "word1", "*word1", vbTextCompare
    40. End Sub

  3. #3
    Lively Member
    Join Date
    Jan 2006
    Posts
    71

    Re: delete from textbox

    Maybe you could do:

    VB Code:
    1. Private Sub Command1_Click()
    2.     Dim strTempArray() As String
    3.     Dim strTemp As String
    4.     Dim intFreeFileNum As Integer
    5.     Dim intCounter As Integer
    6.    
    7.     intFreeFileNum = FreeFile()
    8.    
    9.     strTempArray = Split(Text1.Text, " ")
    10.    
    11.     Text2.Text = strTempArray(0)
    12.    
    13.     Open "\filenamehere.txt" For Input As intFreeFileNum
    14.         Do Until EOF(intFreeFileNum)
    15.             Input #intFreeFileNum, strTemp
    16.            
    17.             strTempArray = Split(strTemp, " ")
    18.            
    19.             For intCounter = 1 To UBound(strTempArray)
    20.                 If LCase(Text2.Text) = LCase(strTempArray(intCounter)) Then
    21.                     Text2.Text = "*" + Text2.Text
    22.                 End If
    23.             Next intCounter
    24.         Loop
    25.     Close intFreeFileNum
    26.  
    27. End Sub

    I think that does what you want to do without needing API.

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Jul 2006
    Posts
    131

    Re: delete from textbox

    i tried both ways but
    i didn't got the star char in the file

    why?

  5. #5
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: delete from textbox

    is the word in the textbox likely to be unicode?
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

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