This project is read-only.

Text property and syntax hightlight

Topics: Developer Forum, User Forum
May 26, 2014 at 9:02 PM
Hi,

I´m changing the Scintilla.Text property in runtime, and it shows the corresponding text, however it doesn´t show the any syntax highlight, and the font is not Courrier New. If I paste the same code inside the control, it works, wich make me suspect that I´m changing the wrong property.

What should I do to set (and get) the component content programming?

Tks in advance,

Mauro Assis
May 27, 2014 at 3:14 PM
Something else must be going on besides just changing the Scintilla.Text property, because doing that wouldn't affect the current lexer and/or font settings. Are you sure you're not also changing the BackColor, ForeColor, Font, and/or other properties?


Jacob
May 27, 2014 at 3:46 PM
Jacob,

Thanks about your prompt answer.

Below, the code I wrote:
            ScintillaNET.Scintilla sne = new ScintillaNET.Scintilla();

            sne.ConfigurationManager.Language = "python";
            sne.Indentation.SmartIndentType = ScintillaNET.SmartIndent.Simple;
            sne.TabIndex = 4;
            sne.Whitespace.Mode = ScintillaNET.WhitespaceMode.VisibleAfterIndent;
            sne.Text = "import sys";  
            sne.Dock = DockStyle.Fill;

            pg.Controls.Add(sne);
            sne.Invalidate();
            tcScripts.SelectedTabPage = pg;
As you can see, I´m creating the Scintilla controle by code. I tryied the Invalidate(), and this is not updated as well. The "import sys" is for testing only.
As soon I type something in the control, it starts to work properly, that is, show the correct font and highlights sintax.

Mauro
May 27, 2014 at 5:27 PM
Interesting... I can't reproduce your issue. I used the exact code you pasted above and when I run it, the code is added to my Form and syntax highlighting is displayed immediately. I don't have to type something in the control to get it to work. I even tried nesting the Scintilla control in a TabControl as it appears you have also done to see if that could be an issue and everything still worked fine.

Here's a couple things you could try:
  • Make sure that your tab control or parent form isn't doing anything weird in the AddControl event (or whatever) that might be affecting your Scintilla control.
  • Try adding the Scintilla control to your parent first and then setting the Scintilla properties. i.e.
ScintillaNET.Scintilla sne = new ScintillaNET.Scintilla();
pg.Controls.Add(sne);

sne.ConfigurationManager.Language = "python";
sne.Indentation.SmartIndentType = ScintillaNET.SmartIndent.Simple;
sne.TabIndex = 4;
sne.Whitespace.Mode = ScintillaNET.WhitespaceMode.VisibleAfterIndent;
sne.Text = "import sys";  
sne.Dock = DockStyle.Fill;
sne.Invalidate();

tcScripts.SelectedTabPage = pg;
  • Change sne.Invalidate() to sne.Lexing.Colorize(). Calling this method will force Scintilla to rescan the text and reapply syntax highlighting.
Jacob
May 27, 2014 at 9:59 PM
Jacob,

Tks, it worked now. I just put pg.Controls.Add after sne creation, as you suggested above, and it´s ok. I remove the invalidate as well.

Tks again,

Mauro Assis
Atrium Forest
Oct 21, 2014 at 12:22 PM
I also had the same problem: if one changes the text property before the control is added to the parent, highlighting doesn't work. It is triggered by any editing action, but that is weird.

So, adding the control first and then changing properties works great.

Cheers,
Ivan