Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# ContextMenuStrip在指定控件上方绘制不正确_C#_.net_Winforms_Contextmenustrip - Fatal编程技术网

C# ContextMenuStrip在指定控件上方绘制不正确

C# ContextMenuStrip在指定控件上方绘制不正确,c#,.net,winforms,contextmenustrip,C#,.net,Winforms,Contextmenustrip,我正在尝试为我的应用程序创建一个专门的工具栏,它由几个按钮组成,单击这些按钮时,上面会显示一个ContextMenuStrip,并有一些选择。看来我不能把菜单画在正确的位置 下面是一个重现问题的SSCE,下面是一些显示问题的图片 有没有办法解决这个问题 表格1.cs using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawin

我正在尝试为我的应用程序创建一个专门的工具栏,它由几个按钮组成,单击这些按钮时,上面会显示一个
ContextMenuStrip
,并有一些选择。看来我不能把菜单画在正确的位置

下面是一个重现问题的SSCE,下面是一些显示问题的图片

有没有办法解决这个问题

表格1.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;
    }
}
使用系统;
使用System.Collections.Generic;
使用系统组件模型;
使用系统数据;
使用系统图;
使用System.Linq;
使用系统文本;
使用System.Windows.Forms;
命名空间上下文测试
{
公共部分类Form1:Form
{
公共表格1()
{
初始化组件();
}
/// 
///必需的设计器变量。
/// 
private System.ComponentModel.IContainer components=null;
/// 
///清理所有正在使用的资源。
/// 
///如果应释放托管资源,则为true;否则为false。
受保护的覆盖无效处置(布尔处置)
{
if(处理和(组件!=null))
{
组件。Dispose();
}
基地。处置(处置);
}
#区域Windows窗体设计器生成的代码
/// 
///设计器支持所需的方法-不修改
///此方法的内容与代码编辑器一起使用。
/// 
私有void InitializeComponent()
{
this.testToolbar1=新建ContextMenuTest.TestToolbar();
这个.SuspendLayout();
// 
//测试工具栏1
// 
this.testToolbar1.Location=新系统.Drawing.Point(12,12);
this.testToolbar1.Name=“testToolbar1”;
this.testToolbar1.Size=新系统.Drawing.Size(321,23);
this.testToolbar1.TabIndex=1;
// 
//表格1
// 
此.AutoScaleDimensions=新系统.Drawing.SizeF(6F,13F);
this.AutoScaleMode=System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize=新系统.Drawing.Size(352,51);
this.Controls.Add(this.testToolbar1);
this.IsMdiContainer=true;
this.Name=“Form1”;
this.Text=“Form1”;
此选项为.resume布局(false);
}
#端区
专用测试工具栏TestToolbar 1;
}
}
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;
    }
}
使用系统;
使用System.Collections.Generic;
使用系统组件模型;
使用系统图;
使用系统数据;
使用System.Linq;
使用系统文本;
使用System.Windows.Forms;
命名空间上下文测试
{
公共部分类TestToolbar:UserControl
{
公共测试工具栏()
{
初始化组件();
}
私有无效按钮1\u单击(对象发送者,事件参数e)
{
curType=contectType.One;
fromControl=按钮1;
showContextStrip();
}
私有无效按钮2\u单击(对象发送者,事件参数e)
{
curType=contectType.Two;
fromControl=按钮2;
showContextStrip();
}
私有无效按钮3\u单击(对象发送者,事件参数e)
{
curType=contectType.3;
fromControl=按钮3;
showContextStrip();
}
私有无效按钮4_单击(对象发送者,事件参数e)
{
curType=contectType.4;
fromControl=按钮4;
showContextStrip();
}
contectType-curType;
控制来自控制;
枚举上下文类型{1,2,3,4}
私有void showContextStrip()
{
int numShow=0;
开关(curType)
{
案例内容类型。一:
{
numShow=1;
}中断;
案例内容类型。二:
{
numShow=2;
}中断;
案例内容类型。三:
{
numShow=3;
}中断;
案例内容类型。四:
{
numShow=4;
}中断;
}
contextMenuStrip1.Items.Clear();
for(int i=0;icontextMenuStrip1.Show(fromControl, fromControl.Location, ToolStripDropDownDirection.AboveRight);
Point origin = new Point(0, 0);
contextMenuStrip1.Show(fromControl, origin, ToolStripDropDownDirection.AboveRight);