Drag and drop C# not working properly - c#

I got myself drag and drop functionality in my little card game. I want just like in the windows card games to be able to drag and drop my cards. Now, I simply tried to make the drag and drop, but it's acting weird. The card is a custom control called Card.
I first will explain it:
1. I hold my mouse button on the card
2. It moves to another location (without me moving the mouse).
3. The card now moves correctly, but when I release the mouse button, the card will be at that position, but it won't be the position of my mouse since it was off when I clicked.
This is my code:
public void CardMouseDown(object sender, MouseEventArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Left)
{
cardClickInitLocation = ((Card)sender).Location;
}
}
public void CardMouseUp(object sender, MouseEventArgs e)
{
}
public void CardMouseMove(object sender, MouseEventArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Left)
{
((Card)sender).Left = e.X + ((Card)sender).Left - cardClickInitLocation.X;
((Card)sender).Top = e.Y + ((Card)sender).Top - cardClickInitLocation.Y;
}
}
I used my mouseup event before but it's not needed with this method. I cannot see what I could've done wrong.

Fixed it!
Added this to the class:
private Size mouseOffset;
private bool clicked = false;
The three events:
public void CardMouseDown(object sender, MouseEventArgs e)
{
mouseOffset = new System.Drawing.Size(e.Location);
clicked = true;
}
public void CardMouseUp(object sender, MouseEventArgs e)
{
clicked = false;
}
public void CardMouseMove(object sender, MouseEventArgs e)
{
if (clicked)
{
System.Drawing.Point newLocationOffset = e.Location - mouseOffset;
((Card)sender).Left += newLocationOffset.X;
((Card)sender).Top += newLocationOffset.Y;
((Card)sender).BringToFront();
}
}

Related

While dragging rectangle object fast, dragging stops

While dragging rectangle object fast, dragging stops and cursor only moves. Without releasing button click button, moving the cursor on the rectangle object starts the dragging again. It is dragging fine when i drag on a constant speed.
My code is,
private void ConnectorMethod()
{
_draggedLine = new Rectangle();
_draggedLine.Width = 100;
_draggedLine.Height = 12;
_controlModel.PlayerCanvas.Children.Add(_draggedLine);
_draggedLine.PreviewMouseLeftButtonDown += copy_Connector;
_draggedLine.PreviewMouseLeftButtonUp += connector_leftUp;
}
private void copy_Connector(object sender, MouseButtonEventArgs e)
{
_connecting_Connector = (Rectangle)sender;
_Connector_position = e.GetPosition(_connecting_Connector);
_connecting_Connector.PreviewMouseMove += ConnectorMouseMove;
}
private void connector_leftUp(object sender, MouseButtonEventArgs e)
{
_connecting_Connector.PreviewMouseMove -= ConnectorMouseMove;
}
private void ConnectorMouseMove(object sender, MouseEventArgs e)
{
if (e.LeftButton == MouseButtonState.Pressed)
{
Point canvasRelativePosition = e.GetPosition(_controlModel.PlayerCanvas);
Canvas.SetTop(_connecting_Connector, canvasRelativePosition.Y - _Connector_position.Y);
Canvas.SetLeft(_connecting_Connector, canvasRelativePosition.X - _Connector_position.X);
e.Handled = true;
}
}
Thanks in Advance.
I have fixed it with implementation canvas rectangle object in

Mouse-Events are not getting fired when Mouse Button is pressed [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 9 years ago.
Improve this question
when a mouse button is pressed on a control, it doesn't fire any events on it anymore.
I need that, since i wanted to enable myself to navigate through a control by "dragging" it.
The Drag events aren't getting fired either. Don't know why it is this way. It's useless.
I need an event thats getting fired when the mouse moves. Where is it?
Edit:
Look, when you hold the left mouse-button on Google-Map, you can move on the map with your mouse movement. I want to do the same thing with my UserControl. I overrided the OnPaint-Method so it would simply displays a grid. I have also implemented functions to move around with keys. It all works so far. Now i want to move around with the mouse by holding down the left mouse-button and move it. It should be easy and obvious, but it isn't.
So i subscribed all the mouse- and drag-events. Like that:
public partial class GameBoard : UserControl
{
private int m_CellWidth = 5;
private int m_CellHeight = 5;
private Point m_Position = Point.Empty;
private Point m_MousePoint = Point.Empty;
public GameBoard()
{
InitializeComponent();
ResizeRedraw = true;
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
Point firstPoint = new Point(m_Position.X % m_CellWidth, m_Position.Y % m_CellHeight);
int countVisibleCols = (int)Math.Ceiling((double)((double)Width / (double)m_CellWidth));
int countVisibleRows = (int)Math.Ceiling((double)((double)Height / (double)m_CellHeight));
Pen artistsPen = new Pen(Brushes.Black);
for (int i = 0; i < countVisibleCols; i++)
{
Point startPoint = new Point(firstPoint.X + i * m_CellWidth, 0);
Point endPoint = new Point(firstPoint.X + i * m_CellWidth, Height);
e.Graphics.DrawLine(artistsPen, startPoint, endPoint);
}
for (int i = 0; i < countVisibleRows; i++)
{
Point startPoint = new Point(0, firstPoint.Y + i * m_CellHeight);
Point endPoint = new Point(Width, firstPoint.Y + i * m_CellHeight);
e.Graphics.DrawLine(artistsPen, startPoint, endPoint);
}
}
private void GameBoard_MouseUp(object sender, MouseEventArgs e)
{
}
private void GameBoard_MouseMove(object sender, MouseEventArgs e)
{
}
private void GameBoard_MouseLeave(object sender, EventArgs e)
{
}
private void GameBoard_MouseHover(object sender, EventArgs e)
{
}
private void GameBoard_MouseEnter(object sender, EventArgs e)
{
}
private void GameBoard_MouseDown(object sender, MouseEventArgs e)
{
}
private void GameBoard_MouseDoubleClick(object sender, MouseEventArgs e)
{
}
private void GameBoard_MouseClick(object sender, MouseEventArgs e)
{
}
private void GameBoard_DragDrop(object sender, DragEventArgs e)
{
}
private void GameBoard_DragEnter(object sender, DragEventArgs e)
{
}
private void GameBoard_DragOver(object sender, DragEventArgs e)
{
}
private void GameBoard_DragLeave(object sender, EventArgs e)
{
}
}
(Actual subscriptions happens in the designer.)
The Problem is: If the left mouse-button has beeing clicked & hold on my control, NOT ONE of those events are getting fired anymore. So i don't know how to implement the desired feature.
Use the MouseMove() event. You can determine if the Left button is down during a move using the e.Button parameter. Here's an example with a Button:
private void button1_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Left)
{
this.Text = "Left: " + e.X.ToString() + ", " + e.Y.ToString();
}
else if (e.Button == System.Windows.Forms.MouseButtons.None)
{
this.Text = e.X.ToString() + ", " + e.Y.ToString();
}
}
Not all controls behave the same, however. Give more details about exactly which control you're trying to use and how.
In the Visual Studio designer for a form, there's an Events button in the Properties window of a component that typically looks like a lightning bolt. You can use this to tie events to functions. Could this be missing?

How can I capture mouse events that occur outside of a (WPF) window?

I have a Window element that has WindowStyle="None" and AllowsTransparency="True", therefore it has no title bar and supports transparency.
I want the user to be able to move the Window to any position on the screen by left-clicking anywhere within the Window and dragging. The Window should drag along with the mouse as long as the left mouse button is pressed down.
I was able to get this functionality to work with one exception: when the mouse moves outside of the Window (such as when the left mouse button was pressed down near the edge of the Window and the mouse is moved quicly), the Window no longer captures the mouse position and does not drag along with the mouse.
Here is the code from the code-behind that I use to get the job done:
public Point MouseDownPosition { get; set; }
public Point MousePosition { get; set; }
public bool MouseIsDown { get; set; }
private void window_MyWindowName_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
MouseDownPosition = e.GetPosition(null);
MouseIsDown = true;
}
private void window_MyWindowName_MouseMove(object sender, MouseEventArgs e)
{
if (MouseIsDown)
{
MousePosition = e.GetPosition(null);
Left += MousePosition.X - MouseDownPosition.X;
Top += MousePosition.Y - MouseDownPosition.Y;
}
}
private void window_MyWindowName_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
MouseIsDown = false;
}
I think you are looking for this: Processing Global Mouse and Keyboard Hooks in C#
Url: Processing Global Mouse and Keyboard Hooks in C#
This class allows you to tap keyboard and mouse and/or to detect their activity even when an application runs in the background or does not have any user interface at all.
This class raises common .NET events with KeyEventArgs and MouseEventArgs, so you can easily retrieve any information you need.
There is an example and explain and demo to use.
Great tutorial!
Example:
UserActivityHook actHook;
void MainFormLoad(object sender, System.EventArgs e)
{
actHook= new UserActivityHook(); // crate an instance
// hang on events
actHook.OnMouseActivity+=new MouseEventHandler(MouseMoved);
actHook.KeyDown+=new KeyEventHandler(MyKeyDown);
actHook.KeyPress+=new KeyPressEventHandler(MyKeyPress);
actHook.KeyUp+=new KeyEventHandler(MyKeyUp);
}
Now, an example of how to process an event:
public void MouseMoved(object sender, MouseEventArgs e)
{
labelMousePosition.Text=String.Format("x={0} y={1}", e.X, e.Y);
if (e.Clicks>0) LogWrite("MouseButton - " + e.Button.ToString());
}
I believe you are reinventing the wheel. Search for "Window.DragMove".
Example:
private void title_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
this.DragMove();
}
Try it this way:
// method to convert from 'old' WinForms Point to 'new' WPF Point structure:
public System.Windows.Point ConvertToPoint(System.Drawing.Point p)
{
return new System.Windows.Point(p.X,p.Y);
}
// some locals you will need:
bool mid = false; // Mouse Is Down
int x=0, y=0;
// Mouse down event
private void MainForm_MouseDown(object sender, MouseButtonEventArgs e)
{
mid=true;
Point p = e.GetPosition(this);
x = (int)p.X;
y = (int)p.Y;
}
// Mouse move event
private void MainForm_MouseMove(object sender, MouseButtonEventArgs e)
{
if(mid)
{
int x1 = e.GetPosition(this).X;
int y1 = e.GetPosition(this).Y;
Left = x1-x;
Top = y1-y;
}
}
// Mouse up event
private void MainForm_MouseUp(object sender, MouseButtonEventArgs e)
{
mid = false;
}

Moving a control within another control's visible area

I have a PictureBox that is inside a TabPage, and of course this TabPage is part of a TabView and this TabView is inside a Form. I want users be able to move this picture box within the tab page. For this I am using the MouseDown, MouseMove and MouseUp events of the picture box:
private void pictureBoxPackageView_MouseDown(object sender, MouseEventArgs e)
{
if (!_mapPackageIsMoving)
{
_mapPackageIsMoving = true;
}
}
private void pictureBoxPackageView_MouseMove(object sender, MouseEventArgs e)
{
if(_mapPackageIsMoving)
{
pictureBoxPackageView.Location = MousePosition; //This is not exact at all!
return;
}
//Some other code for some other stuff when picturebox is not moving...
}
private void pictureBoxPackageView_MouseUp(object sender, MouseEventArgs e)
{
if (_mapPackageIsMoving)
{
_mapPackageIsMoving = false; //Mouse button is up, end moving!
return;
}
}
But my problem lies in the MouseMove event. As soon as I move mouse after button down, the picture box jumps out of tab page's visible area.
I need to know how to handle the move only within the rectangle of the tab page, and if picture box is being dragged out of tab view's visible area, it shouldn't move anymore unless user brings the mouse inside the tab view's visible rectangle.
Any helps/tips will be appriciated!
You need a variable to hold the original position of the PictureBox:
Modified from a HansPassant answer:
private Point start = Point.Empty;
void pictureBoxPackageView_MouseUp(object sender, MouseEventArgs e) {
_mapPackageIsMoving = false;
}
void pictureBoxPackageView_MouseMove(object sender, MouseEventArgs e) {
if (_mapPackageIsMoving) {
pictureBoxPackageView.Location = new Point(
pictureBoxPackageView.Left + (e.X - start.X),
pictureBoxPackageView.Top + (e.Y - start.Y));
}
}
void pictureBoxPackageView_MouseDown(object sender, MouseEventArgs e) {
start = e.Location;
_mapPackageIsMoving = true;
}

C# - Moving a control to the mouse's position

I am trying to get a control to follow the cursor when the user clicks and drag the control. The problem is that 1.) the control doesn't go to the mouse's position, and 2.) the control flickers and flies all over the place. I've tried a few different methods of doing this, but all so far have failed.
I've tried:
protected override void OnMouseDown(MouseEventArgs e)
{
while (e.Button == System.Windows.Forms.MouseButtons.Left)
{
this.Location = e.Location;
}
}
and
protected override void OnMouseMove(MouseEventArgs e)
{
while (e.Button == System.Windows.Forms.MouseButtons.Left)
{
this.Location = e.Location;
}
}
but neither of these work. Any help is appreciated, and thanks in advance!
Here's how to do it:
private Point _Offset = Point.Empty;
protected override void MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
_Offset = new Point(e.X, e.Y);
}
}
protected override void MouseMove(object sender, MouseEventArgs e)
{
if (_Offset != Point.Empty)
{
Point newlocation = this.Location;
newlocation.X += e.X - _Offset.X;
newlocation.Y += e.Y - _Offset.Y;
this.Location = newlocation;
}
}
protected override void MouseUp(object sender, MouseEventArgs e)
{
_Offset = Point.Empty;
}
_Offset is used here for two purposes: keeping track of where the mouse was on the control when you initially clicked it, and also keeping track of whether the mouse button is down or not (so that the control doesn't get dragged when the mouse cursor goes over it and the button isn't down).
You definitely don't want to switch the ifs in this code to whiles, as it will make a difference.
there are mistakes in the answer 1
1.Set mouse handlers to control ,not to form
like button1_MouseMove
2.do not use this vector,but your control instead(Point newlocation = button1.Location;)
3.you do not need to override handlers.
in my test after these changes button(or other control) moves fine.
Chigook
Try this to move the object according to the mouse's position and the code below given is to gather the mouse's move path and the location saved in the arraylist to obtain the path where mouse point is moving. You have to declare the arraylist globally.
private void panel1_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
ArrayList inList = new ArrayList();
inList.Add(e.X);
inList.Add(e.Y);
list.Add(inList);
}
}
When the user clicks the button the control has to move in the path that the user dragged in the screen
private void button1_Click_2(object sender, EventArgs e)
{
foreach (ArrayList li in list)
{
pic_trans.Visible = true;
pic_trans.Location = new Point(Convert.ToInt32(li[0]), Convert.ToInt32(li[1]));
pic_trans.Show();
}
}
private Point ptMouseDown=new Point();
protected override void MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
ptMouseDown = new Point(e.X, e.Y);
}
}
protected override void MouseMove(object sender, MouseEventArgs e)
{
if (_Offset != Point.Empty)
{
Pointf[] ptArr=new Pointf[]{this.Location};
Point Diff=new Point(e.X-ptMouseDown.X,e.Y-ptMouseDown.Y);
Matrix mat=new Matrix();
mat.Translate(Diff.X,Diff.Y);
mat.TransFromPoints(ptArr);
this.Location=ptArr[0];
}
}
protected override void MouseUp(object sender, MouseEventArgs e)
{
_Offset = Point.Empty;
}

Categories

Resources