This project is read-only.

[Solved]How to use different font with syntax highlighting?

Topics: User Forum
Apr 13, 2013 at 6:01 PM
First of all, sorry for my bad english.
I'm quite new using Scintilla and i have few problems with syntax highlighting.
The code i'm using is as follows:
// designer
this.scintilla.Font = new System.Drawing.Font("Courier New", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(162)));
this.scintilla.Indentation.TabWidth = 3;
this.scintilla.Indentation.UseTabs = false;
this.scintilla.Lexing.Lexer = ScintillaNET.Lexer.Cpp;
this.scintilla.Lexing.LexerName = "cpp";
// designer_end
void DocumentFormLoad(object sender, EventArgs e)
Scintilla.Lexing.Keywords[0] = "frame frame_end bmp_begin bmp_end";
Scintilla.Lexing.Keywords[2] = "bdy bdy_end wpoint wpoint_end itr itr_end cpoint cpoint_end bpoint bpoint_end";
Scintilla.Lexing.Keywords[1] = "centerx centery dvx dvy dvz x y w h row col pic state wait next name head face";
Scintilla.Styles[Scintilla.Lexing.StyleNameMap["NUMBER"]].ForeColor = System.Drawing.Color.Red;
Scintilla.Styles[Scintilla.Lexing.StyleNameMap["WORD"]].ForeColor = System.Drawing.Color.Blue;
Scintilla.Styles[Scintilla.Lexing.StyleNameMap["WORD2"]].ForeColor = System.Drawing.Color.Purple;
Scintilla.Styles[Scintilla.Lexing.StyleNameMap["WORD3"]].ForeColor = System.Drawing.Color.Green;
1) As you see font is set to Courier New in designer but when i execute the program, font is complete different. When i change the font after DocumentForm is loaded, font is changed but whole highlighting is gone. If i write down something after changing the font, syntax is reloaded but font is lost again. How can i work both highlighting and font?

2) Error happens in this expression:
Scintilla.Styles[Scintilla.Lexing.StyleNameMap["WORD3"]].ForeColor = System.Drawing.Color.Green;
It looks like the given key was not present in the dictionary. How can i use it or solve the problem with using a different key?
Apr 14, 2013 at 1:46 PM
Scintilla's Font property is a bit counter-intuitive. Basically, you have a choice - you can use the Font property, or you can use highlighting. All is not yet lost, however. You'll actually need to set the font within the Styles your are using. I'm currently in a bit of a rush, so I apologize for an incomplete answer, but I will come back to this page soon to try and give you some code.
Apr 14, 2013 at 10:36 PM
Ok, so here's how you would do it in code (assuming you don't want to use an XML file for styles):
Scintilla.Styles[Scintilla.Lexing.StyleNameMap["DEFAULT"]].Font = new System.Drawing.Font( _YOUR_FONT_HERE_ );
This will set the base font for the style mechanism in Scintilla to whatever font you choose.
Apr 15, 2013 at 4:17 PM
Edited Apr 15, 2013 at 4:22 PM
I tried to use this one:
Scintilla.Styles[Scintilla.Lexing.StyleNameMap["DOCUMENT_DEFAULT"]].FontName = Form1.Settings.font.Name;
Scintilla.Styles[Scintilla.Lexing.StyleNameMap["DOCUMENT_DEFAULT"]].Font = Form1.Settings.font;
Scintilla.Styles[Scintilla.Lexing.StyleNameMap["DEFAULT"]].FontName = Form1.Settings.font.Name;
Scintilla.Styles[Scintilla.Lexing.StyleNameMap["DEFAULT"]].Font = Form1.Settings.font;
Nothing changed...
I tried to use XML document for syntax highlighting but nothing was highlighted and the font was the one I wanned (Courier New):
<?xml version="1.0" encoding="utf-8" ?>
    <Language Name="dat">
        <Indentation TabWidth="3" UseTabs="false"/>
            <Keywords List="0">frame frame_end bmp_begin bmp_end</Keywords>
            <Keywords List="1">bdy bdy_end wpoint wpoint_end itr itr_end cpoint cpoint_end bpoint bpoint_end opoint opoint_end</Keywords>
            <Keywords List="2">centerx centery dvx dvy dvz x y w h row col pic state wait next name head small file</Keywords>
            <Style Name="DEFAULT" FontName="Courier New" Size="10"/>
            <Style Name="NUMBER" ForeColor="Red" BackColor="White"/>
            <Style Name="WORD" ForeColor="Blue" BackColor="White"/>
            <Style Name="WORD2" ForeColor="Purple" BackColor="White"/>
            <Style Name="WORD3" ForeColor="Green" BackColor="White"/>
I dont know if i'm missing something.

Thanks in Advance...


Highlighting works only if LexerName is defined:
Apr 15, 2013 at 10:15 PM
I was about to write you a post when I saw your edit. I assume that fixed your problem? Yes, you need to have the lexer name defined for highlighting to function.