C# drag and drop picturebox - c#

I have 7 pictureboxes and I want to drag and drop each one of them. I have made the drag and drop but it takes with it the original picturebox which I drag it doesn't leave it on its place. This is my code:
this.pbAND.MouseDown += pictureBox_MouseDown;
pbAND.MouseMove += pictureBox_MouseMove;
pbAND.MouseUp += pictureBox_MouseUp;
this.pbOR.MouseDown += pictureBox_MouseDown;
pbOR.MouseMove += pictureBox_MouseMove;
pbOR.MouseUp += pictureBox_MouseUp;
private void pictureBox_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
p = (PictureBox)sender;
downPoint = e.Location;
var dragImage = (Bitmap)p.Image;
IntPtr icon = dragImage.GetHicon();
Cursor.Current = new Cursor(icon);
p.Parent = this;
p.BringToFront();
DestroyIcon(icon);
}
}
private void pictureBox_MouseMove(object sender, MouseEventArgs e)
{
p = (PictureBox)sender;
if (e.Button == MouseButtons.Left)
{
p.Left += e.X - downPoint.X;
p.Top += e.Y - downPoint.Y;
}
}
private void pictureBox_MouseUp(object sender, MouseEventArgs e)
{
p = (PictureBox)sender;
Control c = GetChildAtPoint(new Point(p.Left - 1, p.Top));
if (c == null) c = this;
Point newLoc = c.PointToClient(p.Parent.PointToScreen(p.Location));
p.Parent = c;
p.Location = newLoc;
}

but it takes with it the original picturebox which I drag it doesn't
leave it on its place.
So you want to make a copy when the PictureBox is dropped?
In the MouseDown() handler, store the original location in the Tag() property:
private void pictureBox_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
p = (PictureBox)sender;
p.Tag = p.Location; // <-- store the Location in the Tag() property
// ... rest of the existing code ...
}
}
In the MouseUp() handler, put a New PictureBox in the current location and reset the original:
private void pictureBox_MouseUp(object sender, MouseEventArgs e)
{
p = (PictureBox)sender;
// create a new PictureBox that looks like the original:
PictureBox PB = new PictureBox();
PB.Size = p.Size;
PB.Image = p.Image;
PB.SizeMode = p.SizeMode;
PB.BorderStyle = p.BorderStyle;
// etc...make it look the same
// ...and place it:
Control c = GetChildAtPoint(new Point(p.Left - 1, p.Top));
if (c == null) c = this;
Point newLoc = c.PointToClient(p.Parent.PointToScreen(p.Location));
PB.Parent = c;
PB.Location = newLoc;
p.Parent.Controls.Add(PB); // <-- add new PB to the form!
// put the original back where it started:
p.Location = (Point)p.Tag;
}

Related

How to store locations properly

I have this form that allows the user to create pictureboxes by clicking and dragging them around with mouse movement. How can I store the coordinates (locations) of where the pictureboxes are each time one is added or when one is moved to a new spot? I am trying to store them in a list as strings for exportation.
private void Form_MouseClick(object sender, MouseEventArgs e)
{
// create new control
PictureBox pictureBox = new PictureBox();
pictureBox.Location = new Point(e.X, e.Y);
pictureBox.BackColor = Color.Red;
this.Controls.Add(pictureBox);
// bind event for each PictureBox
pictureBox.MouseDown += pictureBox_MouseDown;
pictureBox.MouseUp += pictureBox_MouseUp;
pictureBox.MouseMove += pictureBox_MouseMove;
}
Point mouseDownPoint = Point.Empty;
Rectangle rect = Rectangle.Empty;
bool isDrag = false;
private void pictureBox_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
mouseDownPoint = e.Location;
rect = (sender as PictureBox).Bounds;
}
}
private void pictureBox_MouseUp(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
if (isDrag)
{
isDrag = false;
(sender as PictureBox).Location = rect.Location;
this.Refresh();
}
reset();
}
}
private void pictureBox_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
isDrag = true;
rect.Location = this.PointToClient((sender as PictureBox).PointToScreen(new Point(e.Location.X - mouseDownPoint.X, e.Location.Y - mouseDownPoint.Y)));
this.Refresh();
}
}
private void reset()
{
mouseDownPoint = Point.Empty;
rect = Rectangle.Empty;
isDrag = false;
}
To save the loaction of each picturebox, you can define a dictionary to save the key-value pair.
First, define the dictionary box_location_Pairs as a global variable.
Then add the new item to dictionary when "click to a create new picturebox".
Dictionary<PictureBox, Point> box_location_Pairs = new Dictionary<PictureBox, Point>();
// ...
private void Form_MouseClick(object sender, MouseEventArgs e)
{
PictureBox pictureBox = new PictureBox();
pictureBox.Location = new Point(e.X, e.Y);
box_location_Pairs.Add(pictureBox, pictureBox.Location);
// ...
}
If the picturebox is moved, modify its value.
private void pictureBox_MouseUp(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
if (isDrag)
{
isDrag = false;
(sender as PictureBox).Location = rect.Location;
box_location_Pairs[sender as PictureBox] = rect.Location; // modifty key-value pair
this.Refresh();
}
reset();
}
}

How to at runtime allow a user to create a new Picturebox Visual Studio C# Windows Forms Apps

I am trying to create a Form in Visual Studio Windows Forms Apps C# in which during runtime the user or operator can create a new picturebox with a left mouse click and then have the ability to move each picturebox created.
I really do not know where to begin. Anyone have any ideas?
To add a new PictureBox dynamically, you can subscribe to the event Form_MouseClick and create the PictureBox like this:
public Form1()
{
InitializeComponent();
this.MouseClick += Form_MouseClick;
}
private void Form_MouseClick(object sender, MouseEventArgs e)
{
PictureBox pictureBox = new PictureBox();
// cursor location
pictureBox.Location = new Point(e.X, e.Y);
pictureBox.BackColor = Color.Red;
this.Controls.Add(pictureBox);
}
To drag and move the PictureBox, you also nee to subscribe to the event MouseDown, MouseUp, and MouseMove.
Here is a simple demo you can refer to.
private void Form_MouseClick(object sender, MouseEventArgs e)
{
// create new control
PictureBox pictureBox = new PictureBox();
pictureBox.Location = new Point(e.X, e.Y);
pictureBox.BackColor = Color.Red;
this.Controls.Add(pictureBox);
// bind event for each PictureBox
pictureBox.MouseDown += pictureBox_MouseDown;
pictureBox.MouseUp += pictureBox_MouseUp;
pictureBox.MouseMove += pictureBox_MouseMove;
}
Point mouseDownPoint = Point.Empty;
Rectangle rect = Rectangle.Empty;
bool isDrag = false;
private void pictureBox_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
mouseDownPoint = e.Location;
rect = (sender as PictureBox).Bounds;
}
}
private void pictureBox_MouseUp(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
if (isDrag)
{
isDrag = false;
(sender as PictureBox).Location = rect.Location;
this.Refresh();
}
reset();
}
}
private void pictureBox_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
isDrag = true;
rect.Location = this.PointToClient((sender as PictureBox).PointToScreen(new Point(e.Location.X - mouseDownPoint.X, e.Location.Y - mouseDownPoint.Y)));
this.Refresh();
}
}
private void reset()
{
mouseDownPoint = Point.Empty;
rect = Rectangle.Empty;
isDrag = false;
}
You can add the form elements during runtime:
var picture = new PictureBox
{
Name = "pictureBox",
Size = new Size(16, 16),
Location = new Point(100, 100),
Image = Image.FromFile("test.jpg"),
};
this.Controls.Add(picture);
Afterward, you can move it with the Mouse Events

how to open a new window with double click on control added at runtime C#?

I am adding a moveable PictureBox control at runtime in my application as shown in the code below. I would like that when I double click on the PictureBox control, which is added at runtime, a new window opens. I tried to add an event of double click, but it's not working. Can you guide me on this how can I open a new window when double clicking on the added PictureBox control at runtime?
PictureBox[] PicArray;
PictureBox pb = new PictureBox();
pb.BackgroundImage = Image.FromFile(#"C:\Esp_Calculator\ESP Planner\1206570458690641104johnny_automatic_bridge.svg.med.png");
pb.Location = new Point(0, 0);
pb.BackColor = Color.Transparent;
pb.BackgroundImageLayout = ImageLayout.Zoom;
pb.MouseDown += new MouseEventHandler(this.drawArea_MouseDown);
pb.MouseMove += new MouseEventHandler(this.drawArea_MouseMove);
pb.MouseDoubleClick += pb_MouseDoubleClick;
drawArea.Controls.Add(pb);
List<PictureBox> lblList = new List<PictureBox>();
foreach (Control d in Controls)
if (d is PictureBox)
lblList.Add((PictureBox)d);
PicArray = lblList.ToArray();
private void drawArea_MouseDown(object sender, MouseEventArgs e)
{
move = e.Location;
}
private void drawArea_MouseMove(object sender, MouseEventArgs e)
{
Control Pb = (Control)sender;
if ((Control.ModifierKeys & Keys.Control) != 0)
{
if ((Control.MouseButtons & MouseButtons.Left) != 0)
{
Pb.Left += e.X - move.X;
Pb.Top += e.Y - move.Y;
}
}
}
void pb_MouseDoubleClick(object sender, System.Windows.Forms.MouseEventArgs e)
{
Editor ed = new Editor();
ed.Show();
}
I got it. DoubleClick does the trick instead of MouseDoubleClick Event. Thanks . Here is the answer and working code:
PictureBox[] PicArray;
PictureBox pb = new PictureBox();
pb.BackgroundImage = Image.FromFile(#"C:\Esp_Calculator\ESP Planner\1206570458690641104johnny_automatic_bridge.svg.med.png");
pb.Location = new Point(0, 0);
pb.BackColor = Color.Transparent;
pb.BackgroundImageLayout = ImageLayout.Zoom;
pb.MouseDown += new MouseEventHandler(this.drawArea_MouseDown);
pb.MouseMove += new MouseEventHandler(this.drawArea_MouseMove);
pb.DoubleClick += pb_DoubleClick;
drawArea.Controls.Add(pb);
List<PictureBox> lblList = new List<PictureBox>();
foreach (Control d in Controls)
if (d is PictureBox)
lblList.Add((PictureBox)d);
PicArray = lblList.ToArray();
private void drawArea_MouseDown(object sender, MouseEventArgs e)
{
move = e.Location;
}
private void drawArea_MouseMove(object sender, MouseEventArgs e)
{
Control Pb = (Control)sender;
if ((Control.ModifierKeys & Keys.Control) != 0)
{
if ((Control.MouseButtons & MouseButtons.Left) != 0)
{
Pb.Left += e.X - move.X;
Pb.Top += e.Y - move.Y;
}
}
}
void pb_DoubleClick(object sender, System.EventArgs e)
{
Editor ed = new Editor();
ed.Show();
}

C# Drag & Drop labels

i have a problem with drag&drop. I have code:
void labelWorker_MouseEvent(object sender, MouseEventArgs e)
{
Label labelWorker = (Label)sender;
labelWorker.DoDragDrop(labelWorker, DragDropEffects.Move);
labelWorker.MouseDown += new MouseEventHandler(labelWorker_MouseDown);
labelWorker.MouseMove += new MouseEventHandler(labelWorker_MouseMove);
labelWorker.MouseUp += new MouseEventHandler(labelWorker_MouseUp);
}
bool isDragged = false;
Point ptOffset;
void labelWorker_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
Label labelWorker = (Label)sender;
isDragged = true;
Point ptStartPosition = labelWorker.PointToScreen(new Point(e.X, e.Y));
ptOffset = new Point();
ptOffset.X = labelWorker.Location.X - ptStartPosition.X;
ptOffset.Y = labelWorker.Location.Y - ptStartPosition.Y;
}
else
{
isDragged = false;
}
}
void labelWorker_MouseMove(object sender, MouseEventArgs e)
{
Label labelWorker = (Label)sender;
if (isDragged)
{
Point newPoint = labelWorker.PointToScreen(new Point(e.X, e.Y));
newPoint.Offset(ptOffset);
labelWorker.Location = newPoint;
}
}
void labelWorker_MouseUp(object sender, MouseEventArgs e)
{
Label labelWorker = (Label)sender;
isDragged = false;
}
Label starts freaking out when I try to move it. I think the problem is in
void labelWorker_MouseMove(object sender, MouseEventArgs e)
{
Label labelWorker = (Label)sender;
if (isDragged)
{
Point newPoint = labelWorker.PointToScreen(new Point(e.X, e.Y));
newPoint.Offset(ptOffset);
labelWorker.Location = newPoint;
}
}
Ok. I just made it :)
bool clicked = false;
int iOldX;
int iOldY;
int iClickX;
int iClickY;
void labelWorker_MouseDown(object sender, MouseEventArgs e)
{
Label labelWorker = (Label)sender;
if (e.Button == MouseButtons.Left)
{
Point p = ConvertFromChildToForm(e.X, e.Y, labelWorker);
iOldX = p.X;
iOldY = p.Y;
iClickX = e.X;
iClickY = e.Y;
clicked = true;
}
}
void labelWorker_MouseMove(object sender, MouseEventArgs e)
{
Label labelWorker = (Label)sender;
if (clicked)
{
Point p = new Point(); // New Coordinate
p.X = e.X + labelWorker.Left;
p.Y = e.Y + labelWorker.Top;
labelWorker.Left = p.X - iClickX;
labelWorker.Top = p.Y - iClickY;
}
}
void labelWorker_MouseUp(object sender, MouseEventArgs e)
{
clicked = false;
}
private Point ConvertFromChildToForm(int x, int y, Control control)
{
Point p = new Point(x, y);
control.Location = p;
return p;
}

C# drag controls around a panel

i am developing a system which allow user to drag objects around within a same panel, i went through some research and founds that i should use mouse events like mouse_up, mouse_down and mouse_move.
The the program will generate 3 picturebox and allow the user to drag around the every picturebox within the panel, but the program i code did not work perfectly as when i drag over a picturebox, the picturebox will move, but not according to my mouse cursor location, it is somewhere else, besides, when dragging, there is picturebox shadows in the panel, i've tried those update(),refresh(), and invalidate() but it seems not useful for me. Below are my codes, thanks for helping
public partial class Form1 : Form
{
List<PictureBox> pictureBoxList = new List<PictureBox>();
private bool isDragging = false;
public Form1()
{
InitializeComponent();
for (int i = 0; i < 3; i++)
{
PictureBox picture = new PictureBox
{
Name = "pictureBox" + i,
Size = new Size(20, 20),
Location = new Point(i * 40, i * 40),
BorderStyle = BorderStyle.FixedSingle,
SizeMode = PictureBoxSizeMode.Zoom,
ImageLocation = "A.jpg"
};
pictureBoxList.Add(picture);
foreach (PictureBox p in pictureBoxList)
{
p.MouseDown += new MouseEventHandler(c_MouseDown);
p.MouseMove += new MouseEventHandler(c_MouseMove);
p.MouseUp += new MouseEventHandler(c_MouseUp);
pnlDisplayImage.Controls.Add(p);
pnlDisplayImage.Refresh();
}
}
}
void c_MouseDown(object sender, MouseEventArgs e)
{
isDragging = true;
}
void c_MouseMove(object sender, MouseEventArgs e)
{
if (isDragging == true) {
Control c = sender as Control;
for (int i = 0; i < pictureBoxList.Count(); i++)
{
if (c.Equals(pictureBoxList[i]))
{
pictureBoxList[i].Location = new Point(e.X, e.Y);
}
}
}
}
void c_MouseUp(object sender, MouseEventArgs e)
{
PictureBox c = sender as PictureBox;
isDragging = false;
for (int i = 0; i < pictureBoxList.Count(); i++) {
if (c.Equals(pictureBoxList[i])){
pictureBoxList[i].Location = new Point(e.X, e.Y);
}
}
}
private void pnlDisplayImage_Paint(object sender, PaintEventArgs e)
{
foreach (PictureBox p in pictureBoxList)
{
pnlDisplayImage.Controls.Add(p);
}
}
}
Finally I've found what are the problems that caused my program not running as my expectations. The main problem is that I accidentally put the foreach loop inside the for loop which I used to create pictureBox, this problem caused the pictureBox comes out some shadows effect while dragging during run time due to there are few same pictureBox. Also, I have change a little bit of the codes and it now run as what I expected. Below are the code that I want for answer.
public partial class Form1 : Form
{
List<PictureBox> pictureBoxList = new List<PictureBox>();
private bool isDragging = false;
Point move;
public Form1()
{
InitializeComponent();
for (int i = 0; i < 3; i++)
{
PictureBox picture = new PictureBox
{
Name = "pictureBox" + i,
Size = new Size(20, 20),
Location = new Point(i * 40, i * 40),
BorderStyle = BorderStyle.FixedSingle,
SizeMode = PictureBoxSizeMode.Zoom,
ImageLocation = "A.jpg"
};
pictureBoxList.Add(picture);
}
foreach (PictureBox p in pictureBoxList)
{
p.MouseDown += new MouseEventHandler(c_MouseDown);
p.MouseMove += new MouseEventHandler(c_MouseMove);
p.MouseUp += new MouseEventHandler(c_MouseUp);
pnlDisplayImage.Controls.Add(p);
pnlDisplayImage.Refresh();
}
}
void c_MouseDown(object sender, MouseEventArgs e)
{
Control c = sender as Control;
isDragging = true;
move = e.Location;
}
void c_MouseMove(object sender, MouseEventArgs e)
{
if (isDragging == true) {
Control c = sender as Control;
for (int i = 0; i < pictureBoxList.Count(); i++)
{
if (c.Equals(pictureBoxList[i]))
{
pictureBoxList[i].Left += e.X - move.X;
pictureBoxList[i].Top += e.Y - move.Y;
}
}
}
}
void c_MouseUp(object sender, MouseEventArgs e)
{
isDragging = false;
}
}
Try something like (it's custom control with overrides, but should be easy to convert to events):
private bool _isMoved = false; // true if move mode on
private Point _pointMove = new Point(0); // for moving
protected override void OnMouseDown(MouseEventArgs e)
{
// if left button pressed
if(e.Button == MouseButtons.Left)
{
_pointMove.X = e.X;
_pointMove.Y = e.Y;
_isMoved = true;
Cursor = Cursors.SizeAll;
Capture = true;
}
base.OnMouseDown (e);
}
protected override void OnMouseUp(MouseEventArgs e)
{
// if move mode on
if(_isMoved)
{
_isMoved = false;
Cursor = Cursors.Default;
Capture = false;
}
base.OnMouseUp (e);
}
protected override void OnMouseMove(MouseEventArgs e)
{
// if move mode on
if (_isMoved)
{
Left += e.X - _pointMove.X;
Top += e.Y - _pointMove.Y;
}
base.OnMouseMove (e);
}

Categories

Resources