Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8559099
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T15:57:45+00:00 2026-06-11T15:57:45+00:00

I am creating a Context Menu Strip once a Rich Text Box is right

  • 0

I am creating a Context Menu Strip once a Rich Text Box is right clicked. There are 2 options, one to change the font and one to change the background color. However, once I click one of the menu options, the Context Menu Strip doesn’t close and overlays dialogs that are displayed. I know I can make it “global” and force it to close, but I would rather not. What is the best way to handle this?

// If the console is right clicked then show font options
private void rtb_console_MouseUp(object sender, MouseEventArgs e)
{
    if (e.Button == System.Windows.Forms.MouseButtons.Right)
    {
        ContextMenuStrip menu = new ContextMenuStrip();
        menu.Items.Add("Change Font");
        menu.Items.Add("Change Background Color");
        menu.Show(this, new Point(e.X, e.Y));
        menu.ItemClicked += new ToolStripItemClickedEventHandler(menu_ItemClicked_ChangeFont);
    }
}  

// Determine whether to change the font or the font background color
void menu_ItemClicked_ChangeFont(object sender, ToolStripItemClickedEventArgs e)
{
    Application.DoEvents();  // Read that this might help, but it doesn't
    if (e.ClickedItem.Text == "Change Font")
    {
        FontDialog font = new FontDialog();

        font.ShowColor = true;
        font.Font = rtb_console.Font;
        font.Color = rtb_console.ForeColor;

        if (font.ShowDialog() == System.Windows.Forms.DialogResult.OK)
        {
            rtb_console.Font = font.Font;
            rtb_console.ForeColor = font.Color;
        }
    }
    else if (e.ClickedItem.Text == "Change Background Color")
    {
        ColorDialog color = new ColorDialog();
        color.Color = rtb_console.BackColor;

        if (color.ShowDialog() == System.Windows.Forms.DialogResult.OK)
        {
            rtb_console.BackColor = color.Color;
        }
    }
}  

So this is what happens:
ContextMenuStrip Persisting

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T15:57:46+00:00Added an answer on June 11, 2026 at 3:57 pm

    You don’t want to create the ContextMenuStrip and show it manually each time. The better way to do this is to create the ContextMenuStrip once. Then assign it for the RichTextBox by assigning it to the ContextMenuStrip property of the RichTextBox. Doing this, you will no longer need to manually launch the ContextMenuStrip everytime a user clicks on it. It will occur automagically. It will also hide itself automagically in the way you would expect upon clicking on it.

    Do this once and then remove your event handler for the MouseUp event:

    ContextMenuStrip menu = new ContextMenuStrip(); 
    menu.Items.Add("Change Font"); 
    menu.Items.Add("Change Background Color"); 
    menu.ItemClicked += new ToolStripItemClickedEventHandler(menu_ItemClicked_ChangeFont);
    rtb_console.ContextStripMenu = menu;
    

    Also, please please please don’t use Application.DoEvents(); to try and force the UI to update itself. Head over to here and read the top answer. In general, if you are using Application.DoEvents(), you are doing something wrong and should considering changing your approach.

    One thing you may also consider doing, but it is really just a matter of preference… If you are using Visual Studio, consider creating you ContextMenuStrip in the designer. That way, you can add your items, icons, and individual callbacks for each item very easily and visually. Just something I like to do out of pure personal preference.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In one of my Android projects, I am creating a context menu from a
I'm currently creating a right-click context menu by instantiating a new JMenu on right
Silverlight 4 now include the option for creating a context menu upon right clicking.
I am using XML file for creating Context Menu for my ListView. (Please see
I am creating a system tray program with a shortcut/context menu, but I can't
What controls would be most suitable for creating a complex context menu, similar what
Is there a place I can view/change global shortcut options like Command + 9
I'm creating a context menu in code behind with a submenu. I want the
I'm creating dynamically Button views with a context menu. When a context menu item
So I'm interested in creating a custom context menu for each of my list

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.