views:

893

answers:

2

Hi there,

As you can tell from the title, Im having a bit of issue regarding assigning and removing format styles to and from selected text in the RichTexBox control.

I know how to make text individually Bold/Italic/Underline, but not a combination of these. I know of ways that can achieve this character by character, but this would seem time-consuming on the interface. If it can be effortlessly done in Wordpad, Im sure it can be achieved here!

Is there no such method or such that exists that can allow me to "add" or "remove" a style from RichTextBox.SelectedFont?

+2  A: 

Unless I am completely misunderstanding the question

// Get the current text selection or to text entered after the insertion point. 
// Build new font based on the selection font, make it both Bold and Underline
// Apply new font to currently selected text (or for new text at insertion point

Font currFont = richTextBox.SelectionFont;
Font boldUnderFont = new Font(currFont, FontStyle.Bold | FontStyle.Underline);
richTextBox.SelectionFont = boldUnderFont;
BlueShepherd
Hey! Thanks for the reply! I haven't tried this yet, and whilst this looks good for adding styles, how would u remove, say "bold" from a selectedfont which has a style-combination of bold,underline and italics?
Shalan
Oh wait...ideally this can be used for both! Let me have a try at this and get back to u.
Shalan
To remove Bold ... Get the current font. AND it with a NOT on the Bold and reassign.
BlueShepherd
A: 

I am trying to change color and text style within richtextbox for selected text only. but it is not working pl. guide me my coading is as follows Dim selStart As Integer = RichTextBox1.SelectionStart Dim selLength As Integer = RichTextBox1.SelectionLength Dim FontSize As Integer = Val(combSize.Text) '14 Dim FontName As System.Drawing.Font = CType(combFont.SelectedValue, System.Drawing.Font) Dim currFont As System.Drawing.Font Dim fonts As New FontStyle If Me.RadioButton2.Checked Then '' fonts = FontStyle.Bold '' Else '' fonts = FontStyle.Regular '' End If End If

    If RichTextBox1.SelectionLength > 0 Then


        Dim i As Integer
        For i = 0 To selLength - 1
            ' select 1 char
            RichTextBox1.Select(selStart + i, 1)
            ' get the current font
            currFont = RichTextBox1.SelectionFont
            ' create and assign to the char a new font 
            ' with the new style

            RichTextBox1.SelectionFont = New Font(Font.Name, FontSize, currFont.Style)
        Next
        ' reselect the original selection
        RichTextBox1.Select(selStart, selLength)
    Else
        ' RichTextBox1.Font = Font(FontName, FontSize, fonts)
        ''RichTextBox1.SelectionFont = New Font(RichTextBox1.SelectionFont.Name, FontSize, RichTextBox1.SelectionFont.Style)
    End If
Parkash