• 00
  • :
  • 00
  • :
  • 00

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;
    }
}

 

posted @ 2020-05-09 09:49  Garson_Zhang  阅读(1189)  评论(0编辑  收藏  举报