Find/Replace matched item font's color

Sep 4, 2008 at 12:53 AM
Hi,

Is there a way to change the font's foreground/background color of a word that have been found by the Find/Replace dialog box? Right now when you click Find or Find Next and if a word is found, its color is White on a Gray background which is very hard to pick out of the whole document. If someone would just point me in the right direction, i'd change the code myself.

Thank you
Coordinator
Sep 4, 2008 at 8:14 PM
What you want to do is set the Selection.ForeColorUnfocused and Selection.BackColorUnfocused on the Scintilla control. This can be done in the designer.
Sep 5, 2008 at 3:26 AM
Thanks Chris for your answer. I did what you said and it does work but only half way. I need to change a line in Selection.cs for both of the property ForeColorUnfocused and BackColorUnfocused before it would work. I may have mess-up your logic but it does work. Below is the snippet of code, with --- and +++ marking the lines.

        public Color ForeColorUnfocused
        {
            get
            {
                if (Scintilla.ColorBag.ContainsKey("Selection.ForeColorUnfocused"))
                    return Scintilla.ColorBag["Selection.ForeColorUnfocused"];

                return SystemColors.HighlightText;
            }
            set
            {
                if (value == ForeColorUnfocused)
                    return;

                if (ForeColorUnfocused == SystemColors.HighlightText)
                {
                    ---Scintilla.ColorBag.Remove("Selection.ForeColorUnfocused");
                    +++Scintilla.ColorBag["Selection.ForeColorUnfocused"] = value;
                }
                else
                {
                    Scintilla.ColorBag["Selection.ForeColorUnfocused"] = value;
                }
               
                if(!Scintilla.ContainsFocus)
                    NativeScintilla.SetSelFore(true, Utilities.ColorToRgb(value));
            }
        }


        public Color BackColorUnfocused
        {
            get
            {
                if (Scintilla.ColorBag.ContainsKey("Selection.BackColorUnfocused"))
                    return Scintilla.ColorBag["Selection.BackColorUnfocused"];

                return Color.LightGray;
            }
            set
            {
                if (value == BackColorUnfocused)
                    return;

                if (BackColorUnfocused == Color.LightGray) {
                    ---Scintilla.ColorBag.Remove("Selection.BackColorUnfocused");
                    +++Scintilla.ColorBag["Selection.BackColorUnfocused"] = value;
                } else {
                    Scintilla.ColorBag["Selection.BackColorUnfocused"] = value;
                }
               
                if(!Scintilla.ContainsFocus)
                    NativeScintilla.SetSelBack(true, Utilities.ColorToRgb(value));
            }
        }
Coordinator
Sep 5, 2008 at 3:47 AM
I got the fix in, I didn't use your code but you certainly put me in the right direction. The respective if ([PropertyName] == [DefaultValue]) is supposed to be if (value == [DefaultValue]). So the whole logic behind this is that we don't need to store the color in the colorbag if it's the default to save memory. I also noticed that ForeColor and BackColor also suffered from this bug.
Sep 5, 2008 at 5:27 AM
Ah, make perfect sense. I was looking at the code and asking myself, now WTH is that supposed to mean? Might as well remove the whole if...else statement. But i figured, it worked and also when you give it a glance, you would know what to do next :-)