Getox Posted September 18, 2005 Posted September 18, 2005 Is there anyway to highlight certain words/letters like If, Else, New and others? Quote Page Edit 2.0 Alpha 2 OUT NOW! - Download Now -
Diesel Posted September 18, 2005 Posted September 18, 2005 Syntax highlighting is complex. But, I suppose if you want a lightweight version, you could make a collection of all the keywords and check on keypress (or textchanged) to see if the word currently being typed in is a keyword, if so highlight it. Here is a very simple example: http://www.c-sharpcorner.com/Code/2003/June/SyntaxHighlightInRichTextBoxP2.asp Quote
Getox Posted September 18, 2005 Author Posted September 18, 2005 Um... I use windows forms... VB.net.... Quote Page Edit 2.0 Alpha 2 OUT NOW! - Download Now -
Diesel Posted September 18, 2005 Posted September 18, 2005 It's all the same 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 Quote
Nate Bross Posted September 18, 2005 Posted September 18, 2005 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.' // 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. Quote ~Nate� ___________________________________________ Please use the [vb]/[cs] tags on posted code. Please post solutions you find somewhere else. Follow me on Twitter here.
Nate Bross Posted September 18, 2005 Posted September 18, 2005 That's great, we posted at the same time, but you submitted first. Quote ~Nate� ___________________________________________ Please use the [vb]/[cs] tags on posted code. Please post solutions you find somewhere else. Follow me on Twitter here.
Diesel Posted September 18, 2005 Posted September 18, 2005 @Nate I swear your location 5 minutes ago said Chi-Town, now it says tempe Quote
Getox Posted September 18, 2005 Author Posted September 18, 2005 @Diesel: So how would i go about using that code on my current RichTextBox? Quote Page Edit 2.0 Alpha 2 OUT NOW! - Download Now -
Diesel Posted September 18, 2005 Posted September 18, 2005 Add an event handler AddHandler myRtf.TextChanged, AddressOf(this.TextChangedEvent) Quote
Nate Bross Posted September 18, 2005 Posted September 18, 2005 It did say Chi-Town, for Chicago. But I moved to Tempe Arizona three weeks ago and forgot to change it. I know...off topic... Quote ~Nate� ___________________________________________ Please use the [vb]/[cs] tags on posted code. Please post solutions you find somewhere else. Follow me on Twitter here.
Getox Posted September 19, 2005 Author Posted September 19, 2005 It's all the same 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.. Quote Page Edit 2.0 Alpha 2 OUT NOW! - Download Now -
Diesel Posted September 19, 2005 Posted September 19, 2005 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. Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.