我需要从window2.panel中的window1.listbox drop中拖放。
写一下:
public partial class Form1 : Form
{
Routers r = new Routers();
public Form1()
{
InitializeComponent();
r.Show();
panel1.DragOver += new DragEventHandler(panel1_DragOver);
panel1.DragEnter += new DragEventHandler(panel1_DragEnter);
panel1.MouseUp += new MouseEventHandler(panel1_MouseUp);
panel1.DragDrop += new DragEventHandler(panel1_DragDrop);
panel1.AllowDrop = true;
this.AllowDrop = true;
this.DragDrop += new DragEventHandler(Form1_DragDrop);
}
void Form1_DragDrop(object sender, DragEventArgs e)
{
throw new NotImplementedException();
}
void panel1_DragDrop(object sender, DragEventArgs e)
{
if (isDrop == false)
{
isDrop = true;
Button b = new Button();
b.Text = (string)e.Data.GetData(DataFormats.StringFormat);
b.Location = new Point(e.X, e.Y);
this.panel1.Controls.Add(b);
}
}
void panel1_MouseUp(object sender, MouseEventArgs e)
{
if (isDrop)
{
isDrop = false;
}
}
bool isDrop = false;
void panel1_DragEnter(object sender, DragEventArgs e)
{
isDrop = false;
if (e.Data.GetDataPresent(DataFormats.StringFormat))
e.Effect = DragDropEffects.None;
}
void panel1_DragOver(object sender, DragEventArgs e)
{
e.Effect = DragDropEffects.None;
}
}
写下window2
public partial class Routers : Form
{
public Routers()
{
InitializeComponent();
this.listBox1.MouseDown += new MouseEventHandler(listBox1_MouseDown);
this.listBox1.DragOver += new DragEventHandler(listBox1_DragOver);
}
private void listBox1_DragOver(object sender, DragEventArgs e)
{
e.Effect = DragDropEffects.Move;
}
private void listBox1_MouseDown(object sender, MouseEventArgs e)
{
if (this.listBox1.SelectedItem != null)
{
string item = this.listBox1.SelectedItem.ToString();
this.listBox1.DoDragDrop(item, DragDropEffects.Move);
}
}
}
DragDrop事件不起作用。 并且在拖动时不会更改光标
答案 0 :(得分:0)
拖动目标时,您需要将e.Effect
设置为None
以外的其他内容。
答案 1 :(得分:0)
有关解决方案,请参阅此主题:
private void Form1_DragEnter(object sender, DragEventArgs e)
{
e.Effect = DragDropEffects.All;
}