7 people like it.

RichTextBox SQL highlighting

Simple RichTextBox SQL highlighting with win32 LockWindowUpdate DllImport

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
15: 
16: 
17: 
18: 
19: 
20: 
21: 
22: 
23: 
24: 
25: 
26: 
27: 
28: 
29: 
30: 
31: 
32: 
33: 
34: 
35: 
36: 
37: 
38: 
39: 
40: 
41: 
42: 
module SQL_Highlighing

open System.Runtime.InteropServices

module Lock =
    [<DllImport(@"User32", CharSet = CharSet.Ansi, SetLastError = false, ExactSpelling = true)>]
    extern void LockWindowUpdate(int hWnd)

open System.Text.RegularExpressions
open System.Drawing

type SyntaxRTB() = 
    inherit System.Windows.Forms.RichTextBox()

    override X.OnTextChanged(e : System.EventArgs) =
        base.OnTextChanged(e); X.ColorTheKeyWords()

    member X.ColorTheKeyWords() =
        let HL s c =
            let color(m : Match, color : Color) =
                X.SelectionStart    <- m.Index
                X.SelectionLength   <- m.Length
                X.SelectionColor    <- color
            Regex.Matches(X.Text, "\\b" + s + "\\b", RegexOptions.IgnoreCase) |> fun mx ->
                for m in mx do if (m.Success) then color(m,c)

        let SelectionAt = X.SelectionStart
        Lock.LockWindowUpdate(X.Handle.ToInt32())

        HL "(select)|(where)|(from)|(top)|(order)|(group)|(by)|(as)|(null)|(insert)|(exec)|(into)" Color.Blue
        HL "(join)|(left)|(inner)|(outer)|(right)|(on)" Color.Red
        HL "(and)|(or)|(not)" Color.DarkGreen
        HL "(case)|(when)|(then)|(else)|(end)|(if)|(begin)" Color.Teal
        HL "(cast)|(nvarchar)|(bit)|(datetime)|(int)|(table)" Color.BlueViolet
        HL "(datepart)" Color.DarkOrange
        HL "(avg)|(abs)|(max)|(min)" Color.DarkRed

        X.SelectionStart    <- SelectionAt
        X.SelectionLength   <- 0
        X.SelectionColor    <- Color.Black

        Lock.LockWindowUpdate(0)
module SQL_Highlighing
namespace System
namespace System.Runtime
namespace System.Runtime.InteropServices
Multiple items
type DllImportAttribute =
  inherit Attribute
  new : dllName:string -> DllImportAttribute
  val EntryPoint : string
  val CharSet : CharSet
  val SetLastError : bool
  val ExactSpelling : bool
  val PreserveSig : bool
  val CallingConvention : CallingConvention
  val BestFitMapping : bool
  val ThrowOnUnmappableChar : bool
  member Value : string

Full name: System.Runtime.InteropServices.DllImportAttribute

--------------------
DllImportAttribute(dllName: string) : unit
type CharSet =
  | None = 1
  | Ansi = 2
  | Unicode = 3
  | Auto = 4

Full name: System.Runtime.InteropServices.CharSet
field CharSet.Ansi = 2
val LockWindowUpdate : hWnd:int -> unit

Full name: SQL_Highlighing.Lock.LockWindowUpdate
Multiple items
val int : value:'T -> int (requires member op_Explicit)

Full name: Microsoft.FSharp.Core.Operators.int

--------------------
type int = int32

Full name: Microsoft.FSharp.Core.int

--------------------
type int<'Measure> = int

Full name: Microsoft.FSharp.Core.int<_>
val hWnd : int
namespace System.Text
namespace System.Text.RegularExpressions
namespace System.Drawing
Multiple items
type SyntaxRTB =
  inherit RichTextBox
  new : unit -> SyntaxRTB
  member ColorTheKeyWords : unit -> unit
  override OnTextChanged : e:EventArgs -> unit

Full name: SQL_Highlighing.SyntaxRTB

--------------------
new : unit -> SyntaxRTB
namespace System.Windows
namespace System.Windows.Forms
Multiple items
type RichTextBox =
  inherit TextBoxBase
  new : unit -> RichTextBox
  member AllowDrop : bool with get, set
  member AutoSize : bool with get, set
  member AutoWordSelection : bool with get, set
  member BackgroundImage : Image with get, set
  member BackgroundImageLayout : ImageLayout with get, set
  member BulletIndent : int with get, set
  member CanPaste : clipFormat:Format -> bool
  member CanRedo : bool
  member DetectUrls : bool with get, set
  ...

Full name: System.Windows.Forms.RichTextBox

--------------------
System.Windows.Forms.RichTextBox() : unit
val X : SyntaxRTB
override SyntaxRTB.OnTextChanged : e:System.EventArgs -> unit

Full name: SQL_Highlighing.SyntaxRTB.OnTextChanged
val e : System.EventArgs
Multiple items
type EventArgs =
  new : unit -> EventArgs
  static val Empty : EventArgs

Full name: System.EventArgs

--------------------
System.EventArgs() : unit
member SyntaxRTB.ColorTheKeyWords : unit -> unit
member SyntaxRTB.ColorTheKeyWords : unit -> unit

Full name: SQL_Highlighing.SyntaxRTB.ColorTheKeyWords
val HL : (string -> Color -> unit)
val s : string
val c : Color
val color : (Match * Color -> unit)
val m : Match
type Match =
  inherit Group
  member Groups : GroupCollection
  member NextMatch : unit -> Match
  member Result : replacement:string -> string
  static member Empty : Match
  static member Synchronized : inner:Match -> Match

Full name: System.Text.RegularExpressions.Match
val color : Color
type Color =
  struct
    member A : byte
    member B : byte
    member Equals : obj:obj -> bool
    member G : byte
    member GetBrightness : unit -> float32
    member GetHashCode : unit -> int
    member GetHue : unit -> float32
    member GetSaturation : unit -> float32
    member IsEmpty : bool
    member IsKnownColor : bool
    ...
  end

Full name: System.Drawing.Color
property System.Windows.Forms.TextBoxBase.SelectionStart: int
property Capture.Index: int
property System.Windows.Forms.RichTextBox.SelectionLength: int
property Capture.Length: int
property System.Windows.Forms.RichTextBox.SelectionColor: Color
Multiple items
type Regex =
  new : pattern:string -> Regex + 1 overload
  member GetGroupNames : unit -> string[]
  member GetGroupNumbers : unit -> int[]
  member GroupNameFromNumber : i:int -> string
  member GroupNumberFromName : name:string -> int
  member IsMatch : input:string -> bool + 1 overload
  member Match : input:string -> Match + 2 overloads
  member Matches : input:string -> MatchCollection + 1 overload
  member Options : RegexOptions
  member Replace : input:string * replacement:string -> string + 5 overloads
  ...

Full name: System.Text.RegularExpressions.Regex

--------------------
Regex(pattern: string) : unit
Regex(pattern: string, options: RegexOptions) : unit
Regex.Matches(input: string, pattern: string) : MatchCollection
Regex.Matches(input: string, pattern: string, options: RegexOptions) : MatchCollection
property System.Windows.Forms.RichTextBox.Text: string
type RegexOptions =
  | None = 0
  | IgnoreCase = 1
  | Multiline = 2
  | ExplicitCapture = 4
  | Compiled = 8
  | Singleline = 16
  | IgnorePatternWhitespace = 32
  | RightToLeft = 64
  | ECMAScript = 256
  | CultureInvariant = 512

Full name: System.Text.RegularExpressions.RegexOptions
field RegexOptions.IgnoreCase = 1
val mx : MatchCollection
property Group.Success: bool
val SelectionAt : int
module Lock

from SQL_Highlighing
property System.Windows.Forms.Control.Handle: nativeint
System.IntPtr.ToInt32() : int
property Color.Blue: Color
property Color.Red: Color
property Color.DarkGreen: Color
property Color.Teal: Color
property Color.BlueViolet: Color
property Color.DarkOrange: Color
property Color.DarkRed: Color
property Color.Black: Color
Raw view New version

More information

Link:http://fssnip.net/5L
Posted:6 years ago
Author:nCdy
Tags: winforms