20.3 How can I drag file names from Windows Explorer and drop them into a listbox? Place a ListBox on your form, set its AllowDrop property and handle both DragEnter and DragDrop as below.
private void listBox1_DragEnter(object sender, System.Windows.Forms.DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop))
e.Effect = DragDropEffects.All;
else
e.Effect = DragDropEffects.None;
}
private void listBox1_DragDrop(object sender, System.Windows.Forms.DragEventArgs e)
{
string[] files = (string[])e.Data.GetData("FileDrop", false);
foreach (string s in files)
{
//just filename
listBox1.Items.Add(s.Substring(1 + s.LastIndexOf(@"\")));
//or fullpathname
// listBox1.Items.Add(s);
}
}
private void listBox1_DragEnter(object sender, System.Windows.Forms.DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop))
e.Effect = DragDropEffects.All;
else
e.Effect = DragDropEffects.None;
}
private void listBox1_DragDrop(object sender, System.Windows.Forms.DragEventArgs e)
{
string[] files = (string[])e.Data.GetData("FileDrop", false);
foreach (string s in files)
{
//just filename
listBox1.Items.Add(s.Substring(1 + s.LastIndexOf(@"\")));
//or fullpathname
// listBox1.Items.Add(s);
}
}
解决方案 »
- IComparable接口问题
- 求助,关于c#在winform中生成excel的删除空白行问题
- 有什么软件能生成intermec打印机的 IPL文件吗
- 怎样获得IDataReader包含的列集合?
- 发送邮件身份验证问题!急急急
- ★★ 如何修改CheckedListBox 选定项对应的文本值
- int icdev=IC.auto_int(0,9600)这句报外部组件发生异常
- 散分了,ado.net讨论关于批量修改数据提交的合理解决方案
- 点击项目文件VS说无法从web服务器获取项目文件
- 谁有windows常的那些ICO按钮啊?比较保存、打开等等之类的常用的ICO文件。
- 请问如何用C#中的webrequest,webresponse实现断点续传
- 很值得问的问题:动态产生对象,能动态命名吗??(在线等待)
{
Graphics g = e.Graphics;
g.DrawImage(bitmap , new Rectangle(0, 0, 700,500),0,0, bitmap.Width, bitmap.Height,GraphicsUnit.Pixel);
} private void Form1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
Rectangle r = new Rectangle(0, 0, bitmap.Width, bitmap.Height);
if(r.Contains(new Point(e.X, e.Y)))
{
DoDragDrop(bitmap, DragDropEffects.Copy);
}
} private void Form1_DragEnter(object sender, System.Windows.Forms.DragEventArgs e)
{
if(e.Data.GetDataPresent(typeof(Bitmap)))
{
e.Effect = DragDropEffects.Copy;
}
else
{
e.Effect = DragDropEffects.None;
}
} private void Form1_DragDrop(object sender, System.Windows.Forms.DragEventArgs e)
{
object data = e.Data.GetData(typeof(Bitmap));
if(data != null)
{
bitmap = (Bitmap)data;
this.Invalidate(this.ClientRectangle);
}
}
}
你参考一下!给你个拖放图像的例子!希望对你有帮助!