从桌面拖放并在Windows窗体中删除表单不起作用? [英] Drag From Desktop and Drop in windows Form not working?

查看:53
本文介绍了从桌面拖放并在Windows窗体中删除表单不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,

我正在开发一个Windows应用程序,我正在将我的小拖放功能添加到我的窗体中

这是场景

- 我得到了带有文本框的窗体

- 我想从桌面拖出一些东西说文字或文件名或文件夹

并放入我的Testbox
- 我设置TextboxTrue的Allow-Drop属性

这里是我的代码

Hallo,
I m developing a windows application where i am iplemeneting small Drag and Drop function to my windows form
Here is the scenarios
--I got windows Form with Text boxes
-Now i want to Drag something from Desktop say text or filename or folder
and Drop in my Testbox
--i set Allow-Drop Property of my TextboxTrue
here is my code

   private void Mainform_Load(object sender, EventArgs e)
     {
         txtbx_def.AllowDrop = true;
         txtbx_def.DragEnter += new DragEventHandler(Def_DragEnter);
         txtbx_def.DragDrop += new DragEventHandler(Def_DragDrop);

     }

private void txtbx_def_DragDrop(object sender, DragEventArgs e)
    {
        txtbx_def.Text = e.Data.GetData(DataFormats.Text).ToString();

    }

    private void txtbx_def_DragEnter(object sender, DragEventArgs e)
    {

        if (e.Data.GetDataPresent(DataFormats.Text))
        {
            e.Effect = DragDropEffects.Copy;

        }
        else
        {
            e.Effect = DragDropEffects.None;
        }

    }



但即便是事件也不是Trigerred为什么????

它无法正常工作


But even the event is not Trigerred why????
It is not working

推荐答案

其中一个事件可能会触发:DragEnter事件。

但是得到了什么从桌面拖出的不是文本 - 它的文件。所以试试这个:

The chances are that one of the events is firing: the DragEnter event.
But what gets dragged from the desktop is not text - it's files. So try this:
private void textBox1_DragEnter(object sender, DragEventArgs e)
    {
    if (e.Data.GetDataPresent(DataFormats.FileDrop))
        {
        e.Effect = DragDropEffects.Copy;

        }
    else
        {
        e.Effect = DragDropEffects.None;
        }
    }

您应该会发现DragDrop事件现在被触发。

You should find the DragDrop event gets triggered now.


尝试两者似乎都不能正常工作
tried both seems not working at all unfortunately


这篇关于从桌面拖放并在Windows窗体中删除表单不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆