Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/291.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# 如何解决Form1.cs中派生的自定义控件类和EventHandler之间的冲突_C#_Winforms - Fatal编程技术网

C# 如何解决Form1.cs中派生的自定义控件类和EventHandler之间的冲突

C# 如何解决Form1.cs中派生的自定义控件类和EventHandler之间的冲突,c#,winforms,C#,Winforms,问题是: 上面的代码用于名为CustomListView的类。它是一个自定义控件,其目的是允许我右键单击一行并使用该行执行函数。我成功地将其构建为dll,将其添加到工具箱中,并在表单上使用它。我可以像普通的ListView一样用信息填充它,但是ContextMenuStrip的任何右键单击功能都不起作用 解决办法的尝试: 我已将ContextMenuStrip分配给CustomListView控件。我认为问题在于CustomListView类中的EventHandler,即OnMouseDown

问题是: 上面的代码用于名为CustomListView的类。它是一个自定义控件,其目的是允许我右键单击一行并使用该行执行函数。我成功地将其构建为dll,将其添加到工具箱中,并在表单上使用它。我可以像普通的ListView一样用信息填充它,但是ContextMenuStrip的任何右键单击功能都不起作用

解决办法的尝试: 我已将ContextMenuStrip分配给CustomListView控件。我认为问题在于CustomListView类中的EventHandler,即OnMouseDown和OnMouseUp没有被使用。相反,Form1.cs中使用了其他具有相同名称的事件处理程序,它们自然不包含任何代码

我是否应该将CustomListView类中的EventHandler移动到Form1.cs?如何使用CustomListView类中的EventHandler?

以下几点:

默认情况下,listview没有ContextMenuStrip。这意味着你必须确保它有一个,然后才能使用它

设置允许显示的布尔值后,必须调用Show方法。事实上,您可能不需要使用boolean,而只需要使用Show和Close方法


好吧,我了解布尔人。编译器如何协调类本身中的事件处理程序以及可从visual studio中的设计器访问的事件处理程序?@t72-如果我没有弄错的话,特定事件的处理程序位于列表中,并按添加顺序运行。由于类中的一个是首先添加的,它将首先运行。有一点需要澄清:如果我已经在MOuseDown/Mouseup中打开了MenuStrip,那么ContextMenuStrip\u Open方法是否有必要?目前正在重做代码以进行这些更改,我还注意到在某个时刻CustomListView中出现了一个NullReferenceException。另外:我注意到当我没有在表单上单击任何鼠标按钮时会发生这种情况。我认为由于某些原因,我没有正确地传递ContextMenuStrip。我在VS中的设计器中设置了它,还尝试将它添加到Form1_Load中。@t72-您是否尝试在我的代码中遵循该示例?请注意,ContextMenuStrip设置为私有字段。您可以随意设置项,并在类中使用代码来处理它们。默认构造函数可以像我所做的那样使用。
public class CustomListView : ListView
{
    public CustomListView()
    {

    }
    private bool ContextAllowed = false;

    public override ContextMenuStrip ContextMenuStrip
    {
        get
        {
            return base.ContextMenuStrip;
        }
        set
        {
            base.ContextMenuStrip = value;
            base.ContextMenuStrip.Opening += ContextMenuStrip_Open;
        }
    }

    protected override void OnMouseDown(MouseEventArgs e)
    {
        if (e.Button == MouseButtons.Right)
        {
            ListViewHitTestInfo test = HitTest(e.X, e.Y); //perform a hit test
            if (test.Item != null) //if it hits an item, display the popup bar
            {
                ContextAllowed = true;
            }
        }
        base.OnMouseDown(e);
    }
    protected override void OnMouseUp(MouseEventArgs e)
    {
        if (e.Button == MouseButtons.Right)
        {
            ContextAllowed = false; //close the menu
        }
        base.OnMouseUp(e);
    }

    private void ContextMenuStrip_Open(object sender, CancelEventArgs e)
    {
        if (!ContextAllowed)
        {
            e.Cancel = true;
        }
    }
}
public class CustomListView : ListView
{
    public CustomListView()
    {
        cms.Items.AddRange(new ToolStripItem[]{ new ToolStripLabel("Test1"),new ToolStripLabel("Test2")});
        ContextMenuStrip = cms;
    }
    private bool ContextAllowed = false;
    private ContextMenuStrip cms = new ContextMenuStrip();

    public override ContextMenuStrip ContextMenuStrip
    {
        get
        {
            return base.ContextMenuStrip;
        }
        set
        {
            base.ContextMenuStrip = value;
            base.ContextMenuStrip.Opening += ContextMenuStrip_Open;
        }
    }

    protected override void OnMouseDown(MouseEventArgs e)
    {
        if (e.Button == MouseButtons.Right)
        {
            ListViewHitTestInfo test = HitTest(e.X, e.Y); //perform a hit test
            if (test.Item != null) //if it hits an item, display the popup bar
            {
                ContextAllowed = true;
                ContextMenuStrip.Show();
            }
        }
        base.OnMouseDown(e);
    }
    protected override void OnMouseUp(MouseEventArgs e)
    {
        if (e.Button == MouseButtons.Right)
        {
            ContextAllowed = false; //close the menu
        }
        base.OnMouseUp(e);
    }

    private void ContextMenuStrip_Open(object sender, CancelEventArgs e)
    {
        if (!ContextAllowed)
        {
            e.Cancel = true;
        }
    }
}