• 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
  • 相关阅读:
    牛式个数
    查找矩阵中某一元素
    破碎的项链
    找鞍点
    方阵形对角矩阵
    间接寻址
    Oracle安装配置的一些备忘点
    通过二维码在Windows、macOS、Linux桌面和移动设备之间传输文件
    httpd连接php-fpm
    nginx反代+lamp+MySQL主从同步的简单实现
  • 原文地址:https://www.cnblogs.com/GarsonZhang/p/12855406.html
Copyright © 2020-2023  润新知