Search code examples
c#.netwinformspositioncontextmenustrip

How to position a Popup Menu to the left side of a Button?


I have a Popup menu. The Popup menu has a two-column layout. Therefore, the Buttons are aligned in two columns, which are the left column and the right column. Inside it, there are Buttons.
Upon clicking one of the Buttons, it will open a Dropdown Menu (created using ContextMenuStrip).

This is how the popup menu and its contents look like:

popupMenu

I have no problem to place the dropdown menu on the right width of the "Style token" button. The problem occurs when I'm trying to place the Dropdown Menu on the left width of the "Remove style" button.
The image below shows the examples:

The dropdown location on the right width of the "Style token" Button works just fine.

rightColumn

The Dropdown location on the left width of the "Remove style" Button is problematic as I don't really know how to code the logic of it.
It should look as shown below:

leftColumn


Actually, I've coded the part I'm having an issue with. However, I'm not really sure if this is the conventional or professional way of coding it. Also, there are times there will be a random gap between the left Dropdown Menu with the "Remove Style" Button.
Below shows the code example:

//--global variable--
int removeStyleStripXPos; //to store xPos of left column dropdown menu

//removeStyleStrip -> the left dropdown menu
//styleTokenStrip -> the right dropdown menu
//--global variable--

//--the mouseUp event handler of "Remove style" button--
private void button13_MouseUp(object sender, MouseEventArgs e)
{
    if (e.Button == MouseButtons.Left)
    {
        removeStyleStrip.Show(button13, new Point(removeStyleStripXPos, 0));
    }
}

//--the mouseUp event handler of "Style token" button--
private void button14_MouseUp(object sender, MouseEventArgs e)
{
    if (e.Button == MouseButtons.Left)
    {
        styleTokenStrip.Show(button14, new Point(this.button14.DisplayRectangle.Right, 0));
    }
}

//--the load event handler of the two column popup menu--
private void TCPopupMenuFull_Load(object sender, EventArgs e)
{
    removeStyleStripXPos = -(this.removeStyleStrip.Width);
}

So, may I know if there are better ways of setting the position of the Dropdown Menu to the left end of the "Remove style" Button?


Solution

  • Thanks for the time and answer, @Jimi. I really appreciate all your helps.

    He shared me an answer and it worked very well. It turns out I just need to tweak the logic under the MouseUp event handler of the button13, the button that opens the Remove style's ContextMenuStrip.


    Codes:

    //--the mouseUp event handler of "Remove style" button--
    private void button13_MouseUp(object sender, MouseEventArgs e)
    {
        if (e.Button == MouseButtons.Left)
        {
            var screenPos = button13.PointToScreen(Point.Empty);
            removeStyleStrip.Show(new Point(screenPos.X - removeStyleStrip.Width, screenPos.Y));
        }
    }