AKever

导航

C# textbox 拖动文件APP

C# textbox 拖动文件 APP

vs2012,  .net framework4.5

-----------------------------------

1.创建新工程 

Other Languages -> Visual C# -> Windows For App

2.点击工程下面Form1.cs,出现编辑拖动编辑界面

3.可拖动Toolbox界面的工具编辑界面(如找不到Toolbox界面:菜单栏->WINDOW->Reset Window Layout)

-----------------------------------

4.双击 Form.cs 进入form1,可编辑界面

5.拖动Toolbox->Common controls->TextBox控件到Form1编辑界面(Form1.cs[Design])

6.双击 "Form1编辑界面"中的TextBox控件级,进入form1.cs编程文件

7. 添加代码:

public partial class Form1 : Form {
... ...
  private void textBox1_DragEnter(object sender, DragEventArgs e)
        {
            Console.WriteLine(" textBox1_DragEnter"); 
            //this.textBox1.Cursor = System.Windows.Forms.Cursors.Arrow;//指定鼠标形状 not use
            //this.textBox1.Cursor = System.Windows.Forms.Cursors.IBeam;  not use
            string fullPath = ((System.Array)e.Data.GetData(DataFormats.FileDrop)).GetValue(0).ToString();

            if (File.Exists(fullPath)) //如果是文件的话,则截取
            {
                //string path = fullPath.Substring(0, fullPath.LastIndexOf("\\"));
                textBox1.Text = fullPath; // fullPath文件路径
            }
            else
            {
                textBox1.Text = fullPath;
            }
        }
... ...
}

 

8.在 "Form1.Designer.cs"的 InitializeComponent()函数中 找到对应的控件,添加代码

this.textBox1.AllowDrop = true; //textBox1是控件名,设置允许拖入,及投入回调函数
this.textBox1.DragEnter += new System.Windows.Forms.DragEventHandler(this.textBox1_DragEnter);

 ---- THE END  !! 

posted on 2015-06-20 20:49  AKever  阅读(390)  评论(0)    收藏  举报