Sunday 8 May 2011

Rich Text Box Control

Rich TextBox Control
     Like the normal TextBox, the RichTextBox control is derived from TextBoxBase. Because of this, it shares a number of features with the TextBox, but is much more diverse. Where a TextBox is commonly used with the purpose of obtaining short text strings from the user, the RichTextBox is used to display and enter formatted text (for example bold, underline, and italic). It does so using a standard for formatted text called Rich Text Format or RTF.
RichTextBox Properties
Name
Availability
Description
CanRedo Read only This property is true if something has
been undone, that can be reapplied,
otherwise false.
CanUndo Read only This property is true if it is possible to perform an undo action on the RichTextBox, otherwise it is false.
RedoActionName Read only This property holds the name of an action that be used to redo something that has been undone in the RichTextBox.
DetectUrls Read/Write Set this property to true to make the control detect URLs and format them (underline as in
a browser).
Rtf Read/Write This corresponds to the Text property, except that this holds the text in RTF.
SelectedRtf Read/Write Use this property to get or set the selected text in the control, in RTF. If you copy this text to another application, for example, MS Word, it will retain all formatting.
SelectedText Read/Write Like SelectedRtf you can use this property to get or set the selected text. Unlike the RTF version of the property however, all formatting is lost.
SelectionAlignment Read/Write This represents the alignment of the selected text. It can be Center, Left, or Right.
SelectionBullet Read/Write Use this property to find out if the selection is formatted with a bullet in front of it, or use it to insert or remove bullets.
BulletIndent Read/Write Use this property to specify the number of pixels a bullet should be indented.
SelectionColor Read/Write Allow you to change the color of the text in
the selection.
SelectionFont Read/Write Allow you to change to font of the text in the selection.
SelectionLength Read/Write Using this property, you either set or retrieve the length of a selection.
SelectionType Read only This property holds information about the selection. It will tell you if one or more OLE objects are selected or if only text is selected.
ShowSelectionMargin Read/Write If you set this property to true, a margin will be shown at the left of the RichTextBox. This will make it easier for the user to select text.
UndoActionName Read only Gets the name of the action that will be used if the user chooses to undo something.
SelectionProtected Read/Write You can specify that certain parts of the text should not be changed by setting this property
to true.
RichTextBox Events
Name
Description
LinkedClick This event is sent when a user clicks on a link within the text.
Protected This event is sent when a user attempts to modify text that has been marked as protected.
SelectionChanged This event is sent when the selection changes. If for some reason you don't want the user to change the selection, you can prevent the change here.
Example of Rich TextBox: To implement rich textbox create new application and design form like below image, and change properties according to your choice.
                                            
In following image i am making selected text bold on clicking the bold Button. Coding for bold button is given below.
                                             
Code for Bold Button
 


 private void button1_Click(object sender, EventArgs e)
        {
            Font oldFont;
            Font newFont;

            // Get the font that is being used in the selected text

            oldFont = this.richTextBox1.SelectionFont;

            // If the font is using bold style now, we should remove the

            // Formatting

            if (oldFont.Bold)
                newFont = new Font(oldFont, oldFont.Style & ~FontStyle.Bold);
            else
                newFont = new Font(oldFont, oldFont.Style | FontStyle.Bold);

            // Insert the new font and return focus to the RichTextBox

            this.richTextBox1.SelectionFont = newFont;
            this.richTextBox1.Focus();

        }
In following image i am making selected text Italic on clicking the italic Button. Coding for italic button is given below.           
                                            
Code for Italic Button
 


 private void button2_Click(object sender, EventArgs e)
        {
            Font oldFont;
            Font newFont;

            // Get the font that is being used in the selected text

            oldFont = this.richTextBox1.SelectionFont;

            // If the font is using Italic style now, we should remove it

            if (oldFont.Italic)
                newFont = new Font(oldFont, oldFont.Style & ~FontStyle.Italic);
            else
                newFont = new Font(oldFont, oldFont.Style | FontStyle.Italic);

            // Insert the new font

            this.richTextBox1.SelectionFont = newFont;
            this.richTextBox1.Focus();

        }
        In following image i am making selected text Underline on clicking the Underline Button. Coding for Underline Button is given below.                                        
                                               
Coding for Underline Button
 


private void button3_Click(object sender, EventArgs e)
        {
            Font oldFont;
            Font newFont;
            // Get the font that is being used in the selected text

            oldFont = this.richTextBox1.SelectionFont;

            // If the font is using Underline style now, we should remove it

            if (oldFont.Underline)
                newFont = new Font(oldFont, oldFont.Style & ~FontStyle.Underline);
            else
                newFont = new Font(oldFont, oldFont.Style | FontStyle.Underline);

            // Insert the new font

            this.richTextBox1.SelectionFont = newFont;
            this.richTextBox1.Focus();

        }
 In following image i am making selected text Center on clicking the Center Button. Coding for Center Button is given below.  
                                                
Coding for Center Button
 


private void button4_Click(object sender, EventArgs e)
        {
            if (this.richTextBox1.SelectionAlignment == HorizontalAlignment.Center)
                this.richTextBox1.SelectionAlignment = HorizontalAlignment.Left;
            else
                this.richTextBox1.SelectionAlignment = HorizontalAlignment.Center;
            this.richTextBox1.Focus();

        }
In following image i am making selected text size large and small by putting the value in size textbox. Coding for this is given below.  
                                                
Coding for Size Change
for this you have to write following code on textbox validating event.

private void txtSize_Validating(object sender, CancelEventArgs e)
        {
            TextBox txt = (TextBox)sender;

            ApplyTextSize(txt.Text);
            this.richTextBox1.Focus();

        }
And then you have to write keypress event on textbox event.

private void txtSize_KeyPress(object sender, KeyPressEventArgs e)
        {
            if ((e.KeyChar < 48 || e.KeyChar > 57) && e.KeyChar != 8 && e.KeyChar != 13)
            {
                e.Handled = true;
            }
            else if (e.KeyChar == 8)
            {
                MessageBox.Show("Please input valid size");
            }
            else if (e.KeyChar == 13)
            {
                // Apply size if the user hits enter

                TextBox txt = (TextBox)sender;

                if (txt.Text.Length > 0)
                    ApplyTextSize(txt.Text);
                e.Handled = true;
                this.richTextBox1.Focus();
            }

        }
and then make a method for applytextsize method

private void ApplyTextSize(string textSize)
        {
            // Convert the text to a float because we'll be needing a float shortly

            float newSize = Convert.ToSingle(textSize);
            FontFamily currentFontFamily;
            Font newFont;

            // Create a new font of the same family but with the new size

            currentFontFamily = this.richTextBox1.SelectionFont.FontFamily;
            newFont = new Font(currentFontFamily, newSize);

            // Set the font of the selected text to the new font

            this.richTextBox1.SelectionFont = newFont;

No comments:

Post a Comment