I have a user control in c# on a windows form the drag drop events are only being fired around the edge of the control, does anyone have a clue what is causing this its driving me mad!?
private void flowDiagram1_DragEnter(object sender, DragEventArgs e)
{
if(e.Data.GetDataPresent(DataFormats.Text))
e.Effect = DragDropEffects.Move;
else
e.Effect = DragDropEffects.None;
}
private void flowDiagram1_DragOver(object sender, DragEventArgs e)
{
if (!m_bDragging)
flowDiagram1_DragDrop(sender, e);
}
private void flowDiagram1_DragDrop(object sender, DragEventArgs e)
{
MessageBox.Show("Drop");
}
I think you need to add this to drag over too:
if(e.Data.GetDataPresent(DataFormats.Text))
e.Effect = DragDropEffects.Move;
else
e.Effect = DragDropEffects.None;
Related
I have a problem. I have a group of picturebox to be dragged into one picturebox. How to disable a specific picturebox after being dragged?? So, it can't be dragged anymore.
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
pictureBox1.DoDragDrop(pictureBox1.Image, DragDropEffects.All);
}
private void pictureBox2_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.Bitmap))
e.Effect = DragDropEffects.Copy;
else
e.Effect = DragDropEffects.None;
}
private void pictureBox2_DragDrop(object sender, DragEventArgs e)
{
if ((e.Data.GetDataPresent(DataFormats.Bitmap)))
this.pictureBox2.Image = (Bitmap)(e.Data.GetData(DataFormats.Bitmap));
}
Instead of dragging the PictureBox's Image, drag the PictureBox.
When dropped, set it's Tag property to true.
In the MouseDown event, check if the Tag property is null, and drag only if it is.
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left && pictureBox1.Tag == null)
pictureBox1.DoDragDrop(pictureBox1.Image, DragDropEffects.All);
}
private void pictureBox2_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(typeof(PictureBox)))
e.Effect = DragDropEffects.Copy;
else
e.Effect = DragDropEffects.None;
}
private void pictureBox2_DragDrop(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(typeof(PictureBox)))
{
var picturebox = ((PictureBox)e.Data.GetData(typeof(PictureBox)));
picturebox.Tag = true;
this.pictureBox2.Image = picturebox.Image;
}
}
I have about 42 listboxes. I'm making a schedule using dragdrop and drag enter.
Sample:
private void listBox1_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.Text))
e.Effect = DragDropEffects.Copy;
else
e.Effect = DragDropEffects.None;
}
private void listBox1_DragDrop(object sender, DragEventArgs e)
{
if (listBox1.Items.Contains(e.Data.GetData(DataFormats.Text)))
{
MessageBox.Show("Duplicate");
}
else
{
listBox1.Items.Add(e.Data.GetData(DataFormats.Text));
}
}
Where should I put that error message so that it will not copy that data.
Add in each of the 42 listboxes only those items not yet added.
No error message is needed.
if (!listBox1.Items.Contains(e.Data.GetData(DataFormats.Text)))
listBox1.Items.Add(e.Data.GetData(DataFormats.Text));
So I made an application, and now I wanted to implement drag and drop feature.
Now while it works ANYWHERE in the Form, when I try to drag it on datagridview, I get the 'cancel' icon.
I have set datagridview allowdrop property to true, subscribed the drag events. Datagrid is visble and enabled.
Could it be a matter of its container (splitcontainer-> tabcontrol -> tabpage, but so far I made no difference by setting all of its containers property allowdrop=true)? What am I missing?
#EDIT the code for the events. Won't help much though.
void Form1_DragDrop(object sender, DragEventArgs e)
{
string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);
if (files.Count() == 1)
{
if (System.IO.File.Exists(files[0]) && files[0].Substring(files[0].LastIndexOf('.') + 1).ToLower().Contains("xls"))
{
loadExcel(files[0]);
}
}
}
void Form1_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop)) e.Effect = DragDropEffects.Copy;
}
void DGW_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.Text)) e.Effect = DragDropEffects.Copy;
}
void DGW_DragDrop(object sender, DragEventArgs e)
{
string head = (string)e.Data.GetData(DataFormats.Text);
//the same follow up code as in the form1_dragdrop
}
I'm trying a drag and drop for a tablelayoutpanel in a panel using winforms/c#, the drag of the tablelayout works successfully but the problem is that the tablelayoutpanel droped doesn't appear !!
any solution please ??
private void Registration_Load(object sender, EventArgs e)
{
panel2.AllowDrop = true;
tableLayoutPanel1.AllowDrop = true;
panel2.DragEnter += panel2_DragEnter;
panel2.DragDrop += panel2_DragDrop;
tableLayoutPanel1.MouseDown += tableLayoutPanel1_MouseDown;
}
private void panel2_DragEnter(object sender, DragEventArgs e)
{
e.Effect = DragDropEffects.Move;
}
private void panel2_DragDrop(object sender, DragEventArgs e)
{
((TableLayoutPanel)e.Data.GetData(typeof(TableLayoutPanel))).Parent (Panel)sender;
}
private void tableLayoutPanel1_MouseDown(object sender, MouseEventArgs e)
{
tableLayoutPanel1.DoDragDrop(tableLayoutPanel1, DragDropEffects.Move);
}
The code is inadequate, you'll need to at least set the Location property of the dropped TLP to ensure it is within the panel bounds and/or located at the mouse cursor. And the Z-order matters, setting the Parent property puts it at the bottom so it can easily be overlapped by other controls in the panel, you need BringToFront().
Try this:
private void panel2_DragEnter(object sender, DragEventArgs e) {
if (e.Data.GetDataPresent(typeof(TableLayoutPanel))) e.Effect = DragDropEffects.Move;
}
private void panel2_DragDrop(object sender, DragEventArgs e) {
var tlp = (TableLayoutPanel)e.Data.GetData(typeof(TableLayoutPanel));
tlp.Location = panel2.PointToClient(new Point(e.X, e.Y));
tlp.Parent = panel2;
tlp.BringToFront();
}
I want to make a DragAndDrop program for my project. I would like to a drag a picture from one picturebox to another and that's working pretty nice. But it won't go back to the orignal picturebox. I am working with 6 picturebox with 3 on first row and 3 on second row. I have drag the picturebox from first row to second and combine with the right description. Is it possible to make only 3 events (mouse_down, dragenter, dragdrop) and stil every picturebox will work because now I have to make 3 events for each picturebox and like this way i need 18 events.... Please help meeee
private void Form1_Load(object sender, EventArgs e)
{
pictureBox1.AllowDrop = true;
pictureBox2.AllowDrop = true;
pictureBox3.AllowDrop = true;
pictureBox4.AllowDrop = true;
pictureBox5.AllowDrop = true;
pictureBox6.AllowDrop = true;
pictureBox1.MouseDown += MouseDown;
pictureBox2.MouseDown += MouseDown;
pictureBox1.DragEnter += pictureBox2_DragEnter;
pictureBox1.DragDrop += pictureBox2_DragDrop;
pictureBox2.DragEnter += pictureBox2_DragEnter;
pictureBox2.DragDrop += pictureBox2_DragDrop;
}
/*private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
pictureBox1.DoDragDrop(pictureBox1.Image, DragDropEffects.Move);
}*/
private void pictureBox2_DragEnter(object sender, DragEventArgs e)
{
e.Effect = DragDropEffects.Move;
}
private void pictureBox2_DragDrop(object sender, DragEventArgs e)
{
pictureBox2.Image = (Image)e.Data.GetData(DataFormats.Bitmap);
pictureBox1.Image = null;
}private void pictureBox2_MouseDown(object sender, MouseEventArgs e)
{
pictureBox2.DoDragDrop(pictureBox1.Image, DragDropEffects.Move);
}
private void pictureBox1_DragEnter(object sender, DragEventArgs e)
{
e.Effect = DragDropEffects.Move;
}
private void pictureBox1_DragDrop(object sender, DragEventArgs e)
{
pictureBox1.Image = (Image)e.Data.GetData(DataFormats.Bitmap);
pictureBox2.Image = null;
}
private void pictureBox2_MouseDown(object sender, MouseEventArgs e)
{
pictureBox2.DoDragDrop(pictureBox1.Image, DragDropEffects.Move);
}
private void pictureBox1_DragEnter(object sender, DragEventArgs e)
{
e.Effect = DragDropEffects.Move;
}
private void pictureBox1_DragDrop(object sender, DragEventArgs e)
{
pictureBox1.Image = (Image)e.Data.GetData(DataFormats.Bitmap);
pictureBox2.Image = null;
}
You should utilize the sender object you get in those events.
Instead of setting the drag object to an image, I changed it to the PictureBox itself:
void pictureBox_MouseDown(object sender, MouseEventArgs e) {
DoDragDrop(sender, DragDropEffects.Move);
}
Notice this isn't pictureBox1_MouseDown, but just pictureBox_MouseDown. All of your PictureBox controls should wire their MouseDown events to this one method:
pictureBox1.MouseDown += pictureBox_MouseDown;
pictureBox2.MouseDown += pictureBox_MouseDown;
// etc...
You continue this process for the other events, too:
void pictureBox_DragEnter(object sender, DragEventArgs e) {
e.Effect = DragDropEffects.Move;
}
For the DragDrop event, cast the dragged object back into a PictureBox control:
void pictureBox_DragDrop(object sender, DragEventArgs e) {
PictureBox pb = e.Data.GetData(typeof(PictureBox)) as PictureBox;
if (pb != null) {
((PictureBox)sender).Image = pb.Image;
pb.Image = null;
}
}