rightclick面板无法正常工作

时间:2014-09-27 08:30:23

标签: c# winforms

private void panel1_MouseClick(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                ColorDialog cd = new ColorDialog();
                if (cd.ShowDialog() == DialogResult.OK)
                {
                    this.panel3.BackColor = cd.Color;
                }
            }
        }

private void panel1_MouseDown(object sender, MouseEventArgs e)
        {
            Panel pnl = sender as Panel;
            if (pnl != null)
                pnl.DoDragDrop(pnl.BackColor, DragDropEffects.Move);
        }


    private void panel2_DragDrop(object sender, DragEventArgs e)
    {
        var data = e.Data.GetData(typeof(Color));

        if (data != null)
            ((Panel)sender).BackColor = (Color)data;
    }

    private void panel2_DragEnter(object sender, DragEventArgs e)
    {
        e.Effect = DragDropEffects.Move;
    }

我想如果我按下右键单击panel1它应该显示颜色选择的颜色对话框,然后我可以将panel1颜色下拉拖到panel2上。 但是在这个代码colpetion终于拖放工作但没有工作panel1右键单击 帮帮我?

1 个答案:

答案 0 :(得分:0)

在鼠标右键单击首先调用mouse down事件,因此它不会调用mouse click事件,因此您在鼠标按下事件中尝试这样做

 private void panel1_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button !=MouseButtons.Right)
            {

            Panel pnl = sender as Panel;
            if (pnl != null)
                pnl.DoDragDrop(pnl.BackColor, DragDropEffects.Move);
            }
        }