0

i first locate the last word in my richtextbox then i check if that string matches a particular string .

then i replace the string with another string.

the problem is that whenever i enter the same string and click my "rename" button it renames both strings because if both strings are now "dog" then it will recognize lastWord as dog and not as the FINAL most recently entered word thus it will be renamed to something else .

private sub getwords()

    dval = dval + 1

    Dim lastWord As String = RichTextBox1.Text.Split(" ").Last


    If dval = 1 And lastWord = "meu" Then

        RichTextBox1.Text = RichTextBox1.Text.Replace(lastWord, "dog")

    End If

    MsgBox(lastWord)

    If dval = 2 And RichTextBox1.Text.EndsWith("dog") And lastWord = "dog" Then

        RichTextBox1.Text = RichTextBox1.Text.Replace(lastWord, "cat")
        dval =0
    End If

end sub
4

1 に答える 1

0

How about this naive solution, it removes the last word then concats it to the end.

private sub getwords()

    dval = dval + 1

    Dim lastWord As String = RichTextBox1.Text.Split(" ").Last


    If dval = 1 AndAlso lastWord = "meu" Then

        RichTextBox1.Text = RichTextBox1.Text.Remove(RichTextBox1.Text.Length - lastWord.Length) & "dog"


    End If

    MsgBox(lastWord)

    If dval = 2 AndAlso RichTextBox1.Text.EndsWith("dog") AndAlso lastWord = "dog" Then

        RichTextBox1.Text = RichTextBox1.Text.Remove(RichTextBox1.Text.Length - lastWord.Length) & "cat"
        dval = 0
    End If

End Sub
于 2013-03-11T21:32:07.133 に答える