Ask Me Help Desk

Ask Me Help Desk (https://www.askmehelpdesk.com/forum.php)
-   Visual Basic (https://www.askmehelpdesk.com/forumdisplay.php?f=469)
-   -   Word Finder Help on VB! (https://www.askmehelpdesk.com/showthread.php?t=658133)

  • May 7, 2012, 03:07 PM
    lamlamlamiaaa
    Word Finder Help on VB!
    So far, I made a program where the user enters a sentence in one textbox and then the user enters a word they want to find in that sentence in another text box. After clicking the button, the program will show the word in capital letters. But, the problem is that, it only capitalizes the first word it finds. For example, "Hi my name is visual basic and I love VIsual basic" IF the user wants to find "Visual" it only capitalizes the first visual and not the second one. I need it so that it does that. Here is my code so far :


    Dim sentence As String
    Dim word As String
    Dim location As String
    Dim new_sentence As String

    sentence = TextBox1.Text
    word = TextBox2.Text
    location = InStr(sentence, word)
    found = found + 1

    new_sentence = Mid(sentence, 1, location - 1) & UCase(word) & Mid(sentence, location + Len(word))
    Label3.Text = new_sentence


    It would be lots of help!
    Thank you (;
  • Sep 28, 2012, 11:21 AM
    jsblume
    The problem is that the way the InStr function is being used you are always finding only the first. What you need is a loop

    You don't need the new_sentence variable. Notice inside the While-Wend loop that I call the InStr function with three arguments instead of two. When the first argument is a number, then it indicates the starting position in the string for the search.

    Dim sentence As String
    Dim word As String
    Dim location As String
    Dim new_sentence As String

    sentence = TextBox1.Text
    word = TextBox2.Text
    location = InStr(sentence, word)
    found = 0

    while location > 0

    found += 1

    sentence = Mid(sentence, 1, location - 1) & UCase(word) & Mid(sentence, location + Len(word))

    location = InStr(location + 1, sentence, word)

    wend

    Label3.Text = sentence

  • All times are GMT -7. The time now is 12:00 PM.