Replace function issues.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • SLDenman
    New Member
    • Sep 2010
    • 4

    Replace function issues.

    I am coding for a text editor with a find and replace function. I am getting errors in my code and need help fixing them.

    Code:
    Private Sub ReplaceButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ReplaceButton.Click
            Dim StartPos, Counter As Integer
            Dim FindString, ReplaceText As String
            .FindString = ""
            .ReplaceText = ""
    
            For Counter = 1 To Len(TextRichTextBox.Text)
                StartPos = InStr(FindTextBox.Text, FindString)
                If StartPos > 0 Then
                    FindTextBox.Text = StartPos - 1
                    FindTextBox.Text.SelLength = Len(FindString)
                    FindTextBox.Text.SelText = "" + ReplaceText
                End If
                Next()
  • Joseph Martell
    Recognized Expert New Member
    • Jan 2010
    • 198

    #2
    First, what kind of errors are you getting? Are you not able to compile, is an exception being thrown, are alligators climbing out of your computer and attacking your furniture?

    Second, if this code is an exact copy of your code as you have typed it in your editor, then I am going to say that there are some syntax errors that you need to take care of:

    Lines 4 & 5 start with the dot operator, but there is no containing with statement. You need to get rid of the . or have an object before the dot.

    Line 14 has "Next()" as though it were a function call. So your For loop has no Next statement to end it.

    You do not have an End Sub statement so this sub is not closed. That would cause a compilation error and likely several other syntax errors.

    Line 11 & 12 are apparently trying to access properties of the string class called "SelLength" and "SelText", but MSDN makes no reference to these properties of the string class. These could be extensions, and if so everything is fine, I just didn't know about them

    You appear to be trying to use the "+" operator to concatenate text (line 12), but in VB the "&" is the concatenation operator.

    Lastly, and this is a VB .Net thing not a VB syntax thing, VB .Net is very object oriented. You are using some older functions that are hold-overs from VB6 and earlier like Len() (line 11) and Instr() (line 8). If this truly is a VB .Net app, as the location of the post suggests, consider using the Length property and either the Contains method or the IndexOf method of the string class. It is more in keeping with an object oriented language, offers greater type safety, and is more efficient.

    Comment

    Working...