Articles in this section
Category / Section

How to create a VS.NET like code insertion application using WinForms SyntaxEditor (EditControl)?

2 mins read

Insertion feature

Since there is no direct way of accessing the DOM object associated with the file contained in the EditControl, it cannot identify the classes and its APIs contained in the file dynamically. The names of the classes and its APIs (at least regular expressions specifying them) have to be specified in the configuration file used the EditControl. This configuration file is then plugged into the EditControl and used for syntax highlighting its contents.

Please refer to the test sample attached below. In this sample, the following format and lexem definitions have been specified to identify the method, property and event handler names.

<format name="EventHandler" Font="Courier New, 10pt, style=Bold" FontColor="Orange" />
<format name="MethodName" Font="Courier New, 10pt, style=Bold" FontColor="Red" />
<format name="PropertyName" Font="Courier New, 10pt, style=Bold" FontColor="Khaki" />
<lexem BeginBlock="CustomEventHandler[0-9]*" IsBeginRegex="true" Type="EventHandler"/>
<lexem BeginBlock="Method[0-9]*" IsBeginRegex="true" Type="MethodName"/>
<lexem BeginBlock="Property[0-9]*" IsBeginRegex="true" Type="PropertyName"/>

 

A sample file - test.cs containing a simple class TestClass has been loaded into the EditControl. Presently this sample contains the auto-code insertion feature for the event handlers only. This can be easily extended for the methods and properties as well. We have used an external configuration file – CustomConfig.xml which is an extended version of the config file used to highlight C#. I have plugged this into the EditControl using the following code:

C#

this.editControl1.Configurator.Open(configFilePath);
this.editControl1.ApplyConfiguration("CustomLanguage");

 

VB

Me.editControl1.Configurator.Open(configFilePath)
Me.editControl1.ApplyConfiguration("CustomLanguage")

 

Please follow the steps below to test this sample.

1. Run the sample.

2. From the combobox, select any of the event handlers – for example: CustomEventHandler3. Note that the EditControl scrolls the appropriate line into view and inserts some custom code.

3. Now type some event handler name into the combobox – for example: CustomEventHandler4. Note that the EditControl correctly inserts an event handler block with the specified name.

The only limitation here is that the names of the event handlers should match the regular expression specified – CustomEventHandler[0-9]*. You could support names of any format by specifying an appropriate regular expression.

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