Moving a control by dragging it with the mouse in C# - c#

I'm trying to move the control named pictureBox1 by dragging it around. The problem is, when it moves, it keeps moving from a location to another location around the mouse, but it does follow it...
This is my code. and I would really appreciate it if you could help me
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
bool selected = false;
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
selected = true;
}
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
if (selected == true)
{
pictureBox1.Location = e.Location;
}
}
private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
{
selected = false;
}
}

All you need:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private Point MouseDownLocation;
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Left)
{
MouseDownLocation = e.Location;
}
}
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Left)
{
pictureBox1.Left = e.X + pictureBox1.Left - MouseDownLocation.X;
pictureBox1.Top = e.Y + pictureBox1.Top - MouseDownLocation.Y;
}
}
}

You can also use the extension:
public static class CmponentsExtensions
{
//Management of mouse drag and drop
#region Menu and Mouse
private static bool mouseDown;
private static Point lastLocation;
/// <summary>
/// To enable control to be moved around with mouse
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="control"></param>
public static void moveItselfWithMouse<T>(this T control) where T: Control
{
control.MouseDown += (o, e)=> { mouseDown = true; lastLocation = e.Location; };
control.MouseMove += (o, e) =>
{
if (mouseDown)
{
control.Location = new Point((control.Location.X - lastLocation.X) + e.X, (control.Location.Y - lastLocation.Y) + e.Y);
control.Update();
}
};
control.MouseUp += (o, e) => { mouseDown = false; } ;
}
public static void moveOtherWithMouse<T>(this T control, Control movedObject) where T : Control
{
control.MouseDown += (o, e) => { mouseDown = true; lastLocation = e.Location; };
control.MouseMove += (o, e) =>
{
if (mouseDown)
{
movedObject.Location = new Point((movedObject.Location.X - lastLocation.X) + e.X, (movedObject.Location.Y - lastLocation.Y) + e.Y);
movedObject.Update();
}
};
control.MouseUp += (o, e) => { mouseDown = false; };
}
#endregion
}
Then you need to use it with some control:
In this case pictureBox1 moved the whole Form
pictureBox1.moveOtherWithMouse(this);
In this case you move only pictureBox:
pictureBox1.moveItselfWithMouse();

try this to move pictureBox control at runtime using mouse
private void pictureBox7_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
xPos = e.X;
yPos = e.Y;
}
}
private void pictureBox7_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
{
PictureBox p = sender as PictureBox;
if (p != null)
{
if (e.Button == MouseButtons.Left)
{
p.Top += (e.Y - yPos);
p.Left += (e.X - xPos);
}
}
}

Related

Reorder customcontrol inside multiple flowLayoutPanel

I am trying to make an app to generate a tierlist.
Each tier consists of a flowlayout and you basically move the controls around by drag-and-drop.
The problem is that even though I can move the elements with drag-and-drop, the element is always added to the end of the list, and not where you drop the pointer.
Is there any way to maintain order during drag-and-drop?
These are the methods that are in the flowlayout
private void flow_DragEnter(object sender, DragEventArgs e)
{
e.Effect = DragDropEffects.Move;
}
private void flow_DragDrop(object sender, DragEventArgs e)
{
((UserControl1)e.Data.GetData(typeof(UserControl1))).Parent = (Panel)sender;
}
While these are from the usercontrol:
private void UserControl1_MouseDown(object sender, MouseEventArgs e)
{
this.DoDragDrop(this, DragDropEffects.Move);
}
You can opt this solution to reorder the dropped controls into a FlowLayoutPanel control. The part that utilizes the Control.ControlCollection.GetChildIndex and Control.ControlCollection.SetChildIndex methods.
Let's say you have a custom Control or UserControl named DragDropControl:
public class DragDropControl : Control
{
public DragDropControl()
{
AllowDrop = true;
BackColor = Color.LightSteelBlue;
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
TextRenderer.DrawText(e.Graphics, Text, Font,
ClientRectangle, Color.Black,
TextFormatFlags.HorizontalCenter |
TextFormatFlags.VerticalCenter);
}
}
Note: From what I see in the images, just use a simple Label control instead.
Let's create a custom FlowLayoutPanel and encapsulate all the required functionalities to not repeat that in your implementation for each FLP.
public class DragDropFlowLayoutPanel : FlowLayoutPanel
{
public DragDropFlowLayoutPanel()
{
AllowDrop = true;
}
[DefaultValue(true)]
public override bool AllowDrop
{
get => base.AllowDrop;
set => base.AllowDrop = value;
}
The custom FLP implements the mouse, drag and drop events of its children as well.
protected override void OnControlAdded(ControlEventArgs e)
{
base.OnControlAdded(e);
if (e.Control is DragDropControl)
{
e.Control.DragOver += OnControlDragOver;
e.Control.DragDrop += OnControlDragDrop;
e.Control.MouseDown += OnControlMouseDown;
}
}
protected override void OnControlRemoved(ControlEventArgs e)
{
base.OnControlRemoved(e);
e.Control.DragOver -= OnControlDragOver;
e.Control.DragDrop -= OnControlDragDrop;
e.Control.MouseDown -= OnControlMouseDown;
}
Handle the child controls MouseDown event to do the drag:
private void OnControlMouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
var control = sender as DragDropControl;
DoDragDrop(control, DragDropEffects.Move);
}
}
Handle the DragEnter and DragOver methods and events of the FLP and its children to set the drop effect.
protected override void OnDragEnter(DragEventArgs e)
{
base.OnDragEnter(e);
if (e.Data.GetDataPresent(typeof(DragDropControl)))
e.Effect = DragDropEffects.Move;
}
protected override void OnDragOver(DragEventArgs e)
{
base.OnDragOver(e);
if (e.Data.GetDataPresent(typeof(DragDropControl)))
e.Effect = DragDropEffects.Move;
}
private void OnControlDragOver(object sender, DragEventArgs e)
{
if (e.Data.GetData(typeof(DragDropControl)) is DragDropControl ddc)
{
var p = PointToClient(new Point(e.X, e.Y));
if (GetChildAtPoint(p) == ddc)
e.Effect = DragDropEffects.None;
else
e.Effect = DragDropEffects.Move;
}
}
Finally, call from the DragDrop method override and event the DropControl method and pass the DragEventArgs param.
protected override void OnDragDrop(DragEventArgs e)
{
base.OnDragDrop(e);
DropControl(e);
}
private void OnControlDragDrop(object sender, DragEventArgs e)
{
DropControl(e);
}
The dropped control takes the index of the control under the mouse position if any otherwise it will be inserted at the end of the Controls collection.
private void DropControl(DragEventArgs e)
{
if (e.Data.GetData(typeof(DragDropControl)) is DragDropControl ddc)
{
var p = PointToClient(new Point(e.X, e.Y));
var child = GetChildAtPoint(p);
var index = child == null
? Controls.Count
: Controls.GetChildIndex(child);
ddc.Parent = this;
Controls.SetChildIndex(ddc, index);
}
}
}
Put it all together.
public class DragDropFlowLayoutPanel : FlowLayoutPanel
{
public DragDropFlowLayoutPanel()
{
AllowDrop = true;
}
[DefaultValue(true)]
public override bool AllowDrop
{
get => base.AllowDrop;
set => base.AllowDrop = value;
}
protected override void OnControlAdded(ControlEventArgs e)
{
base.OnControlAdded(e);
if (e.Control is DragDropControl)
{
e.Control.DragOver += OnControlDragOver;
e.Control.DragDrop += OnControlDragDrop;
e.Control.MouseDown += OnControlMouseDown;
}
}
protected override void OnControlRemoved(ControlEventArgs e)
{
base.OnControlRemoved(e);
e.Control.DragOver -= OnControlDragOver;
e.Control.DragDrop -= OnControlDragDrop;
e.Control.MouseDown -= OnControlMouseDown;
}
protected override void OnDragEnter(DragEventArgs e)
{
base.OnDragEnter(e);
if (e.Data.GetDataPresent(typeof(DragDropControl)))
e.Effect = DragDropEffects.Move;
}
protected override void OnDragOver(DragEventArgs e)
{
base.OnDragOver(e);
if (e.Data.GetDataPresent(typeof(DragDropControl)))
e.Effect = DragDropEffects.Move;
}
protected override void OnDragDrop(DragEventArgs e)
{
base.OnDragDrop(e);
DropControl(e);
}
private void OnControlDragOver(object sender, DragEventArgs e)
{
if (e.Data.GetData(typeof(DragDropControl)) is DragDropControl ddc)
{
var p = PointToClient(new Point(e.X, e.Y));
if (GetChildAtPoint(p) == ddc)
e.Effect = DragDropEffects.None;
else
e.Effect = DragDropEffects.Move;
}
}
private void OnControlDragDrop(object sender, DragEventArgs e)
{
DropControl(e);
}
private void OnControlMouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
var control = sender as DragDropControl;
DoDragDrop(control, DragDropEffects.Move);
}
}
private void DropControl(DragEventArgs e)
{
if (e.Data.GetData(typeof(DragDropControl)) is DragDropControl ddc)
{
var p = PointToClient(new Point(e.X, e.Y));
var child = GetChildAtPoint(p);
var index = child == null
? Controls.Count
: Controls.GetChildIndex(child);
ddc.Parent = this;
Controls.SetChildIndex(ddc, index);
}
}
}

Custom ToggleButton doesn't respond to ManipulationCompleted event

I have a custom UI element that inherits from System.Windows.Controls.Primitives.ToggleButton. I'm also routing my mouse events through a custom TouchDevice that raises touch events instead.
For some reason, the ManipulationCompleted event never fires. First, the custom TouchDevice can be found here: http://blakenui.codeplex.com/SourceControl/changeset/view/67526#Blake.NUI.WPF/Touch/MouseTouchDevice.cs
Here are the relevant parts of my class:
public class ToggleSwitch: ToggleButton {
private Grid _root;
private readonly IList<int> _activeTouchDevices;
private const double UNCHECKED_TRANSLATION = 0;
private TranslateTransform _backgroundTranslation;
private TranslateTransform _thumbTranslation;
private Grid _root;
private Grid _track;
private FrameworkElement _thumb;
private double _checkedTranslation;
private double _dragTranslation;
private bool _wasDragged;
private bool _isDragging;
public override void OnApplyTemplate()
{
...
MouseTouchDevice.RegisterEvents(_root);
_root.IsManipulationEnabled = true;
_root.TouchDown += OnTouchDown;
_root.TouchUp += OnTouchUp;
_root.GotTouchCapture += OnGotTouchCapture;
_root.LostTouchCapture += OnLostTouchCapture;
_root.ManipulationStarted += OnManipulationStarted;
_root.ManipulationDelta += OnManipulationDelta;
_root.ManipulationCompleted += OnManipulationCompleted;
}
private void OnTouchDown(object sender, TouchEventArgs e)
{
e.TouchDevice.Capture(_root);
}
private void OnGotTouchCapture(object sender, TouchEventArgs e)
{
if (e.TouchDevice.Captured == _root)
{
Manipulation.AddManipulator(_root,e.TouchDevice);
_activeTouchDevices.Add(e.TouchDevice.Id);
}
}
private void OnManipulationStarted(object sender, ManipulationStartedEventArgs e)
{
e.Handled = true;
_isDragging = true;
_dragTranslation = Translation;
ChangeVisualState(true);
Translation = _dragTranslation;
}
private void OnManipulationDelta(object sender, ManipulationDeltaEventArgs e)
{
e.Handled = true;
var horizontalChange = e.DeltaManipulation.Translation.X;
var direction = Math.Abs(horizontalChange) >= Math.Abs(e.DeltaManipulation.Translation.Y) ? Orientation.Horizontal : Orientation.Vertical;
if (direction == Orientation.Horizontal && horizontalChange != 0.0)
{
_wasDragged = true;
_dragTranslation += horizontalChange;
Translation = Math.Max(UNCHECKED_TRANSLATION, Math.Min(_checkedTranslation, _dragTranslation));
}
}
private void OnManipulationCompleted(object sender, ManipulationCompletedEventArgs e)
{
e.Handled = true;
_isDragging = false;
var click = false;
if (_wasDragged)
{
var edge = (IsChecked ?? false) ? _checkedTranslation : UNCHECKED_TRANSLATION;
if (Translation != edge)
{
click = true;
}
}
else
{
click = true;
}
if (click)
{
OnClick();
}
_wasDragged = false;
}
}
The OnManipulationCompleted method is never entered.

Catch a mouseUP event during a while

here is my code:
public partial class Form1 : Form
{
Graphics g;
bool mouseUP = false;
double dimensions = 4.0;
SolidBrush brush;
public Form1()
{
InitializeComponent();
g = this.CreateGraphics();
brush = new SolidBrush(Color.Black);
}
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
mouseUP = false;
backgroundWorker1.RunWorkerAsync(e);
}
private bool mouseIsUP()
{
return mouseUP;
}
private void Form1_MouseUp(object sender, MouseEventArgs e)
{
mouseUP = true;
MessageBox.Show("UP");
}
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
backgroundWorker1.ReportProgress(0,e.Argument);
}
private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
while (!mouseIsUP())
{
g.FillEllipse(brush, ((MouseEventArgs)e.UserState).X - (int)dimensions / 2, ((MouseEventArgs)e.UserState).Y - (int)dimensions / 2, (int)dimensions, (int)dimensions);
dimensions += 0.2;
Thread.Sleep(10);
}
}
}
Why the event mouseUP is never occurring???
If i remove the while I can see the MessageBox "UP"...
I'm trying to make bigger a ellipse while the mouse is pressed. When I release the mouse button the ellipse should not grow more.
Thanks in advance!
Because you are not binding your event handlers to the events (assuming you didn't just miss out that code).
For example, within Form1's constructor, you could add
MouseUp += Form1_MouseUp;
MouseDown += Form1_MouseDown;
It looks like the background worker is somehow blocking the event loop in your code. Where is DoWork being called on the worker?
I think a better solution might be to override the OnPaint method, check if the mouse is down there, then draw your circle.
bool isMouseDown = false;
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
isMouseDown = true;
}
private void Form1_MouseUp(object sender, MouseEventArgs e)
{
isMouseDown = false;
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
if (isMouseDown)
{
g.FillEllipse(brush, ((MouseEventArgs)e.UserState).X - (int)dimensions / 2, ((MouseEventArgs)e.UserState).Y - (int)dimensions / 2, (int)dimensions, (int)dimensions);
dimensions += 0.2;
Thread.Sleep(10);
}
}

Drawing a rectangle on a picture box

When this form opens, it has a background image of the fullscreen and the user can mouse down and draw a rectangle which will be used later to crop the background image. The problem I am getting is that when the form paints, it's not drawing a rectangle so the user doesnt know the the area they are selecting
This is my code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Quick_Screenshot
{
public partial class Crop : Form
{
private Rectangle croprect = Rectangle.Empty;
private bool mouseDown = false;
private bool selectedArea = false;
Point sp, ep;
Bitmap background;
public Rectangle CropArea
{
get { return croprect; }
}
public bool SelectedArea
{
get { return selectedArea; }
}
public Crop(Bitmap image)
{
InitializeComponent();
background = image;
picBox.MouseDown += new MouseEventHandler(Crop_MouseDown);
picBox.MouseUp += new MouseEventHandler(Crop_MouseUp);
picBox.MouseMove += new MouseEventHandler(Crop_MouseMove);
picBox.Paint += new PaintEventHandler(Crop_Paint);
}
private void Crop_Load(object sender, EventArgs e)
{
picBox.Image = (Image)background;
}
private void Crop_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Right)
this.Close();
else
{
mouseDown = true;
sp = ep = e.Location;
}
}
private void Crop_MouseUp(object sender, MouseEventArgs e)
{
ep = e.Location;
mouseDown = false;
croprect = GetRectangle(sp, ep);
if (croprect.Width > 10 && croprect.Height > 10)
{
selectedArea = true;
}
else
{
croprect = Rectangle.Empty;
Taskbar.Balloon("Selected area too small", "Quick Screenshot", ToolTipIcon.Error);
}
this.Close();
}
private void Crop_MouseMove(object sender, MouseEventArgs e)
{
if (mouseDown)
{
ep = e.Location;
Update();
}
}
private void Crop_Paint(object sender, PaintEventArgs e)
{
e.Graphics.DrawRectangle(Pens.Red, GetRectangle(sp, ep));
e.Graphics.Save();
}
private Rectangle GetRectangle(Point p1, Point p2)
{
return new Rectangle(
Math.Min(p1.X, p2.X),
Math.Min(p1.Y, p2.Y),
Math.Abs(p1.X - p2.X),
Math.Abs(p1.Y - p2.Y)
);
}
}
}
You should call the Invalidate() method on the picBox variable to refresh the control.
Instead of this:
private void Crop_MouseMove(object sender, MouseEventArgs e)
{
if (mouseDown)
{
ep = e.Location;
Update();
}
}
Use this:
private void Crop_MouseMove(object sender, MouseEventArgs e)
{
if (mouseDown)
{
ep = e.Location;
picBox.Invalidate();
}
}
You should call Invalidate() instead of Update()

How to select ListViewItem with left mouse button only?

How can i prevent the right click from selecting an item in my listview both in click and double click?
You could use this code, I think it should do the work. You need to set some bool variable to indicate that the right mouse has been clicked in your MouseDown, then Clear selected items, if SelectedIndexChanged event handler fired because of the right click and then reset the indicator on MouseUp event. Check the code:
bool rightClicked = false;
private void listView1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Right)
{
rightClicked = true;
}
else
{
rightClicked = false;
}
}
private void listView1_SelectedIndexChanged(object sender, EventArgs e)
{
if (rightClicked)
{
listView1.SelectedItems.Clear();
}
}
private void listView1_MouseUp(object sender, MouseEventArgs e)
{
rightClicked = false;
}
EDIT: This is the best I could do, it preserves the selection but flickers. the solution could be implemented using some custom drawing of the items but that requires too much time. I leave that to you.
bool rightClicked = false;
int [] lviListIndex = null;
private void listView1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Right)
{
rightClicked = true;
lviListIndex = new int[listView1.SelectedItems.Count];
listView1.SelectedIndices.CopyTo(lviListIndex, 0);
}
else
{
rightClicked = false;
}
}
private void listView1_SelectedIndexChanged(object sender, EventArgs e)
{
if (rightClicked)
{
listView1.SelectedIndices.Clear();
}
}
private void listView1_MouseUp(object sender, MouseEventArgs e)
{
if (rightClicked)
{
listView1.SelectedIndexChanged -= new System.EventHandler(listView1_SelectedIndexChanged);
if (lviListIndex != null)
{
foreach (int index in lviListIndex)
{
listView1.SelectedIndices.Add(index);
}
}
lviListIndex = null;
listView1.SelectedIndexChanged += new System.EventHandler(listView1_SelectedIndexChanged);
}
rightClicked = false;
}
Subclass the listview and add this override:
protected override void WndProc(ref Message m)
{
const int WM_RBUTTONUP = 0x0205;
const int WM_RBUTTONDOWN = 0x0204;
if ((m.Msg != WM_RBUTTONDOWN) && (m.Msg != WM_RBUTTONUP))
{
base.WndProc(ref m);
}
}
This works by ignoring messages from the right button of the mouse.
you can check like this if (e.Button == MouseButtons.Left)

Categories

Resources