RichTextBox questions

It's all the same

Visual Basic:
    Private Sub TextChangedEvent(ByVal sender As Object, ByVal e As EventArgs)
        ' Calculate the starting position of the current line.
        Dim end As Integer = 0
        Dim start As Integer = 0
        start = (m_rtb.SelectionStart - 1)
        Do While (start > 0)
            If (m_rtb.Text(start) = vbLf) Then
                start = (start + 1)
                'TODO: Warning!!! break;If
            End If
            start = (start - 1)
        Loop
        ' Calculate the end position of the current line.
        end = m_rtb.SelectionStart
        Do While (end < m_rtb.Text.Length)
            If (m_rtb.Text(end) = vbLf) Then
                'TODO: Warning!!! break;If
            End If
            end = (end + 1)
        Loop
        ' Extract the current line that is being edited.
        Dim line As String = m_rtb.Text.Substring(start, (end - start))
        ' Backup the users current selection point.
        Dim selectionStart As Integer = m_rtb.SelectionStart
        Dim selectionLength As Integer = m_rtb.SelectionLength
        ' Split the line into tokens.
        Dim r As Regex = New Regex("([ \"& vbTab&"{}();])")
        Dim tokens() As String = r.Split(line)
        Dim index As Integer = start
        For Each token As String In tokens
            ' Set the token's default color and font.
            m_rtb.SelectionStart = index
            m_rtb.SelectionLength = token.Length
            m_rtb.SelectionColor = Color.Black
            m_rtb.SelectionFont = New Font("Courier New", 10, FontStyle.Regular)
            ' Check whether the token is a keyword. 
            Dim keywords() As String
            "public"
            "void"
            "using"
            "static"
            "class"
            
            Dim i As Integer = 0
            Do While (i < keywords.Length)
                If (keywords(i) = token) Then
                    ' Apply alternative color and font to highlight keyword. 
                    m_rtb.SelectionColor = Color.Blue
                    m_rtb.SelectionFont = New Font("Courier New", 10, FontStyle.Bold)
                    'TODO: Warning!!! break;If
                End If
                i = (i + 1)
            Loop
            index = (index + token.Length)
        Next
        ' Restore the users current selection point. 
        m_rtb.SelectionStart = selectionStart
        m_rtb.SelectionLength = selectionLength
    End Sub
 
I've said it before, and other people have said it too. 'If you look hard at C# code, it is very easy, time consuming yes, but easy to convert to vb.net.'

Visual Basic:
// Calculate the starting position of the current line.
int start = 0, end = 0;
for (start = m_rtb.SelectionStart - 1; start > 0; start--) {
if (m_rtb.Text[start] == '\n') { start++; break; }
}

// Calculate the end position of the current line.
for (end = m_rtb.SelectionStart; end < m_rtb.Text.Length; end++) {
if (m_rtb.Text[end] == '\n') break;
} 

Converted to VB

' Calculate the starting position of the current line.
Dim start As Integer =  0,end As Integer =  0 
For start = m_rtb.SelectionStart - 1 To  0- 1  Step  start - 1
If m_rtb.Text(start) = vbCrLf Then
	 start = start + 1
End If
Next
 
' Calculate the end position of the current line.
For end = m_rtb.SelectionStart To  m_rtb.Text.Length- 1  Step  end + 1
If m_rtb.Text(end) = vbCrLf Then
	 Exit For
End If
Next

I haven't got time to convert the entire thing, but you get the idea.
 
Diesel said:
It's all the same

Visual Basic:
    Private Sub TextChangedEvent(ByVal sender As Object, ByVal e As EventArgs)
        ' Calculate the starting position of the current line.
        Dim end As Integer = 0
        Dim start As Integer = 0
        start = (m_rtb.SelectionStart - 1)
        Do While (start > 0)
            If (m_rtb.Text(start) = vbLf) Then
                start = (start + 1)
                'TODO: Warning!!! break;If
            End If
            start = (start - 1)
        Loop
        ' Calculate the end position of the current line.
        end = m_rtb.SelectionStart
        Do While (end < m_rtb.Text.Length)
            If (m_rtb.Text(end) = vbLf) Then
                'TODO: Warning!!! break;If
            End If
            end = (end + 1)
        Loop
        ' Extract the current line that is being edited.
        Dim line As String = m_rtb.Text.Substring(start, (end - start))
        ' Backup the users current selection point.
        Dim selectionStart As Integer = m_rtb.SelectionStart
        Dim selectionLength As Integer = m_rtb.SelectionLength
        ' Split the line into tokens.
        Dim r As Regex = New Regex("([ \"& vbTab&"{}();])")
        Dim tokens() As String = r.Split(line)
        Dim index As Integer = start
        For Each token As String In tokens
            ' Set the token's default color and font.
            m_rtb.SelectionStart = index
            m_rtb.SelectionLength = token.Length
            m_rtb.SelectionColor = Color.Black
            m_rtb.SelectionFont = New Font("Courier New", 10, FontStyle.Regular)
            ' Check whether the token is a keyword. 
            Dim keywords() As String
            "public"
            "void"
            "using"
            "static"
            "class"
            
            Dim i As Integer = 0
            Do While (i < keywords.Length)
                If (keywords(i) = token) Then
                    ' Apply alternative color and font to highlight keyword. 
                    m_rtb.SelectionColor = Color.Blue
                    m_rtb.SelectionFont = New Font("Courier New", 10, FontStyle.Bold)
                    'TODO: Warning!!! break;If
                End If
                i = (i + 1)
            Loop
            index = (index + token.Length)
        Next
        ' Restore the users current selection point. 
        m_rtb.SelectionStart = selectionStart
        m_rtb.SelectionLength = selectionLength
    End Sub
This code dont work in vb.net..
 
I translated it with a tool.
Shouldn't be that hard to fix it up man!

Change this
Dim keywords() As String
to this

Dim keywords() As String = {"public","void","using", "static", "class"}

and fix whatever else is wrong.
 
Back
Top