Drog and Drop TableLayoutPanel in a panel? - c#

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();
}

Related

C# Drag and Drop picturebox 2

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;
}
}

C# Drag and Drop - Muliple Pictures Box to Panel or Flow layout panel

I'm currently implementing drag & drop function in C# Window Forms. I've created multiple pictures box on left navigation, so user can drag and drop to right side panel.
But it does look like pictures box can not drop to the panel directly.
When i compile, it runs. But, when I drag and drop it makes following error:
"unable to cast object of type 'System.Windows.Forms.Panel' to type 'System.Windows.Forms.PictureBox'."
Please suggest how can i make drag and drop "picutre 1, 2, 3" to the panel accordingly.?
private void Form3_Load(object sender, EventArgs e)
{
pictureBox4.DragEnter += new DragEventHandler(pictureBox4_DragEnter);
pictureBox4.DragDrop += new DragEventHandler(pictureBox4_DragDrop);
pictureBox4.MouseDown += new MouseEventHandler(pictureBox4_MouseDown);
panel2.AllowDrop = true;
}
private void pictureBox4_DragDrop(object sender, DragEventArgs e)
{
PictureBox pb = (PictureBox)sender;
pb.Image = (Image)e.Data.GetData(DataFormats.Bitmap);
}
private void pictureBox4_MouseDown(object sender, MouseEventArgs e)
{
PictureBox pb = (PictureBox)sender;
pb.Select();
pb.DoDragDrop(pb.Image, DragDropEffects.Copy);
panel1.Show();
}
private void panel2_DragDrop(object sender, DragEventArgs e)
{
PictureBox pb = (PictureBox)sender;
pb.Image = (Image)e.Data.GetData(DataFormats.Bitmap);
}
private void panel2_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.Bitmap))
{
e.Effect = DragDropEffects.Copy;
}
else
{
e.Effect = DragDropEffects.None;
}
}
I suppose in this code your sender is actually a Panel. Check it's type in the debugger:
private void panel2_DragDrop(object sender, DragEventArgs e)
{
PictureBox pb = (PictureBox)sender;
pb.Image = (Image)e.Data.GetData(DataFormats.Bitmap);
}
As a good principle always do typecasting like this:
PictureBox pb = sender as PictureBox; // will give you null if it can't be cast to PictureBox
if(pb != null)
{
// do something...
}

How can I save definitely the new location of a TableLayoutPanel after a drag and drop in a panel?

I'm trying a drag and drop for a tablelayoutpanel in a panel using winforms/c#, the drag and drop of the tablelayoutpanel works successfully but how can I save the new location of the tablelayoutpanel using a button action ? 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)
{
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();
}
private void tableLayoutPanel1_MouseDown(object sender, MouseEventArgs e)
{
tableLayoutPanel1.DoDragDrop(tableLayoutPanel1, DragDropEffects.Move);
}
i find a solution to my problem. the idea is to save the location of the items in the database (Location.X a,d Location.Y) then in the load page we fix the location of the dropped item with the location saved in the database.

Drag and Drop both sides

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;
}
}

Drag&Drop: DataGridView rows "between" TabPage

[Solved] I want to drag some DataGridViewRows from a DataGridView, contained in a TabPage, to another DataGridView also contained in another TabPage. I have set the DataGridView's Event (How could I Drag and Drop DataGridView Rows under each other?), but i don't know how I can "navigate" between TabPages!
Here's a bare bone example how I can drag text from one textbox on a tab to another one on a separate tab:
private void textBox1_MouseDown(object sender, MouseEventArgs e)
{
textBox1.DoDragDrop(textBox1.Text, DragDropEffects.Copy | DragDropEffects.Move);
}
private void tabControl1_DragOver(object sender, DragEventArgs e)
{
Point location = tabControl1.PointToClient(Control.MousePosition);
for (int tab = 0; tab < tabControl1.TabCount; ++tab)
{
if (tabControl1.GetTabRect(tab).Contains(location))
{
tabControl1.SelectedIndex = tab;
break;
}
}
}
private void textBox2_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.Text))
e.Effect = DragDropEffects.Copy;
else
e.Effect = DragDropEffects.None;
}
private void textBox2_DragDrop(object sender, DragEventArgs e)
{
textBox2.Text = e.Data.GetData(DataFormats.Text).ToString();
}
NOTE: You must set AllowDrop property to true on the TabControl, and of course on the destination control.
Cheers
I have solved the question by myself (and #mrlucmorin) :
internal void dgv_MouseDown(object sender, MouseEventArgs e)
{
DataGridView dgv = (DataGridView)sender;
List<DataGridViewRow> result = new List<DataGridViewRow>();
foreach(DataGridViewRow row in dgv.SelectedRows)
{
result.Add(row);
}
dgv.DoDragDrop(result, DragDropEffects.Copy | DragDropEffects.Move);
}
private void dgv_DragEnter(object sender, DragEventArgs e)
{
e.Effect = DragDropEffects.Copy;
}
private void dgv_DragDrop(object sender, DragEventArgs e)
{
try
{
DataGridView dataGridView1 = (DataGridView)sender;
List<DataGridViewRow> rows = new List<DataGridViewRow>();
rows = (List<DataGridViewRow>)e.Data.GetData(rows.GetType());
//some stuff
}
}

Categories