gau*_*inc 18 c# drag-and-drop winforms
如何通过拖放将文件加载到表单?
会出现哪个事件?
我应该使用哪个组件?
以及如何在将文件拖放到表单后确定文件名和其他属性?
谢谢!
码
private void panel1_DragEnter(object sender, DragEventsArgs e){
if (e.Data.GetDataPresent(DataFormats.Text)){
e.Effect = DragDropEffects.Move;
MessageBox.Show(e.Data.GetData(DataFormats.Text).toString());
}
if (e.Data.GetDataPresent(DataFormats.FileDrop)){
}
}
Run Code Online (Sandbox Code Playgroud)
好的,这很有效.
文件怎么样?我怎样才能获得文件名和扩展名?
这只是一个dragEnter动作.
Sar*_*fan 30
此代码将循环并打印拖入窗口的所有文件的全名(包括扩展名):
if (e.Data.GetDataPresent(DataFormats.FileDrop))
{
string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);
foreach (string filePath in files)
{
Console.WriteLine(filePath);
}
}
Run Code Online (Sandbox Code Playgroud)
小智 5
请查看以下链接以获取更多信息
http://doitdotnet.wordpress.com/2011/12/18/drag-and-drop-files-into-winforms/
private void Form2_DragDrop(object sender, DragEventArgs e) {
if (e.Data.GetDataPresent(DataFormats.FileDrop)) {
string[] filePaths = (string[])(e.Data.GetData(DataFormats.FileDrop));
foreach (string fileLoc in filePaths) {
// Code to read the contents of the text file
if (File.Exists(fileLoc)) {
using (TextReader tr = new StreamReader(fileLoc)) {
MessageBox.Show(tr.ReadToEnd());
}
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
谢谢.
| 归档时间: |
|
| 查看次数: |
32147 次 |
| 最近记录: |