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?
Related
I am doing a simple drag and drop operation in windows forms. Whenever I start the operation the cursor changes shape. I know this is directly related to the DragDropEffects and I can't find an option that results in the default cursor. Can anybody help? Here is the code:
a.MouseDown += new MouseEventHandler(ButtonDown);
a.DragEnter += new DragEventHandler(ButtonDragEnter);
a.AllowDrop = true;
and here are the functions:
private void ButtonDown(object sender, EventArgs e)
{
PictureBox p = (PictureBox)sender;
ButtonClick(sender, e);
p.DoDragDrop(p.BackColor, DragDropEffects.All);
}
private void ButtonDragEnter(object sender, DragEventArgs e)
{
e.Data.GetFormats();
e.Effect = DragDropEffects.None;
Color c = new Color();
c = (Color) e.Data.GetData(c.GetType());
ButtonClick(sender, e,c);
}
Ok answered my own question here:
You first need to add an event to giveFeedBack:
a.GiveFeedback += new GiveFeedbackEventHandler(DragSource_GiveFeedback);
and here is the feedback function:
private void DragSource_GiveFeedback(object sender, GiveFeedbackEventArgs e)
{
e.UseDefaultCursors = false;
}
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();
}
}
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I am working in C#.net based windows application...In the application i am creating controls dynamically and added mouse events to that one..But while moving child control it's moving out of parent control...I tried below Code..but it's not working...So how can we do it?
private Point start = Point.Empty;
private bool _mapPackageIsMoving;
Control SelectedControl = null;
private void button3_Click_1(object sender, EventArgs e)
{
Label lbl = new Label();
lbl.Name = "id1";
lbl.Text = "move";
lbl.MouseDown += new MouseEventHandler(label1_MouseDown);
lbl.MouseMove += new MouseEventHandler(label1_MouseMove);
lbl.MouseUp += new MouseEventHandler(label1_MouseUp);
this.panel2.Controls.Add(lbl);
SelectedControl = lbl;
}
void label1_MouseUp(object sender, MouseEventArgs e)
{
_mapPackageIsMoving = false;
}
void label1_MouseMove(object sender, MouseEventArgs e)
{
Label lControl = sender as Label;
if (lControl != null)
{
if (_mapPackageIsMoving)
{
int nx = Math.Min(Math.Max(lControl.Left + (e.X - start.X), 0), lControl.Parent.Width - SelectedControl.Width);
int ny = Math.Min(Math.Max(lControl.Top + (e.Y - start.Y), 0), lControl.Parent.Height - SelectedControl.Height);
lControl.Location = new Point(nx, ny);
}
}
}
void label1_MouseDown(object sender, MouseEventArgs e)
{
start = e.Location;
_mapPackageIsMoving = true;
}
You need to clip the cursor, i.e. prevent it moving outside the panel that parents the label. The changes below should help.
void label1_MouseDown(object sender, MouseEventArgs e)
{
start = e.Location;
_mapPackageIsMoving = true;
Cursor.Clip = panel2.RectangleToScreen(panel2.ClientRectangle);
}
void label1_MouseUp(object sender, MouseEventArgs e)
{
_mapPackageIsMoving = false;
Cursor.Clip = null;
}
Drag and Drop on a Windows Form on CodeProject is a handy reference (the code is in VB.NET).
Having trouble with something here which I'm hoping is actually simple.
I have a custom UserControl in WPF which allows me to display an image. When the program is running a user can add this UserControl as many times as they like to a canvas. In effect a simple image viewer where they can add and move images about.
I would like to be able to right click these images open a contextMenu and then choose send backward of bring forward and the images would then change z order depending which menu choice was clicked.
I have the user control set up with the contextMenu so I just need to know the code for changing the z order of this userControl...
Any help is much appreciated :)
namespace StoryboardTool
{
/// <summary>
/// Interaction logic for CustomImage.xaml
/// </summary>
public partial class CustomImage : UserControl
{
private Point mouseClick;
private double canvasLeft;
private double canvasTop;
public CustomImage()
{
InitializeComponent();
cusImageControl.SetValue(Canvas.LeftProperty, 0.0);
cusImageControl.SetValue(Canvas.TopProperty, 0.0);
}
public void chooseImage()
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Title = "Choose Image to Add";
if (ofd.ShowDialog() == true)
{
BitmapImage bImage = new BitmapImage();
bImage.BeginInit();
bImage.UriSource = new Uri(ofd.FileName);
bImage.EndInit();
image.Width = bImage.Width;
image.Height = bImage.Height;
image.Source = bImage;
image.Stretch = Stretch.Fill;
}
}
private void cusImageControl_LostMouseCapture(object sender, MouseEventArgs e)
{
((CustomImage)sender).ReleaseMouseCapture();
}
private void cusImageControl_MouseUp(object sender, MouseButtonEventArgs e)
{
((CustomImage)sender).ReleaseMouseCapture();
cusImageControl.Cursor = Cursors.Arrow;
}
private void cusImageControl_MouseMove(object sender, MouseEventArgs e)
{
if ((((CustomImage)sender).IsMouseCaptured) && (cusImageControl.Cursor == Cursors.SizeAll))
{
Point mouseCurrent = e.GetPosition(null);
double Left = mouseCurrent.X - mouseClick.X;
double Top = mouseCurrent.Y - mouseClick.Y;
mouseClick = e.GetPosition(null);
((CustomImage)sender).SetValue(Canvas.LeftProperty, canvasLeft + Left);
((CustomImage)sender).SetValue(Canvas.TopProperty, canvasTop + Top);
canvasLeft = Canvas.GetLeft(((CustomImage)sender));
canvasTop = Canvas.GetTop(((CustomImage)sender));
}
else if ((((CustomImage)sender).IsMouseCaptured) && (cusImageControl.Cursor == Cursors.SizeNWSE))
{
/*Point mouseCurrent = e.GetPosition(null);
cusImageControl.Height = cusImageControl.canvasTop + mouseClick.Y;
cusImageControl.Width = cusImageControl.canvasLeft + mouseClick.X;
mouseClick = e.GetPosition(null);*/
}
}
private void cusImageControl_MouseDown(object sender, MouseButtonEventArgs e)
{
mouseClick = e.GetPosition(null);
canvasLeft = Canvas.GetLeft(((CustomImage)sender));
canvasTop = Canvas.GetTop(((CustomImage)sender));
((CustomImage)sender).CaptureMouse();
}
private void ContextMenuBringForward_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show("Bring Forward");
}
private void ContextMenuSendBackward_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show("Send Backward");
}
private void ContextMenuMove_Click(object sender, RoutedEventArgs e)
{
cusImageControl.Cursor = Cursors.SizeAll;
}
private void ContextMenuResize_Click(object sender, RoutedEventArgs e)
{
cusImageControl.Cursor = Cursors.SizeNWSE;
}
}
}
See Panel attached property Canvas.SetZIndex. Change z-Index of all elements to 0, and z-Index of your right-clicked control to 1.
void mouseUp(object sender, MouseButtonEventArgs e)
{
foreach (var child in yourCanvas.Children) Canvas.SetZIndex(child, 0);
Canvas.SetZIndex((UIElement)sender, 1);
}
The following code works where selected is defined as my UserControl and set in the mouseDown event.
private void ContextMenuSendBackward_Click(object sender, RoutedEventArgs e)
{
Canvas parent = (Canvas)LogicalTreeHelper.GetParent(this);
foreach (var child in parent.Children)
{
Canvas.SetZIndex((UIElement)child, 0);
}
Canvas.SetZIndex(selected, 1);
}
Thanks to voo for all his help.
My Windows Forms code has two superposed PictureBoxes: a small X on the top corner of a user-loaded image. I'll call them DelImage and Image, respectively. Like this:
http://imgur.com/fsW7R8i
The DelImage appears (Visible = true) on Image_MouseEnter and disappears on Image_MouseLeave. Now, I want the DelImage to have a custom behavior as well when the mouse enters it, but DelImage never fires events (nor MouseEnter, nor MouseLeave, nor Click). I've tried bringing it to front, but it already is in front of the Image.
Here's some code:
private void picImage_MouseEnter(object sender, EventArgs e)
{
delImage.Visible = true;
}
private void picImage_MouseLeave(object sender, EventArgs e)
{
delImage.Visible = false;
}
private void picDelImage_MouseEnter(object sender, EventArgs e)
{
delImage.Visible = true;
this.Cursor = Cursors.Hand;
}
private void picDelImage_MouseLeave(object sender, EventArgs e)
{
this.Cursor = Cursors.Arrow;
}
private void picDelImage_Click(object sender, EventArgs e)
{
Panel currentPanel = this.tlpImages.Controls["pnlImage"] as Panel;
currentPanel.Visible = false;
}
With this code, the picDelImage events are never reached. I never enter DelImage, apparently because I never leave Image. What can I do to make my PictureBoxes have the expected behavior?