Google Translate

Valutazione attuale: 5 / 5

Stella attivaStella attivaStella attivaStella attivaStella attiva

If a word starts with # or @ or other special char like $, your autocomplete search fails? Here a workaround to fix it.

Brief Story

As reported from the developer website, ScintillaNET is a powerful text editing control for Windows Forms applications and a managed wrapper around the versatile Scintilla component. And what is Scintilla?

Let me still report a sentence from the scintilla website: "Scintilla is a free source code editing component. It comes with complete source code and a license that permits use in any free project or commercial product."

A lot of editors use it. Wikipedia reports a short list of 24 applications but has a big and complete list of them. By the way, the most well known is Notepad++.

ScintillaNET,  autocomplete and special chars

Returning to the main argument, I use ScintillaNET in a project as editor for SQL script and also as the extension editor for the datagridview cells.

I had a bug, the autocomplete was not working properly for words that start with special chars like: @, #, $.

I spent wasted time searching on the internet for a solution but I found nothing for my specific case.I read the documentation, some ScintillaNET code sources.

After a lot tries I found a workaround applying some changes in my CharAdded() event code.

I noted that passing a length + 1 in the, the search starts to work but now only if char starts with special chars...let me say lol. So I tried to read the word at the position - 1 to see if it starts with a special char, if yes I add 1 to length.

That's all.

EVENT CharAdded()

	' FIX AutoComplete
	' - Now autocomple works with words start with special chars (@,#)
	' - .WordStartPosition(pos, False)
	'   False allows any char but is not enough to fix the issue	'
	' - .AutoComplete.Show(length)
	'   It needs length and also needs lenght + 1 if word starts with special char

   Private Sub Scintilla1_CharAdded(sender As Object, e As CharAddedEventArgs) Handles Scintilla1.CharAdded

        ' Check Keywords List
        If sender.AutoComplete.List.Count < 0 Then
            Exit Sub
        End If

        'Logic to ensure it's shown correctly
        Dim pos As Integer = sender.NativeInterface.GetCurrentPos()
        Dim length As Integer = pos - sender.NativeInterface.WordStartPosition(pos, False)  '  FIX Autocomplete - false=Any Char

        'Cancel if the length is zero
        If length = 0 Then
            Exit Sub
        End If
        ' FIX Autocomplete - now search works when word start with special char...
		Dim testWord As String = sender.GetRange(sender.NativeInterface.WordStartPosition(pos, False) - 1, pos).Text
        If testWord.StartsWith("#") Or testWord.StartsWith("@") Or testWord.StartsWith("$") Then
            length = length + 1
        End If

        ' Show the autocomplete window (pupup)
        sender.AutoComplete.Show(length) 'length + 1 if word start with sepcial chars
    End Sub


 - have fun -