Results 1 to 13 of 13

Thread: [RESOLVED] Need some help...Maybe with registry?

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Oct 2007
    Posts
    118

    Resolved [RESOLVED] Need some help...Maybe with registry?

    Hey guys.

    I have a very simple word replacer that goes like this:

    vb Code:
    1. txt_write = Replace(txt_write.Text, " Totaly ", " Totally ")
    2. 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.

  2. #2
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    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
    Last edited by Doogle; Apr 19th, 2008 at 01:46 AM.

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Oct 2007
    Posts
    118

    Re: Need some help...Maybe with registry?

    It didn't work .

  4. #4
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    Re: Need some help...Maybe with registry?

    I what way didn't it work ?

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Oct 2007
    Posts
    118

    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?

  6. #6
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    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

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Oct 2007
    Posts
    118

    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.

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

    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
    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

  9. #9

    Thread Starter
    Lively Member
    Join Date
    Oct 2007
    Posts
    118

    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 :|
    Last edited by ebonez; Apr 19th, 2008 at 05:51 AM.

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

    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:
    1. Private Sub Text1_KeyPress(KeyAscii As Integer)
    2. Static mystart As Integer
    3. Dim thisword As String, thisreplace As String, i As Integer, curpos As Integer
    4. Select Case KeyAscii
    5.     Case 32, 44, 46, 63, 33, 13, 10  'add any others you might need
    6.         curpos = Text1.SelStart    'save cursor position
    7.         mystart = InStrRev(Text1, " ", Text1.SelStart) + 1  ' get start position of word, at previous space character
    8.         thisword = Mid(Text1, mystart, Text1.SelStart - mystart + 1)  ' get word
    9.         For i = 0 To UBound(replacearr)    ' loop through array
    10.             If InStr(1, replacearr(i), thisword, vbTextCompare) > 0 Then  'search each element n array
    11.                 thisreplace = Mid(replacearr(i), InStr(replacearr(i), ":") + 1)  ' get replacement word
    12.                 ' in here you can check for case of characters in thisword and set the replacement to the same
    13.                 Text1 = Replace(Text1, thisword, thisreplace)
    14.                 Text1.SelStart = curpos - Len(thisword) + Len(thisreplace)  ' reset cursor position
    15.                 Exit Sub
    16.             End If
    17.         Next
    18. End Select
    19. End Sub
    20.  
    21. Private Sub Form_Load()
    22. Open "replace.txt" For Input As 1   'add path to file
    23.     replacearr = Split(Input(LOF(1), #1), vbNewLine)  'load entire file into array
    24.     Close 1
    25. 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
    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

  11. #11

    Thread Starter
    Lively Member
    Join Date
    Oct 2007
    Posts
    118

    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?

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

    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:
    1. if thisword = ucase(thisword) then
    2. thisreplace = ucase(thisreplace)
    3. elseif left(thisword,1) = ucase(left(thisword,1)) then
    4. thisreplace = strconv(thisreplace, vbpropercase)
    5. end if
    i haven't tested this
    Last edited by westconn1; Apr 19th, 2008 at 09:20 PM.
    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

  13. #13

    Thread Starter
    Lively Member
    Join Date
    Oct 2007
    Posts
    118

    Re: Need some help...Maybe with registry?

    Ok. Thanks so much for your help. Rep +1 and resolved

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