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