未引发 DragDrop 事件

发布于 2024-09-30 21:13:40 字数 1117 浏览 6 评论 0 原文

这是一个愚蠢的问题...我正在尝试将图片框拖放到面板上。我遵循了一些例子,但它不起作用。面板的 DragDrop 事件永远不会引发。我在这个网站上搜索了解决方案,发现了两个已有一年多的主题,但他们的解决方案不起作用。我创建了一个新项目,仅包含以下代码:

public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            pictureBox1.MouseDown += new MouseEventHandler(pictureBox1_MouseDown);
            panel1.DragDrop +=new DragEventHandler(panel1_DragDrop);
            panel1.DragOver +=new DragEventHandler(panel1_DragOver);
        }

        private void panel1_DragOver(object sender, DragEventArgs e)
        {
            Console.WriteLine("DragOver");
        }

        private void panel1_DragDrop(object sender, DragEventArgs e)
        {
            Console.WriteLine("DragDrop");
        }

        private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
        {
            Console.WriteLine("Mouse");
            pictureBox1.DoDragDrop(pictureBox1.Text, DragDropEffects.All);
        }
    }

我还将面板和表单的AllowDrop设置为true。 DragOver 和 MouseDown 被抬起。另外,当我拖动图片框时,光标会变成带圆圈的,就像这是不允许的操作一样。有没有办法让光标变成图片框中的图像?我不想移动图片框,只想向面板添加一个项目。

This is kind of a stupid question... I'm trying to drag and drop a picturebox onto a panel. I followed some exemples, but it doesn't work. The DragDrop event of the panel is never raised. I searched thi site for a solution andfound two topics over a year old, but their solutions did not work. I created a new project, with only this code :

public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            pictureBox1.MouseDown += new MouseEventHandler(pictureBox1_MouseDown);
            panel1.DragDrop +=new DragEventHandler(panel1_DragDrop);
            panel1.DragOver +=new DragEventHandler(panel1_DragOver);
        }

        private void panel1_DragOver(object sender, DragEventArgs e)
        {
            Console.WriteLine("DragOver");
        }

        private void panel1_DragDrop(object sender, DragEventArgs e)
        {
            Console.WriteLine("DragDrop");
        }

        private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
        {
            Console.WriteLine("Mouse");
            pictureBox1.DoDragDrop(pictureBox1.Text, DragDropEffects.All);
        }
    }

I also set the AllowDrop of the panel and the form to true. DragOver and MouseDown are raised. Also, when I drag the picturebox, the cursor become a barred circled, like it was an operation that wasn't allowed. Is there a way that the cursor becomes the image in the picture box? I don't want the picturebox to move, only to add an item to the panel.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

耀眼的星火 2024-10-07 21:13:40

问题很容易解决。

您必须在 DragEnter 中设置适当的 Effect:

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

之后,DragDrop 事件才能正确触发。

The problem is easy to solve.

You must just set in DragEnter appropriate Effect:

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

After that DragDrop event is fired correctly.

木有鱼丸 2024-10-07 21:13:40

理查德,问题是拖放操作并不像您在此处编写的代码那么简单。这里您还没有开始拖动运动,应该从代码开始,您可以在此处阅读更多相关信息... http://msdn.microsoft.com/en-us/library/system.windows.forms.control.dodragdrop(v= VS.90).aspx

如果您只想移动 PictureBox... 在运行时将图片框拖到winform中

最后在同一 Windows 窗体应用程序的实例之间拖放

希望这会有所帮助。

Richard, the problem is that drag and drop is not as simple operation as you have coded here. Here you haven't started the drag movement which should start with code and you can read more about it here... http://msdn.microsoft.com/en-us/library/system.windows.forms.control.dodragdrop(v=VS.90).aspx

If you just want to move the PictureBox... dragging picturebox inside winform on runtime

And finally Drag and Drop between Instances of the same Windows Forms Application

Hope this helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文