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);
}
}
Related
It seems like my script is not being executed. I created my own exit button, pressed on "view code" and added this line:
public void leaveButton_Click ( object sender, EventArgs e )
{
this.Close();
}
I tried the same with:
Application.Exit();
I thought that it might just not work on an image (which it actually should?) so I created a button with the same function... The same result; nothing.
I ignored it for the first part and looked for some different lines to add. My borders are set to none but I still wanted it to be movable. So I added this:
private bool mouseDown;
private Point lastLocation;
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
mouseDown = true;
lastLocation = e.Location;
}
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
if (mouseDown)
{
this.Location = new Point(
(this.Location.X - lastLocation.X) + e.X, (this.Location.Y - lastLocation.Y) + e.Y);
this.Update();
}
}
private void Form1_MouseUp(object sender, MouseEventArgs e)
{
mouseDown = false;
}
I do not truly understand it so there might be an error in it. It does nothing.
I am a beginner programmer and I feel like I am repeating code unnecessarily. I want to make a picture puzzle game consisting of 16 pictureboxes. The problem is that I feel like I have to repeat code for each picturebox's events as in the below example:
Point move;
bool isDragging = false;
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
isDragging = true;
move = e.Location;
}
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
if(isDragging == true)
{
pictureBox1.Left += e.X - move.X;
pictureBox1.Top += e.Y - move.Y;
pictureBox1.BringToFront();
}
}
private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
{
isDragging = false;
}
Just create one method for each of your 3 events:
private void pictureBox_MouseDown(object sender, MouseEventArgs e)
{
isDragging = true;
move = e.Location;
}
private void pictureBox_MouseMove(object sender, MouseEventArgs e)
{
if(isDragging == true)
{
// Luckily the sender parameter will tell us which PictureBox we are dealing with
PictureBox pb = (PictureBox)sender;
pb.Left += e.X - move.X;
pb.Top += e.Y - move.Y;
pb.BringToFront();
}
}
private void pictureBox_MouseUp(object sender, MouseEventArgs e)
{
isDragging = false;
}
Then go to each of your 16 picture boxes in the designer and set the MouseUp event handler to point to pictureBox_MouseUp and the MouseMove event handler to point to pictureBox_MouseMove and the MouseDown event handler to point to pictureBox_MouseMove. Do this for each of the 16 picture boxes.
Try to trigger same events for all PictureBox Controls
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
isDragging = true;
move = e.Location;
}
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
PictureBox pictureBox = sender as PictureBox;
if(isDragging == true)
{
pictureBox .Left += e.X - move.X;
pictureBox .Top += e.Y - move.Y;
pictureBox .BringToFront();
}
}
private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
{
isDragging = false;
}
Create a new component contained Image component and set dock parent container. Write your drag drop codes into the new component.
For example,
public partial class DraggablePictureBox : UserControl
{
public DraggablePictureBox()
{
InitializeComponent();
}
/// <summary>
/// Sets image of inner picture
/// </summary>
public Image Image
{
get {
return InnerPictureBox.Image;
}
set
{
InnerPictureBox.Image = value;
}
}
private void InnerPictureBox_MouseMove(object sender, MouseEventArgs e)
{
if (isDragging == true)
{
this.Left += e.X - move.X;
this.Top += e.Y - move.Y;
this.BringToFront();
}
}
private void InnerPictureBox_MouseUp(object sender, MouseEventArgs e)
{
isDragging = false;
}
private void InnerPictureBox_MouseDown(object sender, MouseEventArgs e)
{
isDragging = true;
move = e.Location;
}
private Point move;
private bool isDragging = false;
}
Now you have a one drag drop code for images.
One way to combat this would be to put these in methods, for example
MovePicturePox(Point move, Point newPos, PictureBox pb)
{
pb.Left += newPos.X - move.X;
pb.Top += newPos.Y - move.Y;
pb.BringToFront();
}
These methods can then be called like so
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
if(isDragging == true)
{
MovePictureBox(move, new Point(e.X, y.Y), pictureBox1);
}
}
I am trying to make my Form, which has no border, moveable with holding the left mousebutton down and exit the while loop, when releasing the mousebutton.
But the code I have right now doesn't exit the loop on release.
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
mouseDown = true;
while (mouseDown)
{
mouseX = MousePosition.X;
mouseY = MousePosition.Y - 30;
this.SetDesktopLocation(mouseX, mouseY);
if (e.Button != MouseButtons.Left)
mouseDown = false;
}
I also tried to add a mouseUp event but it cant happen as long as mouseDown is active.
private void Form1_MouseUp(object sender, MouseEventArgs e)
{
mouseDown = true;
}
OK, I fixed it for myself.
I just did this:
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
mouseDown = true;
}
private void Form1_MouseUp(object sender, MouseEventArgs e)
{
mouseDown = false;
}
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
if (mouseDown)
{
mouseX = MousePosition.X - 20;
mouseY = MousePosition.Y - 40;
this.SetDesktopLocation(mouseX, mouseY);
}
}
By using a loop in the on mouse up event you are locking the thread. You could use the MouseMove event with a public variable to check if the mouse is down.
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;
}
}
I am creating a "cropping tool", and i need to make a panel that contains 2 buttons draggable.
Until now i've tried something like this, but the change location event happens only when i click the right button of the mouse...
this.MouseDown += new MouseEventHandler(onRightClickMouse);
private void onRightClickMouse(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
Point localMouseClickPoint = new Point(e.X, e.Y);
panel1.Location = localMouseClickPoint;
}
}
My question: How can i make that panel draggable in my form?(I mean click on the panel then drag it to a location).
Try something like this:
delegate void updatePanelCallback();
panel1.MouseDown += new MouseEventHandler(onMouseDown);
panel1.MouseUp += new MouseEventHandler(onMouseUp);
System.Timers.Timer runTimer = new System.Timers.Timer(100);
runTimer.Elapsed += new ElapsedEventHandler(onTimerElapsed);
private void onMouseDown(object sender, MouseEventArgs e)
{
if (e.Button != MouseButtons.Right)
{
return;
}
runTimer.Enabled = false;
}
private void onMouseUp(object sender, MouseEventArgs e)
{
runTimer.Enabled = false;
}
public void updatePanelLocation()
{
if (this.InvokeRequired)
{
this.Invoke(new updatePanelCallback(updatePanelLocation), new object[] {});
}
else
{
Cursor curs = new Cursor(Cursor.Current.Handle);
panel1.Location = curs.Position;
}
}
private void onTimerElapsed(object source, ElapsedEventArgs e)
{
updatePanelLocation();
}
You could try something in two steps, preparing the action on MouseDown event and finishing it on MouseUp.