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

使RichTextBox可以拖拽

2012年03月14日 ⁄ 综合 ⁄ 共 1557字 ⁄ 字号 评论关闭

默认情况下,这个控件不支持拖拽,没有相应的事件。

我们只需重写一下相应的事件就可以实现拖拽.

public class RichTextBoxDemo : RichTextBox
    {
        
public delegate void mDrag(DragEventArgs e);
        
public event mDrag DragDrop;        

        public bool AllowDrag
        {
            
get { return base.AllowDrop; }
            
set { base.AllowDrop = value; }
        }

        //private string[] files;

        //public string[] Files
        
//{
        
//    get { return files; }            
        
//}

        
/// <summary>
        
/// 拖拽
        
/// </summary>
        
/// <param name="drgevent"></param>
        protected override void OnDragDrop(DragEventArgs e)
        {
            
base.OnDragDrop(e);
            
//if (this.DragDrop != null)
            
//{
            
//    if (e.Data.GetDataPresent(DataFormats.FileDrop))
            
//    {
            
//        string[] fs = (string[])e.Data.GetData(DataFormats.FileDrop, true);
            
//        this.files = fs;
            
//        this.DragDrop(fs[0]);
            
//    }
            
//}

            
if (this.DragDrop != null)
            {
                
this.DragDrop(e);
            }
        }

    }

这样,类RichTextBoxDemo就可以进行拖拽了。

应用:

 

this.RichTextBoxDemo1.AllowDrag = true;
this.RichTextBoxDemo1.DragDrop +=new RichTextBoxDemo1.mDrag(RichTextBoxDemo1_DragDrop);

private void RichTextBoxDemo1_DragDrop(DragEventArgs e)
        {
            MessageBox.Show(((
string[])e.Data.GetData(DataFormats.FileDrop, false))[0]);

        }

作者:冰碟
出处:http://www.cnblogs.com/icebutterfly/
版权:本文版权归作者和博客园共有
转载:欢迎转载,为了保存作者的创作热情,请按要求【转载】,谢谢
要求:未经作者同意,必须保留此段声明;必须在文章中给出原文连接;否则必究法律责任

抱歉!评论已关闭.