zoukankan      html  css  js  c++  java
  • C#TextBox控件拖拽实现获得文件路径,双击选择文件夹

    步骤:

    1、 通过DragEnter事件获得被拖入窗口的“信息”(可以是若干文件,一些文字等等),在DragDrop事件中对“信息”进行解析。
    2、接受拖放控件的AllowDrop属性必须设置成true;
    3、必须在DragEnter事件中设置好要接受拖放的效果,默认为无效果。(所以单独写DragDrop事件是不会具有拖拽功能的)

    private void textBox1_DragEnter(object sender, DragEventArgs e)
    {
        if (e.Data.GetDataPresent(DataFormats.FileDrop))
        {
            e.Effect = DragDropEffects.Link;
            this.textBox1.Cursor = System.Windows.Forms.Cursors.Arrow;  //指定鼠标形状(更好看)  
        }
        else
        {
            e.Effect = DragDropEffects.None;
        }
    }
    
    private void textBox1_DragDrop(object sender, DragEventArgs e)
    {
         //GetValue(0) 为第1个文件全路径
         //DataFormats 数据的格式,下有多个静态属性都为string型,除FileDrop格式外还有Bitmap,Text,WaveAudio等格式
         string path = ((System.Array)e.Data.GetData(DataFormats.FileDrop)).GetValue(0).ToString();
        textBox1.Text = path;
        this.textBox1.Cursor = System.Windows.Forms.Cursors.IBeam; //还原鼠标形状
    }
    txt_RootPath.AllowDrop = true;
    txt_RootPath.DragEnter += Txt_RootPath_DragEnter;
    txt_RootPath.DragDrop += Txt_RootPath_DragDrop;
    txt_RootPath.DoubleClick += Txt_RootPath_DoubleClick;
    private void Txt_RootPath_DoubleClick(object sender, EventArgs e)
    {
    
        FolderBrowserDialog openFileDialog = new FolderBrowserDialog();
        if (openFileDialog.ShowDialog() == DialogResult.OK)
        {
            (sender as TextBox).Text = openFileDialog.SelectedPath;
            this.LoadProject();
        }
    }
    
    private void Txt_RootPath_DragDrop(object sender, DragEventArgs e)
    {
        string path = ((System.Array)e.Data.GetData(DataFormats.FileDrop)).GetValue(0).ToString();
        (sender as TextBox).Text = path;
        (sender as TextBox).Cursor = System.Windows.Forms.Cursors.IBeam; //还原鼠标形状
        this.LoadProject();
    }
    
    private void Txt_RootPath_DragEnter(object sender, DragEventArgs e)
    {
        if (e.Data.GetDataPresent(DataFormats.FileDrop))
        {
            e.Effect = DragDropEffects.Link;
            (sender as Control).Cursor = System.Windows.Forms.Cursors.Arrow;  //指定鼠标形状(更好看)  
        }
        else
        {
            e.Effect = DragDropEffects.None;
        }
    }
    慎于行,敏于思!GGGGGG
  • 相关阅读:
    ConcurrentHashMap总结
    HashMap在多线程环境下操作可能会导致程序死循环
    oracle数据库的 to char 和to date 区别(时间格式化)
    SQL中的cast()函数用法
    常见的垃圾收集器有3类-java面试一
    mybatis中sql引用
    mysql find_in_set 查询
    用Redis实现微博关注关系的分析
    C#与C++相比较之STL篇(续一)
    Vite2.0 入门
  • 原文地址:https://www.cnblogs.com/GarsonZhang/p/12855406.html
Copyright © 2011-2022 走看看