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. Help with positioning text pointer in TextBox after formatting.

Help with positioning text pointer in TextBox after formatting.

Scheduled Pinned Locked Moved WPF
helptutorial
3 Posts 2 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

    Hello all, Basically i am trying to format single word in textbox. For eg. if user types 'Or' , 'and' i want to hilight them and bold them. This i am doing by extending textbox control and overriding OnRender. I could able to do that, but the problem is the cursor position is getting messup, in other words, when words are becoming bold cursor position is not getting adjusted accordingly, and am not sure how to do that. Here is my code protected override void OnRender(DrawingContext drawingContext) { if (this.Text != "") { this.Background = new SolidColorBrush(Colors.Transparent); FormattedText formattedText = new FormattedText( this.Text, System.Threading.Thread.CurrentThread.CurrentCulture, FlowDirection.LeftToRight, new Typeface(this.FontFamily,this.FontStyle, this.FontWeight,this.FontStretch), this.FontSize, Brushes.Black ); //Text that matches the textbox's double leftMargin = 4.0 + this.BorderThickness.Left; double topMargin = 2 + this.BorderThickness.Top; 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 List<Pair> ranges = dec.Ranges(this.Text); foreach (Pair p in ranges) { formattedText.SetForegroundBrush(Brushes.Blue, p.Start, p.Length); formattedText.SetFontWeight(FontWeights.Bold, p.Start, p.Length); } drawingContext.DrawText(formattedText, new Point(leftMargin - this.HorizontalOffset-2,topMargin)); } //base.OnRender(drawingContext); } Any help would be appreciated. Thanks.

    M 1 Reply Last reply
    0
    • U User 2322509

      Hello all, Basically i am trying to format single word in textbox. For eg. if user types 'Or' , 'and' i want to hilight them and bold them. This i am doing by extending textbox control and overriding OnRender. I could able to do that, but the problem is the cursor position is getting messup, in other words, when words are becoming bold cursor position is not getting adjusted accordingly, and am not sure how to do that. Here is my code protected override void OnRender(DrawingContext drawingContext) { if (this.Text != "") { this.Background = new SolidColorBrush(Colors.Transparent); FormattedText formattedText = new FormattedText( this.Text, System.Threading.Thread.CurrentThread.CurrentCulture, FlowDirection.LeftToRight, new Typeface(this.FontFamily,this.FontStyle, this.FontWeight,this.FontStretch), this.FontSize, Brushes.Black ); //Text that matches the textbox's double leftMargin = 4.0 + this.BorderThickness.Left; double topMargin = 2 + this.BorderThickness.Top; 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 List<Pair> ranges = dec.Ranges(this.Text); foreach (Pair p in ranges) { formattedText.SetForegroundBrush(Brushes.Blue, p.Start, p.Length); formattedText.SetFontWeight(FontWeights.Bold, p.Start, p.Length); } drawingContext.DrawText(formattedText, new Point(leftMargin - this.HorizontalOffset-2,topMargin)); } //base.OnRender(drawingContext); } Any help would be appreciated. Thanks.

      M Offline
      M Offline
      Mark Salsbery
      wrote on last edited by
      #2

      I'm not sure how you expect the TextBox to know about any formatted text you've rendered. TextBox doesn't support formatted text - RichTextBox does. Mark

      Mark Salsbery Microsoft MVP - Visual C++ :java:

      U 1 Reply Last reply
      0
      • M Mark Salsbery

        I'm not sure how you expect the TextBox to know about any formatted text you've rendered. TextBox doesn't support formatted text - RichTextBox does. Mark

        Mark Salsbery Microsoft MVP - Visual C++ :java:

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

        Thats correct, but rich text box is hell slow in formatting. I followed http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/3475ecef-4061-4317-99c6-cf759f69cd53[^] article. Thanks.

        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