ARTICLE

RichTextbox with context menu for setting Color and Font in .Net windows application

Posted by Shinuraj Articles | Windows Forms C# December 21, 2010
In this article you will learn how ro use RichTextbox with context menu for setting Color and Font in .Net windows application
Reader Level:

Steps:

  1. Add a RichTextBox to the Form
     
  2. Add Contextmenu and set two items as a)Add Color , b)Set Font
     
  3. Set this as the contextmenu for the RichTextbox(take the property and directly set )
     
  4. Add ColorDialog and fontDialog to the form
     
  5. Hook the event contextMenuStrip1_ItemClicked and use the "SelectionFont" and "SelectionColor" property of RichTextbox as shown in the below code to set the color and font

           private void contextMenuStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
            {
                if (e.ClickedItem.Text ==" Add Color ")
                {
                    DialogResult color = colorDialog1.ShowDialog();
                    // See if user pressed ok.
                    if (color == DialogResult.OK)
                    {
                        // Set Color to the selected text
                        this.richTextBox1.SelectionColor = colorDialog1.Color;
                    }
                }
                if (e.ClickedItem.Text ==" Set Font ")
                {
                    DialogResult font = fontDialog1.ShowDialog();
                    if (font == DialogResult.OK)
                    {
                        // Set selection font to the fontDialog1.Font
                        this.richTextBox1.SelectionFont = fontDialog1.Font;
                    }
                }
            }

    image1.gif

    image2.gif

Login to add your contents and source code to this article
post comment
     
COMMENT USING
PREMIUM SPONSORS
DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and add new content to existing PDF documents from within your applications.
Get Career Advice from Experts
SPONSORED BY
  • PDF reports have never been easier to create. With our included WYSIWYG Designer, you can layout your reports, set up your data source and let DynamicPDF ReportWriter do the rest.