This project is read-only.

Custom Styles

Topics: User Forum
Jul 30, 2012 at 8:09 PM

I have set up custom styling for specific words in the editor;  Code below using Regex to find the words to be styled;

int es = sci.EndStyled;
int mask = (1 << sci.StyleBits) - 1;

foreach (Match match in matches)
    int pos = match.Index;
    int len = match.Length;

    sci.StartStyling(pos, mask);
    sci.SetStyling(len, 0x000090);
    sci.StartStyling(es, mask);

sci.StartStyling(es, mask);



The issue is that as soon as a character is typed on the same line or the line below is empty the style is reset to default style; I have tried various arrangements of this code to similar effects; one is the opposite is is default styled until a character is added to the line;

The Highlight code is called on file open, file switch, and onSciCharAdded event for scintilla;

If someone could explain a bit better (read the scintilla and scintillaNET documentation on styles) how to achieve this that would be greatly appreciated

Jul 30, 2012 at 8:51 PM

Model your styling based from the INI lexer.

Jul 30, 2012 at 11:46 PM

Im using a different version. But is still doesnt adress teh issue fully. Example


JavaScript document code:

function HelloWorld()
    WFMessageBoxShow("Hello World!");

return 0;
The function call WFMessageBoxShow is highlighted properly; I put the cursor at the end of the line and press the space bar or any other letter. It returns to black (default style) until those characters are backspaced; What im looking for is an explanation of why the styling is "revoked" after i have style it when characters are added; even tho the styling code runs whenever a character is added to the control; if i change the C# code and do not pt in the startStyling() @ sci.EndStyled the last instance of the function call is never styled at all;

Jul 31, 2012 at 12:19 AM

It's because Scintilla is constantly restyling portions of the document. Its internal algorithms determine what parts of the document style are invalidated. Think of it like window painting with GDI. In order for your styles to work without getting stomped on you need to set the lexer to "container" and do all your styling in the StyleNeeded notification. The parameters in StyleNeeded tell you the range of text that needs to be styled. Any characters in this range that aren't styled when the notification is sent will revert to default.

Again look at the INI lexer as an example. Even if the syntax isn't the same the logic is exactly what you're trying to do.

Jul 31, 2012 at 1:42 AM

Ah this helps my understanding; Now i there a way to use this message (StylingNeeded) while using the build in JS lexer to do the normal styling, to just restyle portions of invalidated text? ie the method calls in question; i ask because it is more semantic highlighting that a constant syntax. in other words the names are not always the same. 

Jul 31, 2012 at 10:30 PM

Unfortunately no. If a Lexer is used Scintilla won't fire StyleNeeded. What you can do is:

  • modify the lexer and rebuild the native SciLexer.dll. I don't recommend this since it would create a rogue version of a fairly common dll.
  • Create your own lexer based off of JavaScript's (Is it still using cpp?). There's a lot involved to get everything just right but it's not impossible. 
  • I don't know if this will fill your needs but you can change the lexer's keywords list at any time. 
Jul 31, 2012 at 10:48 PM

Can one add keywords in the C# as appose to editing the XML file?

Jul 31, 2012 at 11:27 PM

sci.SetKeywords(0, keyWordList);

keyWordList is space delimited

Aug 1, 2012 at 12:15 AM

I see that in the XML there are different keyword lists and different styles is there a way to make a new style and a new keyword list that i can then dynamically add keywords to?


and thank you very much for all your help.

Aug 2, 2012 at 4:48 AM

I have created a suitable solution; The style used has to match one of the current styles for keyword lists, but i just used keyword list 1 (Types etc) and added words to the sci control when a JavaScript document was being edited; Thank you for all the help;