I have been trying to fix an error for the last week.
I got a panel and inside this panel there is a picturebox (a map).
Whenever you press the mouse button and you move it to somewhere it moves the map, but whenever you release it and press it again and move it, it goes back to the default position of the map (but still dragable).
I need it to be that whenever someone release the button press, and click and move it again it proceed from the position it is currently.
I am sure it has something to with my MouseMove event and I have tried a lot of things and couldn't manage to fix it.
Here are my codes for the MouseUp, MouseDown and MouseMove events.
private bool Dragging;
private Point lastLocation;
private void countryMapImage_MouseUp(object sender, MouseEventArgs e)
{
Dragging = false;
lastLocation = e.Location;
}
private void countryMapImage_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
Dragging = true;
lastLocation = e.Location;
}
}
private void countryMapImage_MouseMove(object sender, MouseEventArgs e)
{
if (Dragging == true)
{
int dx = e.X - lastLocation.X;
int dy = e.Y - lastLocation.Y;
countryMapImage.Padding = new Padding(Padding.Left + dx, Padding.Top + dy, Padding.Right - dx, Padding.Bottom - dy);
countryMapImage.Invalidate();
}
}
Would appreciate the help!
I have tried changing some values on the mousemove padding event, mousedown and mouseup events and nothing solved it. I have already tried to look for some answers but couldn't find any that solves the problem.
Try this: When user clicks the Map, capture the location of the cursor (in "screen" coordinates) and also the current location of the Map control. Now, as you move the mouse (and only if LButton is down) calculate a delta of the current cursor position (in "screen" coordinates) relative to the cursor position captured on the MouseDown event. Then, set the new position of the Map control to be the sum of its location that you captured on the MouseDown event plus the delta.
One key to having this work properly is to take the e.Location of the handler events and convert the point to screen coordinates relative to the sender.
public MainForm()
{
InitializeComponent();
countryMapImage.MouseMove += onMapMouseMove;
countryMapImage.MouseDown += onMapMouseDown;
}
Point
// Where's the cursor in relation to screen when mouse button is pressed?
_mouseDownScreen = new Point(),
// Where's the 'map' control when mouse button is pressed?
_controlDownPoint = new Point(),
// How much has the mouse moved from it's original mouse-down location?
_mouseDelta = new Point();
private void onMapMouseDown(object sender, MouseEventArgs e)
{
if (sender is Control control)
{
_mouseDownScreen = control.PointToScreen(e.Location);
Text = $"{_mouseDownScreen}";
_controlDownPoint = countryMapImage.Location;
}
}
private void onMapMouseMove(object sender, MouseEventArgs e)
{
if (MouseButtons.Equals(MouseButtons.Left))
{
if (sender is Control control)
{
var screen = control.PointToScreen(e.Location);
_mouseDelta = new Point(screen.X - _mouseDownScreen.X, screen.Y - _mouseDownScreen.Y);
Text = $"{_mouseDownScreen} {screen} {_mouseDelta}";
var newControlLocation = new Point(_controlDownPoint.X + _mouseDelta.X, _controlDownPoint.Y + _mouseDelta.Y);
if(!countryMapImage.Location.Equals(newControlLocation))
{
countryMapImage.Location = newControlLocation;
}
}
}
}
The MouseUp event shouldn't need to be handled in this simple scenario.
Related
When is try with code, there appear two label and when move, screen become white from where they move. I want single label move with mouse move.
bool mDown = false;
private void label13_MouseMove(object sender, MouseEventArgs e)
{
if (mDown)
{
label13.Location = e.Location;
}
}
private void label13_MouseDown(object sender, MouseEventArgs e)
{
mDown = true;
}
private void label13_MouseUp(object sender, MouseEventArgs e)
{
mDown = false;
}
The e.Location gives you a mouse position relative to the control that is being clicked. So to fix that, instead of
label13.Location = e.Location;
use
var pos = this.PointToClient(Cursor.Position);
label13.Location = new Point(pos.X - offset.X, pos.Y - offset.Y);`
Create the offset variable as a property of the form (type Point) and initialize it on the mouse down event:
offset = e.Location;
We can get X and Y points which by mouse move on the picturebox like;
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
double Xcoordinate = e.X;
double Ycoordinate = e.Y;
label1.Text = Xcoordinate.ToString();
label2.Text = Ycoordinate.ToString();
}
My question How can I get Xcoordinate and Ycoordinate from other events for ex; MouseClick event or my new defined function?
Actually I want to reach XCoordinate and Ycoordinate parameter from FormLoad. How can I do that?
Use Cursor Position property..
private void MoveCursor()
{
// Set the Current cursor, move the cursor's Position,
// and set its clipping rectangle to the form.
this.Cursor = new Cursor(Cursor.Current.Handle);
Cursor.Position = new Point(Cursor.Position.X - 50, Cursor.Position.Y - 50);
Cursor.Clip = new Rectangle(this.Location, this.Size);
}
The MouseMove event happens to give you the mouse position. This is not included in other EventArgs. You can always get the mouse positon through Cursor.Position.
The static method Control.MousePosition will get you the absolute position of the mouse pointer on the screen. You can translate it using Control.PointToClient to get the coordinates local to a control of interest.
If I remember correctly, the one caveat is that MouseEventArgs gives you the mouse position as it was when the message was posted to the event loop, while Control.MousePosition gives you the position right now. For most applications, this difference is probably not a big deal.
You can Use this Solution to Get co-ordinate of picture Box at the time of other event
protected override void OnMouseClick(MouseEventArgs e)
{
base.OnMouseClick(e);
textBox1.Text = e.X.ToString();
textBox2.Text = e.Y.ToString();
}
private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
{
textBox1.Text = e.X.ToString();
textBox2.Text = e.Y.ToString();
}
}
Or try It also
pictureBox1.MouseClick += (s, e) => MessageBox.Show(String.Format("Mouse Clicked at X: {0} Y: {1}", e.X, e.Y));
I have a panel, that can be moved left or right (chosen automatically according to its current position, distance is static) by click. Also, the user can drag the panel vertically however he wants by clicking the panel, holding the button and moving his mouse. The problem is, that the panel does the left/right move also, when it is dropped after being moved vertically, so the user has to click it again afterwards, to get to correct side (left/right). Here are the methods I am using:
Adding eventhandlers to panel(called Strip here)
Strip.MouseDown += new MouseEventHandler(button_MouseDown);
Strip.MouseMove += new MouseEventHandler(button_MouseMove);
Strip.MouseUp += new MouseEventHandler(button_MouseUp);
Strip.Click += new EventHandler(strip_Click);
And here all the methods mentioned above:
void button_MouseDown(object sender, MouseEventArgs e)
{
activeControl = sender as Control;
previousLocation = e.Location;
Cursor = Cursors.Hand;
}
void button_MouseMove(object sender, MouseEventArgs e)
{
if (activeControl == null || activeControl != sender)
return;
var location = activeControl.Location;
location.Offset(0, e.Location.Y - previousLocation.Y);
activeControl.Location = location;
}
void button_MouseUp(object sender, MouseEventArgs e)
{
activeControl = null;
Cursor = Cursors.Default;
}
void strip_Click(object sender, EventArgs e) // The one moving strip to left or right
{
activeControl = sender as Control;
if (activeControl.Left != 30)
activeControl.Left = 30;
else
activeControl.Left = 5;
}
How to make the panel not move left or right when it was moved vertically?
You'll need to distinguish between a click and a drag. So add a private field named "dragged".
private bool dragged;
In the MouseDown event handler add:
dragged = false;
In the MouseMove event handler add:
if (Math.Abs(location.Y - previousLocation.Y) >
SystemInformation.DoubleClickSize.Height) dragged = true;
In the Click event handler add:
if (dragged) return;
I am working on a project which requires a number of pixels between two points which are horizontal in an image. I am doing this in a windows application form.
Basically when a user clicks on one point in image and then on other point then he should get horizontal distance in form on number of pixels.
I am not getting any idea to do it.
please help.
For an image container, I am just using a pictureBox, though this would work with a label or whatever control you want.
Outside of any function:
private Boolean clicked_once = false;
private Point point1;
private Point point2;
And then I added a MouseClick event (not a Click event) :
private void pictureBox1_MouseClick(object sender, MouseEventArgs e)
{
if (clicked_once == false)
{
clicked_once = true;
point1 = e.Location;
}
else if (clicked_once == true)
{
clicked_once = false;
point2 = e.Location;
int distance = Math.Abs(point1.X - point2.X);
MessageBox.Show("Distance of pixels horizontally: " + distance.ToString());
}
}
Should work.
Assuming your image is displayed using any control descended from Control then you will have access to the Control.MouseClick event (link).
This event uses MouseEventArgs (link), which has a property X.
Should be pretty clear from that point.
EDIT: Added this very simple example:
private int? x1;
private void MyImageControl_MouseClick(object sender, MouseEventArgs e)
{
if (x1.HasValue)
{
MessageBox.Show("Difference of " + Math.Abs(e.X - x1.Value).ToString());
x1 = null;
}
else
{
x1 = e.X;
}
}
I have a fom which has one user control docked to fill.
this user control displays different images.each image has Id and I have a list of imageId vs imageDetail object dictionary.
Mouse Move event of this user control is captured and i am displaying current X and Y position of mouse in tool tip.
I also want to display image detail in tool tip when user keeps the mouse over image for some time.
I tried to do this with Mouse Hover event but it only raised when mouse enters in user control bound. after this if i move mouse within user control mouse hover event does not fire...
How can i display current X, Y position along image detail in tool tip.
is there any way to simulate Mouse Hover event within Mouse Move using some timer.
Is there any sample code..
I solved this problem by
public partial class Form1 : Form
{
Timer timer;
bool moveStart;
int count = 0;
Point prev;
public Form1()
{
InitializeComponent();
timer = new Timer();
timer.Interval = 1000;
timer.Tick += new EventHandler(timer_Tick);
}
void timer_Tick(object sender, EventArgs e)
{
this.timer.Stop();
this.moveStart = false;
this.toolTip1.SetToolTip(this, string.Format("Mouse Hover"));
this.textBox1.Text = (++count).ToString();
}
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
if (this.prev.X == e.X && this.prev.Y == e.Y)
return;
if (moveStart)
{
this.prev = new Point(e.X, e.Y);
this.timer.Stop();
this.toolTip1.SetToolTip(this, string.Format("Mouse Move\nX : {0}\nY : {1}", e.X, e.Y));
this.timer.Start();
}
else
{
moveStart = true;
}
}
}
The simplest way would be to call the MouseOver subroutine from the MouseMove subroutine as such:
void MouseMove(object sender, MouseEventArgs e)
{
//Call the MouseHover event
MouseHover(sender, e);
}
void MouseHover(object sender, EventArgs e)
{
//MouseHover event code
}
If you want more control over when and how to display your tooltip, however, you'll need to do something similar to the following:
Declare a listening variable at class level.
Hook to the MouseHover event so the listening variable is turned on when the mouse enters.
Hook to the MouseLeave event so the listening variable is turned off when the mouse leaves.
Put your tooltip code in the MouseMove handler so it displays your tooltip if the listening variable is on.
Here's some code to demonstrate what's I outlined above.
class Form1
{
bool showPopup = false;
void MouseHover(object sender, EventArgs e)
{
showPopup = true;
}
void MouseLeave(object sender, EventArgs e)
{
showPopup = false;
toolTip.Hide(this);
}
void MouseMove(object sender, MouseEventArgs e)
{
if (showPopup)
{
toolTip.Show("X: " + e.Location.X + "\r\nY: " + e.Location.Y,
this, e.Location)
}
}
}
Of course, you'll have to add a ToolTip with the name toolTip and associate the various methods (subroutines) with the appropriate events of your control (Form, PictureBox, etc).