.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
Gaurav Pal
Post New Web Links

Underlining TextRange in WPF RichTextBox

Posted By:      Posted Date: April 10, 2011    Points: 0   Category :JQuery


I want to be able to underline a text range in a WPF RichTextBox. I'm aware that this could be done using .ApplyPropertyValue(Inline.TextDecorationsProperty, TextDecorations.Underline);

That is not what I'm interested in, though. I want to be able to underline in the same way that the built in WPF spell checker does - i.e. in a way that doesn't affect existing underlinings as such. What I mean is that if I use TextDecorations.Underline to do this, it replaces existing underlinings. I need to be able to add as well as remove the underlining without affecting existing styles. 

Does anyone have a suggestion? I've tried looking into System.Windows.Media, but I haven't been successful so far. Help would be greatly appreciated!


View Complete Post

More Related Resource Links

Printing RichtextBox

I was using the following code to print the contents of a richtextbox:         pd.PrintDocument(((CType(Me.richTextBox.Document, IDocumentPaginatorSource)).DocumentPaginator), "printing as paginator")but it prints in 3 columns. So i tried using this code but it does not print InluneUiContainersDim docWriter As XpsDocumentWriter = PrintQueue.CreateXpsDocumentWriter(ia)     If (docWriter IsNot Nothing And ia IsNot Nothing) Then         Dim paginator As DocumentPaginator = CType(fd, IDocumentPaginatorSource).DocumentPaginator         'Change the PageSize and PagePadding for the document to match the CanvasSize for the printer device.         paginator.PageSize = New Size(ia.MediaSizeWidth, ia.MediaSizeHeight)         Dim pagePadding As Thickness = fd.PagePadding         fd.PagePadding = New Thickness    (Math.Max    (ia.OriginWidth,     pagePadding.Left),     Math.Max    (ia.OriginHeight,     pagePadding.Top),         Math.Max(ia.MediaSizeWidth - (ia.OriginWidth + ia.ExtentWidth), pagePadding.Right), Math.Max(ia.MediaSizeHeight - (ia.OriginHeight + ia.ExtentHeight), pagePadding.Bottom))     fd.ColumnWidth = Double.PositiveInfinity     'Send DocumentPaginator to the printer.     docWriter.Write(paginator) End If Also tried the following but this is completely wrong:        pd.PrintVisual(CType(Me.richTextBox, Visual), "printing as visual")Any ideas?

RichTextBox - Attached Properties, Document and CaretPosition

Hi I have created a couple of attached properties for the richtextbox control, to enable binding to the Document and CaretPosition native properties of the richtextbox. However, I am finding that, when I am programmatically updating the richtextbox document from the view model (rather than typing it into the richtextbox directly), I need to manually set the CaretPosition property of the richtextbox (see the underlined piece of code for the additional manual setting of CaretPosition) whereas the Document property appears to be updating automatically. At least when I step the code and break in on I'm concerned that I've not quite wired this together properly and would be grateful if someone could explain where I've got it wrong. The relevant  XAML is  <RichTextBox x:Name="rtb" Height="auto" Width="auto" local:RichTextBoxExtensions.CaretPosition="{Binding CaretPosition, Mode=TwoWay}" local:RichTextBoxExtensions.CurrentText="{Binding CurrentText, Mode=TwoWay}" cal:Message.Attach="[Event TextChanged] = [Action CheckForPopup]; [Event PreviewKeyUp] = [Action KeyPress($eventArgs)]"> <local:RichTextBoxExtensions.Document> <MultiBinding Converter="{StaticResource IntellisenseController}"> <Binding Path="CurrentDocument" Mode=&qu

Full desired height of a RichTextBox control?

I don't mean the control's .Height property... ...I have a WinForm with a RichTextBox control, and another control underneath the RichTextBox control.  The RichTextBox control is anchored on the left, top, and right sides, so the width is automatically resized as the parent form is resized.  (it also has its Multiline and and WordWrap properties set to True) The item underneath the RichTextBox needs to always be right underneath it (little whitespace), and the RichTextBox needs to always display its entire text (nothing hidden). As I get the form's resize event, I'd like to update the RichTextBox's hight so all text is displayed, however I'm not seeing any properities or methods which would be helpful.  There is the PrefferredHeight property, but it is always returning 13. Basically, it comes dowen to this: when WordWrap is set to true, is there a way to see what the desired height is?  Or is there a way to see if any of the text is clipped? If not, I suppose my next step would be to roll my own custom control and draw the text myself, keeping track of the autowrap and line count, but I thought I'd post a message here first and see if anyone has a better idea. Thanks, Jeff      

double spin text in richtextbox

i want to implement this feature : if i have a following sentence : {my name is james vick and iam a {member|user|visitor} on this {forum|website|site} and iam loving it | iam admin and iam a {supervisor|admin|moderator} on this {forum|website|site} and iam loving it} on a click of button i want to generate this : my name is james vick and iam a member on this site and iam loving it or iam admin and iam a admin on this forum and iam loving it The basic need is to randomly choose words between {|} braces I have been able to do this for inner nest i.e. for {member|user|visitor}. But how to include the outer nest of sentences also i.e. {something blah blah {blah1|blah2} and | something else} ? code for inner nest : Private Sub SimpleButton3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SimpleButton3.Click Dim stringWithTextIn As String = RichTextBox1.Text Dim regex As New Regex("{(.*?)}") Dim r As String = regex.Replace(stringWithTextIn, New MatchEvaluator(AddressOf ReplaceMatch)) RichTextBox2.Text = r End Sub Private Function ReplaceMatch(ByVal m As Match) As String Dim parts = m.Groups(1).Value.Split("|"c) Return parts(r.Next(0, parts.Length)) End Function i know i just have to change the regex but what do i add in it to include outer braces?

missing event in RichTextBox

I'm developing an application that uses the RichTextBox extensively, I need an event that gets triggered at the moment when the vertical scroll bar of the richTectBox appears . so instead of scrolling down to see the rest of the text I want to create another RichTextBox that will hold the rest of the text which didn't fit in the first one …I want to achieve the same functionality as in MS word. I tried so hard to achieve that functionality but it wasn't successfull … I tried to fake it using the SizeChanged event but it wasn't practical because the size of the richTextBox gets changed a lot during the life time of the application   is there such event in windows forms ? … and if not , is there one in WPF ?

Problem when Append DataFormats.xamlPackage RichTextBox

public partial class Window12 : Window { public Window12() { InitializeComponent(); } FileStream fStream; private void btnClick_Click(object sender, RoutedEventArgs e) { TextRange ran = new TextRange(txtMsg.Document.ContentStart, txtMsg.Document.ContentEnd); ran.Save(fStream, DataFormats.XamlPackage); txtMsg.Document.Blocks.Clear(); txtMsg.Focus(); } private void Window_Loaded(object sender, RoutedEventArgs e) { string fileName = @"D:\\part1.xaml"; if (File.Exists(fileName)) fStream = new FileStream(fileName, FileMode.Append, FileAccess.Write); else fStream = new FileStream(fileName, FileMode.Create); txtMsg.Focus(); } private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e) { fStream.Close(); } } Hi, I have WPF RichTextbox and a button When I insert the text or Image and Click the button the text will save in D: drive with DataFormats.XamlPackage Initially it works fine, when I try to append the text (Close window and open again then insert the text )It gives the error in range.Save(fStream,DataFormats.XamlPackage); Error Message:Cannot open package because FileMode or FileAccess value is not valid for the stream.am

Possibly Severe Bug in Windows Forms / TextBox / RichTextBox, Windows Freezing

Problem: Windows Message Processing (in all running apps) completely freezing periodically for about 10 seconds when using certain combination of Forms properties on a form with a TextBox/RichTextBox. This behaviour might not be limited to the two controls. The weird behaviour starts with but is not limited to scrolling text in the textbox with the mousewheel. Using: Visual Studio 2008, Visual Basic, compiling for .NET Framework 3.5 (Any) OS: Windows XP SP3 Reproduce: Open new project, add a new form named TestForm, add a TextBox named TextBox1, enable scrollbars on the TextBox. Add the following function to the form: Public Class TestForm     Private Sub TestForm_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load         ' CONDITIONS WHEN STARTS TO SLOW DOWN:         ' Form.Opacity < 1.0         ' Form.BackColor = Form.TransparencyKey (= whole form is transparent)         ' this makes the form completely transparent         BackColor = Color.Red         TransparencyKey = Color.Red         ' NOTE: when this is set it starts to slow down         Opac

How can I hide the caret of a RichTextBox control?

Hello,Can you please tell me how can I hide the caret of a RichTextBox control completely while my app is running?Thanks.

Richtextbox - how can I find out what lines are currently visible?

I have a scrolling richtextbox that holds more lines than it can display, and I would like to know what lines are currently being displayed.  I'm looking for some method like GetTopVisibleLine, GetBottomVisibleLine, GetFirstVisibleCharIndex, GetLastVisibleCharIndex, etc. and I don't seem to be able to find anything that will give me this info.  Any ideas? Thanks, Mike

use icons for richtextbox

(using vb.net 2008).  I am using the richtextbox (WinForms, not WPF) to stream messages to the user.  I'd like to insert symbols with the messages, particularly the red exclamation point of ErrorProvider for warning messages (I'm not saying I need errorProvider itself, just like to use its symbol).  The symbols should position and size themselves appropriately depending upon the richtextbox font or style. I guess question 1 is, am I really on the right track using the WinForm richtextbox for this purpose, or is this functionality too difficult?  If not, how would I insert the errorprovider's exclamation point icon into the RTF?  

RichTextbox bold

hey, I am having trubble amkign the text in the richtextbox bold..I used this code: thisrichtextbox.SelectionFont.Bold = true; but it gives me this error: Property or indexer 'System.Drawing.Font.Bold' cannot be assigned to -- it is read only     Thanks:)

Textrange.Load not loading Xaml code into a FlowDocument


I'm trying to convert text with Xaml tags into a FlowDocument.  I've done this before and it worked fine (as far as I can recall - that was about a year ago.)  Now I can't get this to work:

private Span FormatString(String _textToFormat)
    FlowDocument _formattedDocument;
    Span _formattedText;

    _myString = "<FlowDocument xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\"><Paragraph>This is some <Run FontWeight=\"bold\">bold</Run> text.</Paragraph></FlowDocument>";

    _formattedDocument = new FlowDocument();
    _formattedText = new Span();

    TextRange textRange = new TextRange(_formattedDocument.ContentStart, _formattedDocument.ContentEnd);

    MemoryStream msDocument = new MemoryStream((new ASCIIEncoding()).GetBytes(_myString));
    if (textRange.CanLoad(DataFormats.Xaml))
        textRange.Load(msDocument, DataFormats.Xaml);

// rest of function trimmed - the problem is above

Things work fine up to and including the creation of the MemoryStream (inspection with the debugger shows that the bytes are in there.)  "textRange.CanLoad" also resolves to true, but textRange.Load(msDocument, DataFormats.Xaml) doesn't seem to do anything - after that, a look at "textRa

Removing first line/getting TextRange from TextBox



I have a plain TextBox and I need to remove the first line in it. Better than setting the Text property with whole new content I thought I would get a TextRange containing the first line and set its Text to empty string, but I cannot figure out how to get TextPointers from the TextBox.

Any idea how to get it or other way to do it?


Transparent Richtextbox

Anyone know how to make a RichTextBox have a transparent background?


RichTextBox - line number under the mouse cursot


Hi all,

How to get the number of the line, which is under the cursor in RichTextBox control.



WPF RichTextBox - saving images


Hi all.


1. Create RichTextBox control and type some text inside

2. Draw anything e.g. in the Paint

3. Copy some part of image to the clipboard

4. Paste it into the RTB (it appears correctly, all seems to be good)

5. Save contents with:

     MemoryStream Src = new MemoryStream();
     new TextRange(RTB.Document.ContentStart, RTB.Document.ContentEnd).Save(Src, System.Windows.DataFormats.Rtf);

6. Load it using:

     new TextRange(RTB.Document.ContentStart, RTB.Document.ContentEnd).Load(Src, System.Windows.DataFormats.Rtf);

7. Image is lost while text is still present.

The same story is with DataFormats.Xaml at least.

Can anybody say how to save RTF with image from WPF RTB? Thanx in advance.




ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  Silverlight  Others  All   

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend