Articles in this section
Category / Section

How to filter the IntelliSense in the WinForms EditControl?

4 mins read

This article explains how to filter the IntelliSense based on the typed characters in the WinForms EditControl.

You can easily filter the IntelliSense suggestions based on the typed characters by using the ContextChoiceOpen and KeyDown events of the EditControl and Form, respectively. These simple steps will demonstrate how to effectively filter and clear IntelliSense suggestions while typing in control.

 

Step 1: Create a configure language XML file for EditControl in the project.

 

Step 2: Add the lexems with the required words with Type as a keyword which is used for filtering the IntelliSense suggestions, and add additional requirements for the config language file.

 

XML

<ConfigLanguage name="CSharp" >
<!--Define the lexems-->.
<lexems>
  <!--Add the Keywords in the lexem-->.
 <lexem BeginBlock="ABORTIO" Type="KeyWord" />
 <lexem BeginBlock="ABORT" Type="KeyWord" />
 <lexem BeginBlock="ABS" Type="KeyWord" />
 <lexem BeginBlock="ACSH" Type="KeyWord" />
 <lexem BeginBlock="ACS" Type="KeyWord" />
 <lexem BeginBlock="ALLOCATE" Type="KeyWord" />
 <lexem BeginBlock="ALL" Type="KeyWord" />
 <lexem BeginBlock="ALPHA" Type="KeyWord" />
 <lexem BeginBlock="AND" Type="KeyWord" />
 <lexem BeginBlock="APPEND" Type="KeyWord" />
 <lexem BeginBlock="AREA" Type="KeyWord" />
 <lexem BeginBlock="ARG" Type="KeyWord" />
 <lexem BeginBlock="AS" Type="KeyWord" />  
 <lexem BeginBlock="." IsBeginRegex="true" Type="KeyWord"  DropContextChoiceList="true"/>
</lexems>
</ConfigLanguage>

 

Step 3: Declare a string property to get the XML file path of configure language, then use the Configurator.Open function to open the file for the EditControl.

 

C#

private string ConfigPath = Path.GetDirectoryName(Application.ExecutablePath) + @"\..\..\config.xml";
this.editControl1.Configurator.Open(ConfigPath);

VB

private string ConfigPath = Path.GetDirectoryName(Application.ExecutablePath) + @"\..\..\config.xml";
Me.editControl1.Configurator.Open(ConfigPath)

Step 4: Applying the appropriate language configuration for the EditControl by using the ApplyConfiguration method.

 

C#

//Apply the configuration in the SyntaxEditor.
this.editControl1.ApplyConfiguration("CSharp");

 

 VB

//Apply the configuration in the SyntaxEditor.
Me.editControl1.ApplyConfiguration("CSharp")

 

Step 5:  Hook the ContextChoiceOpen event for EditControl and KeyDown event for Form which is used to filter and clear the IntelliSence suggestions.

C#

//Create an instance for the EditControl in the Form.
EditControl editControl1 = new EditControl();
 
private string ConfigPath = Path.GetDirectoryName(Application.ExecutablePath) + @"\..\..\config.xml";
this.editControl1.Configurator.Open(ConfigPath);
this.editControl1.ApplyConfiguration("CSharp");
 
//Hook the ContextChoiceOpen event for the EditControl.
this.editControl1.ContextChoiceOpen += new ContextChoiceEventHandler(this.editControl1_ContextChoiceOpen);
 
//Hook the KeyDown event for the Form.
this.KeyDown += Form1_KeyDown;
 
this.Controls.Add(editControl1)

 

VB

//Create an instance for the EditControl in the Form.
Dim editControl1 As EditControl = New EditControl()
 
Private ConfigPath As String = Path.GetDirectoryName(Application.ExecutablePath) + "\..\..\config.xml"
Me.editControl1.Configurator.Open(ConfigPath)
Me.editControl1.ApplyConfiguration("CSharp")
 
//Hook the ContextChoiceOpen event of the EditControl.
Me.editControl1.ContextChoiceOpen += New ContextChoiceEventHandler(Me.editControl1_ContextChoiceOpen)
 
//Hook the KeyDown event of the Form.
Me.KeyDown += Form1_KeyDown
 
Me.Controls.Add(editControl1)

 

Step 6: Use LINQ query in the ContextChoiceOpen event method to filter IntelliSense suggestions by comparing the filter text stored in the string builder with the BeginBlock and KeyWord of the lexeme list in the EditControl, as shown in the following code sample.

C#

//Create a StringBuilder to store the filtering text by appending while typing the character.
System.Text.StringBuilder filterText = new System.Text.StringBuilder();
 
private void editControl1_ContextChoiceOpen(IContextChoiceController controller)
{
      //Get the ConfigLexem list from the edit control instance.
      List<ConfigLexem> newList = editControl1.Language.Lexems.Cast<ConfigLexem>().ToList();
      string dropperText = controller.Dropper.Text.ToUpper();
      filterText.Append(dropperText);
 
      if (dropperText != " ")
      {
           //LINQ query to filter the IntelliSense words.
           List<ConfigLexem> filteredList = newList.Where(L => L.TypeXML.Equals("KeyWord") && L.BeginBlock.StartsWith(filterText.ToString())).ToList<ConfigLexem>();
           foreach (ConfigLexem lexem in filteredList)
           { 
                //Add the filtered items in the IntelliSense. 
                controller.Items.Add(lexem.BeginBlock);
           }
       }
       else
       {
          filterText.Clear();
       }
}

 

VB

//Create a StringBuilder to store the filtering text by appending while typing the character.
Private filterText As System.Text.StringBuilder = New System.Text.StringBuilder()
    
Private Sub editControl1_ContextChoiceOpen(controller As Interfaces.IContextChoiceController) Handles editControl1.ContextChoiceOpen
//Get the ConfigLexem list from the edit control instance.
        Dim newList As List(Of ConfigLexem) = editControl1.Language.Lexems.Cast(Of ConfigLexem)().ToList()
        Dim dropperText As String = controller.Dropper.Text.ToUpper()
        filterText.Append(dropperText)
 
        If dropperText <> " " Then
            //LINQ query to filter the IntelliSense words.
            Dim filteredList As List(Of ConfigLexem) = newList.Where(Function(L) L.TypeXML.Equals("KeyWord") AndAlso L.BeginBlock.StartsWith(filterText.ToString())).ToList()
            For Each lexem As ConfigLexem In filteredList
            //Add the filtered items in the IntelliSense.
                controller.Items.Add(lexem.BeginBlock)
            Next
        Else
            filterText.Clear()
        End If
 
    End Sub

 

Step 7: In the KeyDown event method, validate the typed keys to update the filtering text for IntelliSense suggestions to be shown in the EditControl. The following code samples demonstrate how you can use the space and back keys to clear IntelliSense, and the enter key to select the IntelliSense suggestion with erase any typed previous characters.

C#

private void Form1_KeyDown(object sender, KeyEventArgs e)
{
       if (e.KeyCode == Keys.Space || e.KeyCode == Keys.Back)
       {
                filterText.Clear();
       }
      //Delete the typed text in the control while selecting the Intellisense text. 
       if (e.KeyCode == Keys.Enter)
       { 
            if (filterText.Length >= 1)
                {
                     for (int i = 0; i < filterText.Length ; i++)
                     {
                        this.editControl1.DeleteWordLeft();
                      }
                  filterText.Clear();
                 }
        }
}

 

VB

Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As KeyEventArgs)
        If e.KeyCode = Keys.Space OrElse e.KeyCode = Keys.Back Then
            filterText.Clear()
        End If
        //Delete the typed text in the control while selecting the Intellisense text.
        If e.KeyCode = Keys.Enter Then
            If Text.Length >= 1 Then
 
                For i As Integer = 0 To Text.Length - 1 Step i + 1
                    Me. editControl1.DeleteWordLeft()
                Next
                filterText.Clear()
            End If
        End If
    End Sub

 

Output:

 

Filtering the IntelliSense words in WinForms EditControl/SyntaxEditor.

Conclusion

I hope you enjoyed learning about how to filter the IntelliSense in the WinForms EditControl.

You can refer to our  WinForms EditControl feature tour page to know about its other groundbreaking feature representations. You can also explore our WinForms EditContro documentation to understand how to create and manipulate data.

For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls.

If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forumsDirect-Trac, or feedback portal. We are always happy to assist you!

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please  to leave a comment
Access denied
Access denied