|
-
Nov 28th, 2006, 03:53 PM
#1
Thread Starter
Addicted Member
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?
-
Nov 28th, 2006, 04:05 PM
#2
Re: delete from textbox
Here's one way of doing it using the Replace() function.
VB Code:
Option Explicit
Private Sub ReplaceInFile(ByVal FilePath As String, _
ByVal ReplaceThis As String, ByVal WithThis As String, _
Optional ByVal CompareMethod As VbCompareMethod = vbTextCompare)
Dim strData As String, lonFF As Long
Dim strPath As String
'Get available file handle.
lonFF = FreeFile
'Open file.
Open FilePath For Input As #lonFF
'Load all contents of file into strData.
strData = Input(LOF(lonFF), lonFF)
Close #lonFF 'Close file.
'Replace the words.
strData = Replace$(strData, ReplaceThis, WithThis, , , CompareMethod)
lonFF = FreeFile
'Create a 2nd file.
strPath = FilePath & "(2).txt"
Open strPath For Binary Access Write As #lonFF
Put #lonFF, , strData
Close #lonFF
End Sub
'Test the code.
Private Sub Command1_Click()
'Keep first word.
Text1.Text = Left$(Text1.Text, (InStr(1, Text1.Text, " ") - 1))
'Replace all words.
'vbTextCompare so it's not case-sensitive.
ReplaceInFile "C:\file.txt", "word1", "*word1", vbTextCompare
End Sub
-
Nov 28th, 2006, 04:12 PM
#3
Lively Member
Re: delete from textbox
Maybe you could do:
VB Code:
Private Sub Command1_Click()
Dim strTempArray() As String
Dim strTemp As String
Dim intFreeFileNum As Integer
Dim intCounter As Integer
intFreeFileNum = FreeFile()
strTempArray = Split(Text1.Text, " ")
Text2.Text = strTempArray(0)
Open "\filenamehere.txt" For Input As intFreeFileNum
Do Until EOF(intFreeFileNum)
Input #intFreeFileNum, strTemp
strTempArray = Split(strTemp, " ")
For intCounter = 1 To UBound(strTempArray)
If LCase(Text2.Text) = LCase(strTempArray(intCounter)) Then
Text2.Text = "*" + Text2.Text
End If
Next intCounter
Loop
Close intFreeFileNum
End Sub
I think that does what you want to do without needing API.
-
Nov 29th, 2006, 01:23 PM
#4
Thread Starter
Addicted Member
Re: delete from textbox
i tried both ways but
i didn't got the star char in the file
why?
-
Nov 30th, 2006, 04:25 AM
#5
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|