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

时间:2015-01-02 01:35:43

标签: c# winforms

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;
        }
    }
}

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

尝试解决方案: 我已将ContextMenuStrip分配给CustomListView控件。我认为问题是没有使用CustomListView类中的EventHandlers(即OnMouseDown和OnMouseUp)。相反,在Form1.cs中使用具有相同名称的“其他”事件,这自然不包含代码。

我应该将CustomListView类中的EventHandlers移动到Form1.cs吗?如何在CustomListView类中使用EventHandlers?

1 个答案:

答案 0 :(得分:0)

有几件事:

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

  • 设置允许显示的布尔值后,您必须调用Show()方法。实际上你可以省去布尔值,只使用Show()Close()方法。

    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;
            }
        }
    }