-3
我需要從window1.listbox拖放到window2.panel中。爲什麼不工作拖放?
寫:
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;
}
}
寫窗口2
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事件無法正常工作。 ,拖動時不改變光標
我設置複製,鏈接,更 – Mediator 2011-03-06 14:26:28
不,你不是。您需要在「DragOver」和「DragEnter」中設置效果。 – SLaks 2011-03-06 14:34:29
SLaks,這是怎麼回事? – Mediator 2011-03-08 11:57:20