AnotherUser
AnotherUser

Reputation: 1353

ContextMenuStrip drawn improperly above specified Control

I am trying to create a specialized toolbar for my application, it consists of a few buttons which when clicked should show a ContextMenuStrip above them with a few choices. It seems that I cannot get this menu to be drawn in the proper position.

Below is a SSCCE that reproduces the problem and here are a few pictures showing the problem.

Any ideas on how to fix this?

enter image description here

Form1.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ContextMenuTest
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.testToolbar1 = new ContextMenuTest.TestToolbar();
            this.SuspendLayout();
            // 
            // testToolbar1
            // 
            this.testToolbar1.Location = new System.Drawing.Point(12, 12);
            this.testToolbar1.Name = "testToolbar1";
            this.testToolbar1.Size = new System.Drawing.Size(321, 23);
            this.testToolbar1.TabIndex = 1;
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(352, 51);
            this.Controls.Add(this.testToolbar1);
            this.IsMdiContainer = true;
            this.Name = "Form1";
            this.Text = "Form1";
            this.ResumeLayout(false);

        }

        #endregion

        private TestToolbar testToolbar1;
    }
}

TestToolbar.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ContextMenuTest
{
    public partial class TestToolbar : UserControl
    {
        public TestToolbar()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            curType = contectType.One;
            fromControl = button1;
            showContextStrip();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            curType = contectType.Two;
            fromControl = button2;
            showContextStrip();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            curType = contectType.Three;
            fromControl = button3;
            showContextStrip();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            curType = contectType.Four;
            fromControl = button4;
            showContextStrip();
        }

        contectType curType;
        Control fromControl;
        enum contectType { One, Two, Three, Four }

        private void showContextStrip()
        {
            int numShow = 0;
            switch (curType)
            {
                case contectType.One:
                    {
                        numShow = 1;
                    } break;
                case contectType.Two:
                    {
                        numShow = 2;
                    } break;
                case contectType.Three:
                    {
                        numShow = 3;
                    } break;
                case contectType.Four:
                    {
                        numShow = 4;
                    } break;
            }

            contextMenuStrip1.Items.Clear();

            for (int i = 0; i < numShow; i++)
            {
                ToolStripMenuItem newItem = new ToolStripMenuItem();
                newItem.Text = i.ToString();
                contextMenuStrip1.Items.Add(newItem);
            }

            Console.Out.WriteLine("1Pos: " + button1.Location);
            Console.Out.WriteLine("2Pos: " + button2.Location);
            Console.Out.WriteLine("3Pos: " + button3.Location);
            Console.Out.WriteLine("4Pos: " + button4.Location);
            Console.Out.WriteLine("Ctrl: " + fromControl.Location);

            contextMenuStrip1.Show(fromControl, fromControl.Location, ToolStripDropDownDirection.AboveRight);
        }

        /// <summary> 
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary> 
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Component Designer generated code

        /// <summary> 
        /// Required method for Designer support - do not modify 
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.components = new System.ComponentModel.Container();
            this.button1 = new System.Windows.Forms.Button();
            this.button2 = new System.Windows.Forms.Button();
            this.button3 = new System.Windows.Forms.Button();
            this.button4 = new System.Windows.Forms.Button();
            this.contextMenuStrip1 = new System.Windows.Forms.ContextMenuStrip(this.components);
            this.SuspendLayout();
            // 
            // button1
            // 
            this.button1.Location = new System.Drawing.Point(3, 0);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(75, 23);
            this.button1.TabIndex = 0;
            this.button1.Text = "button1";
            this.button1.UseVisualStyleBackColor = true;
            this.button1.Click += new System.EventHandler(this.button1_Click);
            // 
            // button2
            // 
            this.button2.Location = new System.Drawing.Point(84, 0);
            this.button2.Name = "button2";
            this.button2.Size = new System.Drawing.Size(75, 23);
            this.button2.TabIndex = 1;
            this.button2.Text = "button2";
            this.button2.UseVisualStyleBackColor = true;
            this.button2.Click += new System.EventHandler(this.button2_Click);
            // 
            // button3
            // 
            this.button3.Location = new System.Drawing.Point(165, 0);
            this.button3.Name = "button3";
            this.button3.Size = new System.Drawing.Size(75, 23);
            this.button3.TabIndex = 2;
            this.button3.Text = "button3";
            this.button3.UseVisualStyleBackColor = true;
            this.button3.Click += new System.EventHandler(this.button3_Click);
            // 
            // button4
            // 
            this.button4.Location = new System.Drawing.Point(246, 0);
            this.button4.Name = "button4";
            this.button4.Size = new System.Drawing.Size(75, 23);
            this.button4.TabIndex = 3;
            this.button4.Text = "button4";
            this.button4.UseVisualStyleBackColor = true;
            this.button4.Click += new System.EventHandler(this.button4_Click);
            // 
            // contextMenuStrip1
            // 
            this.contextMenuStrip1.Name = "contextMenuStrip1";
            // 
            // TestToolbar
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.Controls.Add(this.button4);
            this.Controls.Add(this.button3);
            this.Controls.Add(this.button2);
            this.Controls.Add(this.button1);
            this.Name = "TestToolbar";
            this.Size = new System.Drawing.Size(321, 23);
            this.ResumeLayout(false);

        }

        #endregion

        private System.Windows.Forms.Button button1;
        private System.Windows.Forms.Button button2;
        private System.Windows.Forms.Button button3;
        private System.Windows.Forms.Button button4;
        private System.Windows.Forms.ContextMenuStrip contextMenuStrip1;
    }
}

Upvotes: 3

Views: 118

Answers (1)

AnotherUser
AnotherUser

Reputation: 1353

Ahh, it seems my knowledge of English has led me astray.

Positions the ToolStripDropDown relative to the specified control at the specified location and with the specified direction relative to the parent control. (Inherited from ToolStripDropDown.)

This:

contextMenuStrip1.Show(fromControl, fromControl.Location, ToolStripDropDownDirection.AboveRight);

Should be this:

Point origin = new Point(0, 0);
contextMenuStrip1.Show(fromControl, origin, ToolStripDropDownDirection.AboveRight);

Upvotes: 1

Related Questions