问题描述
我正在创建一个简单的拖拽和应用程序下降。我正在使用Windows 10操作系统,Visual Studio 2015&使用c#创建Windows应用程序。
我在表单上添加了面板。对于面板,AllowDrop为true。
我在Panel的事件窗口中添加了3个事件:private void panel1_DragEnter(object sender,DragEventArgs e)
     {
e.Effect = DragDropEffects.All;
}
private void panel1_DragOver(object sender,DragEventArgs e)
{
e.Effect = DragDropEffects.All;
}
private void panel1_DragDrop(object sender,DragEventArgs e)
{
$
}
但它不允许我在这里删除文件。请告诉我代码有什么问题。
G Goyal
Hi,
I am creating a simple application for drag & drop. I am using Windows 10 OS, Visual Studio 2015 & creating windows application using c#.
I have added panel on the form. AllowDrop is true for the panel.
I have added 3 events from event windows for Panel : private void panel1_DragEnter(object sender, DragEventArgs e)
{
e.Effect = DragDropEffects.All;
}
private void panel1_DragOver(object sender, DragEventArgs e)
{
e.Effect = DragDropEffects.All;
}
private void panel1_DragDrop(object sender, DragEventArgs e)
{
}
But it is not allowing me to drop files here. Please let me know what is wrong with the code.
G Goyal
推荐答案
Panel是一个容器控件,用于放置一些其他控件,如果你想将一些文件拖到窗体中,那么如何使用ListBox呢?
Panel is a container control used to place some of another controls, if you want to drag some files to the form, how about to use ListBox instead?
我做了一个简单的使用ListBox演示,你可以参考如下:
I made a simple demo with ListBox, you can refer to it as below:
public partial class Form3 : Form
{
public Form3()
{
InitializeComponent();
}
public string FilePath;
private void Form3_Load(object sender, EventArgs e)
{
listBox1.AllowDrop = true;
}
private void listBox1_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop))
{
e.Effect = DragDropEffects.All;
}
else
{
e.Effect = DragDropEffects.None;
}
}
private void listBox1_DragDrop(object sender, DragEventArgs e)
{
string[] s = (string[])e.Data.GetData(DataFormats.FileDrop, false);
for (int i = 0; i < s.Length; i++)
{
listBox1.Items.Add(s[i]);
}
}
//open the file when select one
private void button1_Click(object sender, EventArgs e)
{
string filePath = GetListBoxItem();
if (!string.IsNullOrEmpty(filePath))
{
System.Diagnostics.Process.Start(filePath);
}
}
//get item
public string GetListBoxItem()
{
string filePath = string.Empty;
bool isSelected = IsListBoxSelected();
if (isSelected == true)
{
string listBoxItemValue = listBox1.SelectedItem.ToString();
filePath = listBoxItemValue;
}
else
{
MessageBox.Show("ListBox must be selected.");
}
return filePath;
}
//determine if select a file
public bool IsListBoxSelected()
{
bool selected;
if (listBox1.SelectedIndex == -1)
{
selected = false;
}
else
{
selected = true;
}
return selected;
}
}
问候,
Stanly
这篇关于拖拽放下怎么样?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!