none
(C#) Context menu popup on mouseover selection

    Question

  • Hi all,

    I understand my topic title sounds a little cryptic, but let me explain it a little better: 

    In Microsoft Word (2007, 2010), if you move your mouse over the selected text, you'll get a little context menu. This enables you to do a few basic tasks with that selection. I'm looking for a way to do the same thing, but I can't really figure out how to do that. 

    Right now, I am using a Rich Textbox, but I'm more than happy to change it to something which enables me to do so. As a matter of fact, I don't really want to use the Rich Textbox, but my alternative is a webbrowser control which doesn't allow me to change the context menu.

    So can anyone help me out on this one?

     

    Thanks a lot.

    Thursday, March 31, 2011 12:16 PM

Answers

  • RichTextBox might be the way to go. Here's an example from MSDN of positioning and displaying a custom ContextMenu for a RichTextbox:

    How to: Position a Custom Context Menu in a RichTextBox

    Basically you wire up an event handler to the RichTextBox ContextMenuOpening event, then add the following code:

    // This method is intended to listen for the ContextMenuOpening event from a RichTextBox.
    // It will position the custom context menu at the end of the current selection.
    void richTextBox_ContextMenuOpening(object sender, ContextMenuEventArgs e)
    {
      // Sender must be RichTextBox.
      RichTextBox rtb = sender as RichTextBox;
      if (rtb == null) return;
    
      ContextMenu contextMenu = rtb.ContextMenu;
      contextMenu.PlacementTarget = rtb;
    
      // This uses HorizontalOffset and VerticalOffset properties to position the menu,
      // relative to the upper left corner of the parent element (RichTextBox in this case).
      contextMenu.Placement = PlacementMode.RelativePoint;
    
      // Compute horizontal and vertical offsets to place the menu relative to selection end.
      TextPointer position = rtb.Selection.End;
    
      if (position == null) return;
    
      Rect positionRect = position.GetCharacterRect(LogicalDirection.Forward);
      contextMenu.HorizontalOffset = positionRect.X;
      contextMenu.VerticalOffset = positionRect.Y;
    
      // Finally, mark the event has handled.
      contextMenu.IsOpen = true;
      e.Handled = true;
    }
    

    I believe that only replaces the default context menu that you get when you right click rather than displaying one when you hover over selected text like Microsoft Office.

     

    This RichTextBoxFormatBar control from the extended wpf tookit on codeplex might be more along the lines of what you're trying to do:

    http://wpftoolkit.codeplex.com/wikipage?title=RichTextBoxFormatBar

    If you don't want to use a RichTextBox, the source code might at least give you some ideas on how to do it with whatever control you choose.




    • Edited by _xr280xr_ Thursday, March 31, 2011 1:39 PM Imported linked example
    • Marked as answer by Hmail Thursday, March 31, 2011 2:25 PM
    Thursday, March 31, 2011 1:31 PM

All replies

  • Hi,

    Check out the Popup control. It can be a little finicky to work with in some cases, but sounds like it will do exactly what you need. It is similar to a tooltip, but one of the most important differences is that it accepts focus. You could nest a toolbar or any other content you want inside the popup. Here's a reference to the class with some examples at the bottom: http://msdn.microsoft.com/en-us/library/system.windows.controls.primitives.popup.aspx. Let us know if you have any questions about implementation.

     

    EDIT: Is your question how to determine when the mouse is over selected text?


    Thursday, March 31, 2011 12:27 PM
  • Hi Hmail,

            as you describe See the link:

              http://atikpassion.blogspot.com/2011/03/wpf-contextmenu-for-listview-item.html

            For context menu though the example is based on list view you can also do it for your RichTextbox.

            Hope it will help you.


    atik sarker
    Thursday, March 31, 2011 12:52 PM
  • RichTextBox might be the way to go. Here's an example from MSDN of positioning and displaying a custom ContextMenu for a RichTextbox:

    How to: Position a Custom Context Menu in a RichTextBox

    Basically you wire up an event handler to the RichTextBox ContextMenuOpening event, then add the following code:

    // This method is intended to listen for the ContextMenuOpening event from a RichTextBox.
    // It will position the custom context menu at the end of the current selection.
    void richTextBox_ContextMenuOpening(object sender, ContextMenuEventArgs e)
    {
      // Sender must be RichTextBox.
      RichTextBox rtb = sender as RichTextBox;
      if (rtb == null) return;
    
      ContextMenu contextMenu = rtb.ContextMenu;
      contextMenu.PlacementTarget = rtb;
    
      // This uses HorizontalOffset and VerticalOffset properties to position the menu,
      // relative to the upper left corner of the parent element (RichTextBox in this case).
      contextMenu.Placement = PlacementMode.RelativePoint;
    
      // Compute horizontal and vertical offsets to place the menu relative to selection end.
      TextPointer position = rtb.Selection.End;
    
      if (position == null) return;
    
      Rect positionRect = position.GetCharacterRect(LogicalDirection.Forward);
      contextMenu.HorizontalOffset = positionRect.X;
      contextMenu.VerticalOffset = positionRect.Y;
    
      // Finally, mark the event has handled.
      contextMenu.IsOpen = true;
      e.Handled = true;
    }
    

    I believe that only replaces the default context menu that you get when you right click rather than displaying one when you hover over selected text like Microsoft Office.

     

    This RichTextBoxFormatBar control from the extended wpf tookit on codeplex might be more along the lines of what you're trying to do:

    http://wpftoolkit.codeplex.com/wikipage?title=RichTextBoxFormatBar

    If you don't want to use a RichTextBox, the source code might at least give you some ideas on how to do it with whatever control you choose.




    • Edited by _xr280xr_ Thursday, March 31, 2011 1:39 PM Imported linked example
    • Marked as answer by Hmail Thursday, March 31, 2011 2:25 PM
    Thursday, March 31, 2011 1:31 PM
  •  

    This RichTextBoxFormatBar control from the extended wpf tookit on codeplex might be more along the lines of what you're trying to do:

    http://wpftoolkit.codeplex.com/wikipage?title=RichTextBoxFormatBar

    If you don't want to use a RichTextBox, the source code might at least give you some ideas on how to do it with whatever control you choose.




    That's exactly what I was looking for. From the source, I can create my own control. Thanks a lot!
    Thursday, March 31, 2011 2:25 PM