Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

RichTextBox searching (efficient coding)

Status
Not open for further replies.

Turpis

Programmer
Apr 16, 2002
151
I am building a program that provides an interface for editing CNC program files here at the shop. I thought it might be nice to point out certain machine codes in different colors for ease of coding. I wrote some test code to do this and though it works, I find myself wondering if there is a more efficient way of going about this.

Code:
    Private Sub btnBlueSelection_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBlueSelection.Click
        Dim iRet As Integer = Me.rtbMain.SelectionStart
        Dim i As Integer = 0
        Dim j As Integer = 0
        Dim iCont As Integer = 0
        Dim myStr As String
        Dim iFound() As Integer

        [COLOR=green]'---Start from the beginning of text[/color]
        Me.rtbMain.Select(0, 0)

        [COLOR=green]'---Get the search string[/color]
        myStr = InputBox("Input text string", "Find")

        [COLOR=green]'---Find all the occurrences[/color]
        Do
            ReDim Preserve iFound(i)
            iFound(i) = Me.rtbMain.Find(myStr, iCont, RichTextBoxFinds.WholeWord)
            iCont = Me.rtbMain.SelectionStart + Len(myStr)
            i += 1
        Loop Until iCont > Len(Me.rtbMain.Text) Or Me.rtbMain.Find(myStr, iCont, RichTextBoxFinds.WholeWord) = -1

        [COLOR=green]'---Change all found words[/color]
        For j = 0 To iFound.Length - 1
            Me.rtbMain.Select(iFound(j), Len(myStr))
            Me.rtbMain.SelectionColor = Color.Blue
        Next

        [COLOR=green]'---Return focus to the box and cursor to[/color] 
        [COLOR=green]'---original position[/color]
        Me.rtbMain.Focus()
        Me.rtbMain.Select(iRet, 0)
    End Sub

Anybody have a better or different way to do this?

Charles
Quality Assurance/Developer
 
Also, if anybody has any ideas on how to (if this "blue" option was chosen) live color the text the same way the IDE in Visual Studio does.

Charles
Quality Assurance/Developer
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top