现在的位置: 首页 > 综合 > 正文

RichTextBox的拖放功能DragDrop

2012年09月03日 ⁄ 综合 ⁄ 共 1140字 ⁄ 字号 评论关闭

参考:http://blog.iordanov.info/?p=32

实现RichTextBox的拖放功能只要设置EnableAutoDragDrop = true就自动实现了,非常简单。但是要实现一些个性化的控制,就有一点复杂了。

我需要实现的是:如果拖放文件进来,不想默认的一样嵌入一个对象连接,而是把文件上传;其他情况就想默认一样。

首先得设定AllowDrop = true;但是vs的控件的事件设定里面是没有DragEnter和DragDrop事件的,只能手工写代码。

   //添加事件
    private void AddRtb1Event()
        {
            rtb1.AllowDrop = true;
            
            rtb1.DragEnter += new DragEventHandler(rtb1_DragEnter);
            rtb1.DragDrop += new DragEventHandler(rtb1_DragDrop);
            
        }       
        private void rtb1_DragEnter(object sender, DragEventArgs e)
        {
            e.Effect = DragDropEffects.Copy  ;
            String[] supportedFormats = e.Data.GetFormats(true);
            if (supportedFormats != null)
            {
                List<string> sfList = new List<string>(supportedFormats);
                if (sfList.Contains(DataFormats.FileDrop.ToString()) )
                {
                    rtb1.EnableAutoDragDrop = false;
                }
                else
                {
                    rtb1.EnableAutoDragDrop = true;

                }
            }

            
        }

        private void rtb1_DragDrop(object sender, DragEventArgs e)
        {
            String[] supportedFormats = e.Data.GetFormats(true);
            if (supportedFormats != null)
            {
                List<string> sfList = new List<string>(supportedFormats);
                if (sfList.Contains(DataFormats.FileDrop.ToString()))
                {
                    string[] fileList = (string[])((System.Array)e.Data.GetData(DataFormats.FileDrop));
                    foreach (string fileName in fileList)
                    {
                        MessageBox.Show(fileName);
                    }
                }
            }
            rtb1.EnableAutoDragDrop = true;
        }

    }

 

抱歉!评论已关闭.