)
We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. (Last updated on: June 24, 2019).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

How to highlight comment statements with unlimited levels of nesting in WinForms SyntaxEditor (EditControl)?

Platform: WinForms |
Control: EditControl |
Published Date: April 30, 2011 |
Last Revised Date: December 12, 2019

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

 

2X faster development

The ultimate WinForms UI toolkit to boost your development speed.
ADD COMMENT
You must log in to leave a comment

Please sign in to access our KB

This page will automatically be redirected to the sign-in page in 10 seconds.

Up arrow icon

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

Live Chat Icon For mobile
Live Chat Icon