[RESOLVED] Need some help...Maybe with registry?
Hey guys. :D
I have a very simple word replacer that goes like this:
vb Code:
txt_write = Replace(txt_write.Text, " Totaly ", " Totally ")
txt_write = Replace(txt_write.Text, " totaly ", " totally ")
It has a lot of that with other words.
Now what I want to happen is for there to be two textboxes and in one you can write a word and in the other write the word you want it to get replaced with. Then they click a button and it does that. Like when the form starts then it does the new one as well as all the old ones.
I hope I explained that alright :|
Thanks.
Re: Need some help...Maybe with registry?
Assuming that your two textBoxes are txtBadWord and txtGoodWord, then something like this
Code:
Private Sub Command1_Click()
Dim strGood As String
Dim strBad As String
'
' Replace the Lower Case words
' (eg totaly with totally)
'
strGood = LCase(txtGoodWord.Text)
strBad = LCase(txtBadWord.Text)
txt_write.txt = Replace(txt_write.Text, strBad, strGood)
'
' Replace the Proper Case words
' (eg Totaly with Totally)
'
strGood = StrConv(strGood, vbProperCase)
strBad = StrConv(strBad, vbProperCase)
txt_write.txt = Replace(txt_write.Text, strBad, strGood)
End Sub
Re: Need some help...Maybe with registry?
Re: Need some help...Maybe with registry?
I what way didn't it work ?
Re: Need some help...Maybe with registry?
It just didn't work completly. I had it on a differnt form aswell and also tried it on the same form as the main textbox. No error came up. And also, is that one supposed to make it load when the form loads aswell?
Re: Need some help...Maybe with registry?
There's a couple of typos in my original. This is the corect version
Code:
Private Sub Command1_Click()
Dim strGood As String
Dim strBad As String
'
' Replace the Lower Case words
' (eg totaly with totally)
'
strGood = LCase(txtGoodword.Text)
strBad = LCase(txtBadWord.Text)
txt_write.Text = Replace(txt_write.Text, strBad, strGood)
'
' Replace the Proper Case words
' (eg Totaly with Totally)
'
strGood = StrConv(strGood, vbProperCase)
strBad = StrConv(strBad, vbProperCase)
txt_write.Text = Replace(txt_write.Text, strBad, strGood)
End Sub
Put the code in the same form as the txt_write TextBox, type something into txtBadWord and what you want to replace it by in txtGoodWord and click Command1 - it will perform the replacements in txt_write
I don't understand the "Form_Load" question,when the Form loads there will be nothing in txtBadWord or txtGoodWord so there will be nothing to replace
Re: Need some help...Maybe with registry?
Sory, I suck at explaining.
I'll try again :P
Ok, so I have to text boxes. One where a user enters a word and another textbox where a user enters another word to replace the first one with.
When the user pushes a button, it will correct it all in the main text box and also add it to the registry or something and make is do the replace as the user types instead of them having to push it constantly to fix it all the time.
And then, when the user exits the program and comes back in, it will load up the same two words and replace them automatically without them having to type them in again.
Re: Need some help...Maybe with registry?
don't use registry to save the replacement values, use a text file or database as it looks like it could be a lot of replacements to be stored, the code would then be dependent on the type of storage you decide to use
to run the replacements automatically you could put the code in the keyup event, to run when the keycode is for a space or other punctuation mark (,.?!)) indicating end of a word, you would then need to find if the last word is in the replacement table and if so replace it
also to speed the program you could have a count each time a word replacement is used then sort the list by frequency of use
Re: Need some help...Maybe with registry?
Ok, thanks. Do you think you could start me off with a little bit of code? Sorry if I'm been to much of an ass :|
Re: Need some help...Maybe with registry?
try like this, there may be better ways, i have tested a bit, but not much
vb Code:
Private Sub Text1_KeyPress(KeyAscii As Integer)
Static mystart As Integer
Dim thisword As String, thisreplace As String, i As Integer, curpos As Integer
Select Case KeyAscii
Case 32, 44, 46, 63, 33, 13, 10 'add any others you might need
curpos = Text1.SelStart 'save cursor position
mystart = InStrRev(Text1, " ", Text1.SelStart) + 1 ' get start position of word, at previous space character
thisword = Mid(Text1, mystart, Text1.SelStart - mystart + 1) ' get word
For i = 0 To UBound(replacearr) ' loop through array
If InStr(1, replacearr(i), thisword, vbTextCompare) > 0 Then 'search each element n array
thisreplace = Mid(replacearr(i), InStr(replacearr(i), ":") + 1) ' get replacement word
' in here you can check for case of characters in thisword and set the replacement to the same
Text1 = Replace(Text1, thisword, thisreplace)
Text1.SelStart = curpos - Len(thisword) + Len(thisreplace) ' reset cursor position
Exit Sub
End If
Next
End Select
End Sub
Private Sub Form_Load()
Open "replace.txt" For Input As 1 'add path to file
replacearr = Split(Input(LOF(1), #1), vbNewLine) 'load entire file into array
Close 1
End Sub
you need to declare the array in the general section at the top of the form
dim replacearr() as string
text file replace.txt, needs to be in format
Code:
word:replacement
word2:replacement2
totaly:totally
make sure no spaces and no empty lines in file
Re: Need some help...Maybe with registry?
Ok, that works. But there is a problem. When I type, for example, "Word" it gets replaced with "replacement," instead of "Replacement" (note the capital R).
Is there anyway to get it to work with the case of the word?
Re: Need some help...Maybe with registry?
at line 12 you can check for capitaliation for the entire word or first character and set the replacement word to match
try insertng these lines at line 12
vb Code:
if thisword = ucase(thisword) then
thisreplace = ucase(thisreplace)
elseif left(thisword,1) = ucase(left(thisword,1)) then
thisreplace = strconv(thisreplace, vbpropercase)
end if
i haven't tested this
Re: Need some help...Maybe with registry?
Ok. Thanks so much for your help. Rep +1 and resolved :D