Category / Section
How to highlight comment statements with unlimited levels of nesting in WinForms SyntaxEditor (EditControl)?
2 mins read
Highlight the comment
Comment statements with unlimited levels of nesting can be highlighted by using the configuration file or by handling the TextChanging event of the EditControl. Please refer to the attached samples for more information.
XML
OnlyLocalSublexems="true" Type="comment">
C#
private void editControl1_TextChanging(object sender, Syncfusion.Windows.Forms.Edit.TextChangingEventArgs e) { if(e.Type==Syncfusion.Windows.Forms.Edit.Enums.TextChange.Inserted) { if(commentopencount!=0) { commentendingposition=this.editControl1.CurrentPosition; this.editControl1.SetTextColor(this.editControl1.ConvertVirtualPointToCoordinatePoint (commentstartingposition),this.editControl1.ConvertVirtualPointToCoordinatePoint(commentendingposition),Color.Green); } //Increment the number of comments open if "(*" has been typed //and decrement the number of comments if "*)" has been typed. charactercount++; if(e.Text=="*") { if((startingcharacter+1)==charactercount) { commentopencount++; if(commentopencount==1) { this.editControl1.SetTextColor(this.editControl1.ConvertVirtualPointToCoordinatePoint (commentstartingposition),this.editControl1.ConvertVirtualPointToCoordinatePoint(this.editControl1.CurrentPosition),Color.Green); } } else { endingcharacter=charactercount; } } if(e.Text=="(") { startingcharacter=charactercount; if(commentopencount==0) commentstartingposition=this.editControl1.CurrentPosition; } if(e.Text==")") { if((endingcharacter+1)==charactercount) { if(commentopencount>0) { commentopencount--; } } } } else if(e.Type==Syncfusion.Windows.Forms.Edit.Enums.TextChange.Deleted) { //Increment the number of comments open if "*)" has been deleted //and decrement the number of comments if "(*" has been deleted. if(e.Text=="*") { if((charactercount+1)==endingcharacter) { commentopencount++; endingcharacter=0; } else { startingcharacter=charactercount; if(commentopencount<2) this.editControl1.SetTextColor(this.editControl1.ConvertVirtualPointToCoordinatePoint (commentstartingposition),this.editControl1.ConvertVirtualPointToCoordinatePoint(this.editControl1.CurrentPosition),Color.Black); } } if(e.Text=="(") { if((charactercount+1)==startingcharacter) { startingcharacter=-1; commentopencount--; } } if(e.Text==")") { { endingcharacter=charactercount; } } if(charactercount>0) charactercount--; } }
VB
Private Sub editControl1_TextChanging(ByVal sender As Object, ByVal e As Syncfusion.Windows.Forms.Edit.TextChangingEventArgs) Handles editControl1.TextChanging If e.Type=Syncfusion.Windows.Forms.Edit.Enums.TextChange.Inserted Then If commentopencount<>0 Then commentendingposition=Me.editControl1.CurrentPosition Me.editControl1.SetTextColor(Me.editControl1.ConvertVirtualPointToCoordinatePoint(commentstartingposition),Me.editControl1.ConvertVirtualPointToCoordinatePoint(commentendingposition),Color.Green) End If ' Increment the number of comments open if "(*" has been typed ' and decrement the number of comments if "*)" has been typed. charactercount += 1 If e.Text="*" Then If (startingcharacter+1)=charactercount Then commentopencount += 1 If commentopencount=1 Then Me.editControl1.SetTextColor(Me.editControl1.ConvertVirtualPointToCoordinatePoint(commentstartingposition),Me.editControl1.ConvertVirtualPointToCoordinatePoint(Me.editControl1.CurrentPosition),Color.Green) End If Else endingcharacter=charactercount End If End If If e.Text="(" Then startingcharacter=charactercount If commentopencount=0 Then commentstartingposition=Me.editControl1.CurrentPosition End If End If If e.Text=")" Then If (endingcharacter+1)=charactercount Then If commentopencount>0 Then commentopencount -= 1 End If End If End If Else If e.Type=Syncfusion.Windows.Forms.Edit.Enums.TextChange.Deleted Then ' Increment the number of comments open if "*)" has been deleted ' and decrement the number of comments if "(*" has been deleted. If e.Text="*" Then If (charactercount+1)=endingcharacter Then commentopencount += 1 endingcharacter=0 Else startingcharacter=charactercount If commentopencount<2 Then Me.editControl1.SetTextColor(Me.editControl1.ConvertVirtualPointToCoordinatePoint(commentstartingposition),Me.editControl1.ConvertVirtualPointToCoordinatePoint(Me.editControl1.CurrentPosition),Color.Black) End If End If End If If e.Text="(" Then If (charactercount+1)=startingcharacter Then startingcharacter=-1 commentopencount -= 1 End If End If If e.Text=")" Then endingcharacter=charactercount End If If charactercount>0 Then charactercount -= 1 End If End If End Sub