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;
}
}
Related
private void lbxMevcutOlmayanUnvanlar_DragOver(object sender, DragEventArgs e)
{
if (e.KeyState == 1)
{
e.Effect = DragDropEffects.Copy;
}
}
private void lbxMevcutOlmayanUnvanlar_DragDrop(object sender, DragEventArgs e)
{ MevcutOlmayanUnvanlarDataSource.Rows.Add((DataRow)e.Data.GetData(typeof(DataRow)), (DataRow)e.Data.GetData(typeof(DataRow)));//I cannot access the data in e.data
}
private void lbxMevcutUnvanlar_MouseDown(object sender, MouseEventArgs e)
{
Point point = new Point(e.X, e.Y);
int sira = lbxMevcutUnvanlar.IndexFromPoint(point);
if (e.Button == MouseButtons.Left)
{
lbxMevcutUnvanlar.DoDragDrop(lbxMevcutUnvanlar.Items[sira], DragDropEffects.Copy);
}
}
enter image description here
enter image description here
The data comes in rows one below the other. actually it should come as columns under the name of "bipkod" and "bipaciklama".
I'm new to programming. I'm making an application for first-grade kids in Visual Studio 2019 WindowsForm and one level is about dragging a picture from a PictureBox in an empty PictureBox that has a label with a simple definition.
So if I have 2 PictureBox that show let say a dog and a chicken and 2 empty PictureBox (1 with label "bones" and one with label "grain"). I want to drag the dog picture into the PictureBox with the label "bones" and the chicken in the one with "grain" label and if correct I show a text "great job" and if not I show a text "try again".
I can drag the pictures but I can't find a method to check if is correct.
Can anybody help me with that?
Here is my code so far:
private void NivelulDoi_Load(object sender, EventArgs e)
{
customPictureBox1.AllowDrop = true;
customPictureBox2.AllowDrop = true;
customPictureBox3.AllowDrop = true;
}
private void customPictureBox1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
customPictureBox1.DoDragDrop(customPictureBox1.Image, DragDropEffects.Copy);
}
}
private void customPictureBox1_DragDrop(object sender, DragEventArgs e)
{
var data = e.Data.GetData(DataFormats.FileDrop);
if (data != null)
{
var fileNames = data as string[];
if (fileNames.Length > 0)
{
customPictureBox1.Image = Image.FromFile(fileNames[0]);
}
}
}
private void customPictureBox1_DragEnter(object sender, DragEventArgs e)
{
e.Effect = DragDropEffects.Copy;
}
private void customPictureBox2_DragEnter(object sender, DragEventArgs e)
{
Dragging(e);
}
private void customPictureBox2_DragDrop(object sender, DragEventArgs e)
{
customPictureBox2.Image = (Bitmap)e.Data.GetData(DataFormats.Bitmap, true);
// figure out how to check if correct
/*if (customPictureBox2.Image == (Bitmap)e.Data.GetData(DataFormats.Bitmap, true))
{
label1.Text = "ai reusit";
}
else
{
label1.Text = "mai incearca";
}*/
}
private void customPictureBox3_DragEnter(object sender, DragEventArgs e)
{
Dragging(e);
}
private void customPictureBox3_DragDrop(object sender, DragEventArgs e)
{
customPictureBox3.Image = (Bitmap)e.Data.GetData(DataFormats.Bitmap, true);
}
private void customPictureBox4_DragEnter(object sender, DragEventArgs e)
{
Dragging(e);
}
private void customPictureBox4_DragDrop(object sender, DragEventArgs e)
{
customPictureBox4.Image = (Bitmap)e.Data.GetData(DataFormats.Bitmap, true);
}
private static void Dragging(DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.Bitmap) && (e.AllowedEffect & DragDropEffects.Copy) != 0)
{
e.Effect = DragDropEffects.Copy;
}
else
{
e.Effect = DragDropEffects.None;
}
}
}
}
Thank you.
A simple way is to define a variable to save the Name of "picturebox drag" and a dictionary to save "drag-drop" pair.
Here is the demo.
string pictureBoxFrom = "";
Dictionary<string, string> picDict = new Dictionary<string, string>();
public Form1()
{
InitializeComponent();
pictureBox2.AllowDrop = true;
pictureBox4.AllowDrop = true;
pictureBox1.MouseDown += pictureBox_MouseDown;
pictureBox3.MouseDown += pictureBox_MouseDown;
pictureBox2.DragEnter += pictureBox_DragEnter;
pictureBox2.DragDrop += pictureBox_DragDrop;
pictureBox4.DragEnter += pictureBox_DragEnter;
pictureBox4.DragDrop += pictureBox_DragDrop;
picDict.Add("pictureBox1", "pictureBox2"); // drag 1 to 2
picDict.Add("pictureBox3", "pictureBox4"); // drag 3 to 4
}
private void pictureBox_MouseDown(object sender, MouseEventArgs e)
{
pictureBoxFrom = (sender as PictureBox).Name;
var img = (sender as PictureBox).Image;
if (img == null) return;
if (DoDragDrop(img, DragDropEffects.Move) == DragDropEffects.Move)
{
//(sender as PictureBox).Image = null;
}
}
private void pictureBox_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.Bitmap))
e.Effect = DragDropEffects.Move;
}
private void pictureBox_DragDrop(object sender, DragEventArgs e)
{
if (picDict.ContainsKey(pictureBoxFrom) && picDict[pictureBoxFrom].Equals((sender as PictureBox).Name))
{
var bmp = (Bitmap)e.Data.GetData(DataFormats.Bitmap);
(sender as PictureBox).Image = bmp;
MessageBox.Show("great job");
}
else
{
MessageBox.Show("try again");
}
}
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'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 have a card game application, and I want to create a simple animation that will make the button move when it is clicked and dragged.
I have tried:
bool _Down = false;
private void button1_MouseDown(object sender, MouseEventArgs e)
{
_Down = true;
}
private void button1_MouseUp(object sender, MouseEventArgs e)
{
_Down = false;
button1.Location = e.Location;
}
private void button1_MouseMove(object sender, MouseEventArgs e)
{
if (_Down)
{
button1.Location = e.Location;
}
}
This doesn't work either. The effect I get is that when the button is clicked and dragged, the button is not visible until the mouse is released, and also, the button doesn't actually stay at the location of the mouse.
I also tried:
bool _Down = false;
private void button1_MouseDown(object sender, MouseEventArgs e)
{
_Down = true;
}
private void button1_MouseUp(object sender, MouseEventArgs e)
{
_Down = false;
button1.Location = Cursor.Position;
}
private void button1_MouseMove(object sender, MouseEventArgs e)
{
if (_Down)
{
button1.Location = Cursor.Position;
}
}
This works better than the first one as the button is visible when dragged and stops at mouse position, but the only problem is that Cursor.Position returns the cursor position in relativeness to the screen, not the form therefore. The button doesn't actually move at the pace of the cursor.
What can I do to achieve what I want?
Moving Control at runtime is very easy:
Point downPoint;
private void button1_MouseDown(object sender, MouseEventArgs e)
{
downPoint = e.Location;
}
private void button1_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left) {
button1.Left += e.X - downPoint.X;
button1.Top += e.Y - downPoint.Y;
}
}
Try this
private void button1_MouseUp(object sender, MouseEventArgs e)
{
_Down = false;
button1.Location = PointToClient(Cursor.Position);
}
private void button1_MouseMove(object sender, MouseEventArgs e)
{
if (_Down)
{
button1.Location = PointToClient( Cursor.Position);
}
}