Category / Section
How to perform the language based Syntax highlighting in WinForms SyntaxEditor (EditControl)?
1 min read
Syntax highlighting
The SyntaxEditor (EditControl) supports Syntax highlighting for C#, VB, XML, HTML, Pascal and Delphi.
Please refer to the code snippets below code for how to customize the configuration settings at run-time.
C#
private void menuItem1_Click(object sender, System.EventArgs e) { try { IConfigLanguage activeLang = this.editControl1.Parser.Formats as IConfigLanguage; frmConfigDialog editConfig = new frmConfigDialog(this.editControl1.Configurator,activeLang); if(editConfig.ShowDialog(this) == DialogResult.OK && activeLang != null) { IConfigLanguage newLang = editConfig.Configurator.KnownLanguageNames.Contains(activeLang.Language)? editConfig.Configurator[ activeLang.Language ] : editConfig.Configurator.DefaultLanguage; if( newLang != null ) { this.editControl1.Configurator = editConfig.Configurator; this.editControl1.ResetColoring( newLang ); } } } catch( ArgumentException ex ) { Debug.WriteLine( ex.Message + Environment.NewLine + ex.StackTrace ); throw; } } private void menuItemCSharp_Click(object sender, System.EventArgs e) { this.editControl1.LoadFile("..\\..\\Test Files\\CSharpSource.cs"); } private void menuItemVB_Click(object sender, System.EventArgs e) { this.editControl1.LoadFile("..\\..\\Test Files\\VBSource.vb"); }
VB
Private Sub menuItem1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles menuItem1.Click Try Dim activeLang As IConfigLanguage = Me.editControl1.Parser.Formats ' Dim editConfig As New frmConfigDialog(Me.editControl1.Configurator, activeLang) If editConfig.ShowDialog(Me) = DialogResult.OK AndAlso Not (activeLang Is Nothing) Then Dim newLang As IConfigLanguage = IIf(editConfig.Configurator.KnownLanguageNames.Contains(activeLang.Language), editConfig.Configurator(activeLang.Language), editConfig.Configurator.DefaultLanguage) 'TODO: For performance reasons this should be changed to nested IF statements If Not (newLang Is Nothing) Then Me.editControl1.Configurator = editConfig.Configurator Me.editControl1.ResetColoring(newLang) End If End If Catch ex As ArgumentException Debug.WriteLine(ex.Message + Environment.NewLine + ex.StackTrace) Throw End Try End Sub Private Sub menuItemCsharp_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles menuItemCsharp.Click Me.editControl1.LoadFile("..\Test Files\CSharpSource.cs") End Sub Private Sub MenuItemVB_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItemVB.Click Me.editControl1.LoadFile("..\Test Files\VBSource.vb") End Sub
Reference link: https://help.syncfusion.com/windowsforms/syntaxeditor/syntax-highlighting