Articles in this section
Category / Section

How to use Autoindent and AutoReplaceTrigger functionalities in WinForms SyntaxEditor (EditControl)?

1 min read

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
Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please sign in to leave a comment
Access denied
Access denied