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


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

Up Next
    Ebook Download
    View all
    Learn
    View all