)
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 use Autoindent and AutoReplaceTrigger functionalities in WinForms SyntaxEditor (EditControl)?

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

EditControl functionalities

It is possible to turn on auto-indentation by setting AutoIndentMode property to Smart and by setting the value of Indent attribute of Lexem tag to true for that lexem in the XML configuration file.

To trigger AutoReplaceTrigger when the keyword is indentended, then you have to handle KeyUp event and replace the old text then insert the new one.

C#

this.editControl1.UseAutoreplaceTriggers = true;
this.editControl1.Language.AutoReplaceTriggers.AddRange(new AutoReplaceTrigger[] { new AutoReplaceTrigger("endif", "endIf") });
void editControl1_KeyUp(object sender, KeyEventArgs e)
{
if (((this.editControl1.GetLine(this.editControl1.CurrentLine)).FindLexemByColumn(this.editControl1.CurrentColumn - 1)) != null)
{
foreach (AutoReplaceTrigger trigger in this.editControl1.Language.AutoReplaceTriggers)
{
ILexem lem= this.editControl1.GetLine(this.editControl1.CurrentLine).FindLexemByColumn(this.editControl1.GetCurrentWordColumn());
Console.WriteLine(lem.Text);
// If the current word has to be replaced
if (trigger.From == ((this.editControl1.GetLine(this.editControl1.CurrentLine)).FindLexemByColumn((this.editControl1.GetCurrentWordColumn()))).Text && this.editControl1.AutoIndentGuideline)
{
//Delete the word
this.editControl1.DeleteText(this.editControl1.Parser.GetCoordinatePoint(this.editControl1.CurrentLine, this.editControl1.GetCurrentWordColumn()), this.editControl1.Parser.GetCoordinatePoint(this.editControl1.CurrentLine, (((((this.editControl1.GetLine(this.editControl1.CurrentLine)).FindLexemByColumn(this.editControl1.GetCurrentWordColumn())).Text).Length) + this.editControl1.GetCurrentWordColumn())));
//Insert the word
this.editControl1.InsertText(this.editControl1.CurrentLine, this.editControl1.CurrentColumn, trigger.To);
int start = this.editControl1.CurrentColumn - lem.Text.Length;
CoordinatePoint _EndPoint = this.editControl1.Parser.GetCoordinatePoint(this.editControl1.CurrentLine, start);
CoordinatePoint _StartPoint = this.editControl1.Parser.GetCoordinatePoint(this.editControl1.CurrentLine, start - 1);
this.editControl1.DeleteText(_StartPoint, _EndPoint);
this.editControl1.CurrentColumn = this.editControl1.CurrentColumn + lem.Text.Length;
break;
}
}
}

 

VB

Me.editControl1.UseAutoreplaceTriggers = True
Me.editControl1.Language.AutoReplaceTriggers.AddRange(New AutoReplaceTrigger() { New AutoReplaceTrigger("endif", "endIf") })
Private Sub editControl1_KeyUp(ByVal sender As Object, ByVal e As KeyEventArgs)
If Not ((Me.editControl1.GetLine(Me.editControl1.CurrentLine)).FindLexemByColumn(Me.editControl1.CurrentColumn - 1)) Is Nothing Then
For Each trigger As AutoReplaceTrigger In Me.editControl1.Language.AutoReplaceTriggers
Dim lem As ILexem= Me.editControl1.GetLine(Me.editControl1.CurrentLine).FindLexemByColumn(Me.editControl1.GetCurrentWordColumn())
If trigger.From = ((Me.editControl1.GetLine(Me.editControl1.CurrentLine)).FindLexemByColumn((Me.editControl1.GetCurrentWordColumn()))).Text AndAlso Me.editControl1.AutoIndentGuideline Then
Me.editControl1.DeleteText(Me.editControl1.Parser.GetCoordinatePoint(Me.editControl1.CurrentLine, Me.editControl1.GetCurrentWordColumn()), Me.editControl1.Parser.GetCoordinatePoint(Me.editControl1.CurrentLine, (((((Me.editControl1.GetLine(Me.editControl1.CurrentLine)).FindLexemByColumn(Me.editControl1.GetCurrentWordColumn())).Text).Length) + Me.editControl1.GetCurrentWordColumn())))
''Insert the word
Me.editControl1.InsertText(Me.editControl1.CurrentLine, Me.editControl1.CurrentColumn, trigger.To)
Dim start As Integer = Me.editControl1.CurrentColumn - lem.Text.Length
Dim _EndPoint As CoordinatePoint = Me.editControl1.Parser.GetCoordinatePoint(Me.editControl1.CurrentLine, start)
Dim _StartPoint As CoordinatePoint = Me.editControl1.Parser.GetCoordinatePoint(Me.editControl1.CurrentLine, start - 1)
Me.editControl1.DeleteText(_StartPoint, _EndPoint)
Me.editControl1.CurrentColumn = Me.editControl1.CurrentColumn + lem.Text.Length
Exit For
End If
Next trigger
End If
End Sub

 

Reference links:

  1. https://help.syncfusion.com/windowsforms/syntax-editor/syntax-highlighting#auto-replace-triggers
  2. https://help.syncfusion.com/windowsforms/syntax-editor/editing#auto-indentation

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