0

I am working on a custom context menu in an open-source text editor project I found on the internet. The custom context menu is made out of a Form. The problem is when I open the custom context menu, the text editor(main form) lost focus(becomes gray). I want it to look like this when the context menu opens, but not this. I have tried some solutions but they didn't work:

Solution 1: Caused the custom context menu to be on the back of the text editor once opened. It then stopped the custom context menu from working properly.

Solution 2: Caused my program to be crashed so it automatically closed.

Solution 3: I tried using either Control.Focus() or Control.Activate() to have focus on the text editor when invoking the custom context menu, but still didn't work.

Are there any other solutions I could try or am I missing something here?

Link(Head to: Branch -> v2.01) to the custom context menu project that I am working on. Please note that I have made it so that Control Key(CTRL) + Right mouse button(RMB) to invoke the custom context menu.

ewu11
  • 65
  • 9
  • Are you mostly concerned about the look of the forms and not input? If so you might consider custom drawing to ensure the form looks as you want it, regardless if it is focused or not. – JonasH Jan 28 '22 at 15:20
  • To me, I guess maintaining focus while opening another form doesn't sound like the looks of the form. I just want to make my custom context menu that was made out of a Form to work similarly like the ordinary Context Menu/ RichContextMenu where, the focus of the parent's form is maintained upon the context menu invocation. – ewu11 Jan 28 '22 at 15:48
  • 1
    What about using a ContextMenu with custom content using `ToolStripControlHost` instead of a Form? It can contain extensible content, e.g., some UserControls as shown here: [How to add Controls, as a toolbar, to a ContextMenuStrip?](https://stackoverflow.com/a/65280439/7444103). That won't steal the focus and you can extend it as you please. – Jimi Jan 28 '22 at 16:47
  • Thank you for the idea, Jimi. However, I'm afraid I'm in a state where I can't code all over again by using another implementation. I am just focusing on how I could fix the current problem that I'm facing. – ewu11 Jan 28 '22 at 17:19

1 Answers1

0

I am not sure whether this is conventionally the correct way to do it, but it works for me.

However, another problem it poses is that the context menu item functions cannot be executed. It can be clicked but then the nothing happens.

//activated event handler for the context menu
private void ContextMenu_Activated(object sender, EventArgs e)
        {
            //solution A
            this.Activate(); //gives the main form the focus

            //solution B
            //this.Focus(); //gives the main form the focus

            contextMenuObj.Visible = true; //forces the context menu to be displayed

            //to maintain highlighted text in textbox in main form
            richTextBox1.HideSelection = false;

            //reset default flag once custom context menu opens
            ctrlIsDown = false;
            rmbIsUp = false;
        }

This way, the main form is not greyed out when the context menu is displayed. workingExample

ewu11
  • 65
  • 9