Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. WPF
  4. Textbox Overriding OnRender and FormattedText problem [modified]

Textbox Overriding OnRender and FormattedText problem [modified]

Scheduled Pinned Locked Moved WPF
csharpdatabasewpfhelptutorial
2 Posts 1 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • U Offline
    U Offline
    User 2322509
    wrote on last edited by
    #1

    I created a custom textbox inheriting from wpf textbox and overriding OnRender. I used FormattedText to set background colors of few words in the textbox. Somehow the fonts are becoming bold, not sure why. How to get-rid of that? Here is my code. if (this.Text != "") { FormattedText formattedText = new FormattedText( this.Text, CultureInfo.InvariantCulture, FlowDirection.LeftToRight, new Typeface(this.FontFamily,this.FontStyle,this.FontWeight,this.FontStretch), this.FontSize, this.Foreground); //Text that matches the textbox's double leftMargin = 4.0 + this.BorderThickness.Left; double topMargin = 2 + this.BorderThickness.Top; formattedText.MaxTextWidth = this.ViewportWidth; // space for scrollbar formattedText.MaxTextHeight = Math.Max(this.ActualHeight + this.VerticalOffset, 0); //Adjust for scrolling drawingContext.PushClip(new RectangleGeometry(new Rect(0, 0, this.ActualWidth, this.ActualHeight)));//restrict text to textbox //Background hilight if (HighlightText != null && HighlightText.Length > 0) { foreach (string text in HighlightText) { int txtEnd = this.Text.Length; int index = 0; int lastIndex = this.Text.LastIndexOf(text, StringComparison.OrdinalIgnoreCase); while (index <= lastIndex) { index = this.Text.IndexOf(text, index, StringComparison.OrdinalIgnoreCase); Geometry geom = formattedText.BuildHighlightGeometry(new Point(leftMargin, topMargin - this.VerticalOffset), index, text.Length); if (geom != null) { drawingContext.DrawGeometry(Brushes.Yellow, null, geom); } index += 1; } } } drawingContext.DrawText(formattedText, new Point(leftMargin, topMargin - this.VerticalOffset)); } Thanks.

    modified on Tuesday, July 28, 2009 5:35 PM

    U 1 Reply Last reply
    0
    • U User 2322509

      I created a custom textbox inheriting from wpf textbox and overriding OnRender. I used FormattedText to set background colors of few words in the textbox. Somehow the fonts are becoming bold, not sure why. How to get-rid of that? Here is my code. if (this.Text != "") { FormattedText formattedText = new FormattedText( this.Text, CultureInfo.InvariantCulture, FlowDirection.LeftToRight, new Typeface(this.FontFamily,this.FontStyle,this.FontWeight,this.FontStretch), this.FontSize, this.Foreground); //Text that matches the textbox's double leftMargin = 4.0 + this.BorderThickness.Left; double topMargin = 2 + this.BorderThickness.Top; formattedText.MaxTextWidth = this.ViewportWidth; // space for scrollbar formattedText.MaxTextHeight = Math.Max(this.ActualHeight + this.VerticalOffset, 0); //Adjust for scrolling drawingContext.PushClip(new RectangleGeometry(new Rect(0, 0, this.ActualWidth, this.ActualHeight)));//restrict text to textbox //Background hilight if (HighlightText != null && HighlightText.Length > 0) { foreach (string text in HighlightText) { int txtEnd = this.Text.Length; int index = 0; int lastIndex = this.Text.LastIndexOf(text, StringComparison.OrdinalIgnoreCase); while (index <= lastIndex) { index = this.Text.IndexOf(text, index, StringComparison.OrdinalIgnoreCase); Geometry geom = formattedText.BuildHighlightGeometry(new Point(leftMargin, topMargin - this.VerticalOffset), index, text.Length); if (geom != null) { drawingContext.DrawGeometry(Brushes.Yellow, null, geom); } index += 1; } } } drawingContext.DrawText(formattedText, new Point(leftMargin, topMargin - this.VerticalOffset)); } Thanks.

      modified on Tuesday, July 28, 2009 5:35 PM

      U Offline
      U Offline
      User 2322509
      wrote on last edited by
      #2

      i found the mistake i was doing. Do not draw the text, which will draw on top the existing text of the textbox making it look bold. I removed drawingContext.DrawText(formattedText, new Point(leftMargin, topMargin - this.VerticalOffset)); from the logic and it worked fine.

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups