Drag and Drop text containing file in textbox.

Introduction

In many of application we need drag and drop functionality; in that same context this article helps you to drag and drop text containing file in to text box. This is a simplest method which I found for text file drag and drop. In case of drag and drop Microsoft .NET has provided support by may properties to control, events and methods.

In this application you can drag and drop a file which may of any extension but contains text in it. For text container I used textbox. For performing drag and drop I handled DragEnterDragDrop event of TextBox and the AllowDrop property.

Property

AllowDrop

This property is set to true for any control for which we want drag and drop support. This control may be a TextBox which holds data.

Events

DragEnter

This event is fired whenever the mouse with a pressed left button will enter into a control's region. At that time, the control's DragEnter event is fired. Generally, we require to check the type of data available, which is done as follows.

 Collapse
private void txtFileContent_DragEnter(object sender, DragEventArgs e)
{
    // If file is dragged, show cursor "Drop allowed"

    if (e.Data.GetDataPresent(DataFormats.FileDrop))
        e.Effect = DragDropEffects.Copy;
    else
    e.Effect = DragDropEffects.None;
}

DragDrop

This event is fired when the left mouse button is released by the user and the user intends to drop data on the target control. Generally, the target control accepts data here as follows.

 Collapse
private void txtFileContent_DragDrop(object sender, DragEventArgs e)
{
    try
    { 
        Array a = (Array) e.Data.GetData(DataFormats.FileDrop);
        if(a != null)
        {
            string s = a.GetValue(0).ToString();
            this.Activate();
        OpenFile(s);
        }
    }
    catch(Exception ex)
    {
        MessageBox.Show("Error in DragDrop function: " + ex.Message);
    }
}

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Shinde Sachin

Software Developer (Senior)
Patni, India
India India

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值