MouseMove doesn't get the right coordinate points - c#

I have a label I am trying to drag. I click on the label, and on the MouseMove() event I am trying to relocate the position of the label.
public void MyLabel_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
((Label)sender).Location = Cursor.Position;
// I have also tried e.Location but none of these moves the label to
// where the the cursor is, always around it, sometimes completely off
}
}

You usually need to store the offset location of the initial mouse down point in the control, or else the control will move on you in a jittering fashion. Then you just do the math:
Point labelOffset = Point.Empty;
void MyLabel_MouseDown(object sender, MouseEventArgs e) {
if (e.Button == MouseButtons.Left) {
labelOffset = e.Location;
}
}
void MyLabel_MouseMove(object sender, MouseEventArgs e) {
if (e.Button == MouseButtons.Left) {
Label l = sender as Label;
l.Location = new Point(l.Left + e.X - labelOffset.X,
l.Top + e.Y - labelOffset.Y);
}
}

Related

how make label move inside panel bounds

i tried this code but when i drag and move label inside panel is normal but when i drag to edge bound of panel it disappear i want it to stop on panel bound is there a way for that?
private void lbl29_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Left)
{
MouseDownLocation = e.Location;
}
panel1.Invalidate();
}
private void lbl29_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Left)
{
lbl29.Left = e.X + lbl29.Left - MouseDownLocation.X;
lbl29.Top = e.Y + lbl29.Top - MouseDownLocation.Y;
}
}
You can try these:
Controls.SetChildIndex(lbl29, 0); or
Controls.SetChildIndex(panel1, 1); or
lbl29.BringToFront(); or
panel1.SendToBack();
But Controls.SetChildIndex(lbl29, 0); and Controls.SetChildIndex(panel1, 1); are more useful.

Check if a control is on top of another

I've been trying to make a drag and drop game. I have 4 panels and 4 labels. You have to drag the labels on top of the correct panel.
The problem is checking if a label is on top of the panel. The user can frely drag the labels.
private void button1_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Left)
{
button1.Left = e.X + button1.Left - MouseDownLocation.X;
button1.Top = e.Y + button1.Top - MouseDownLocation.Y;
}
}
private void button1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Left)
{
MouseDownLocation = e.Location;
}
}
Here is the code i used to move the control. I have to mention that this is a test project, so I used a button instead of a label, but the idea is the same.
Is there any way if I can check whether a control is on top of another or not ?
After each move, simply get the Rectangle from the Bounds property of your button and panel, then use either Intersect() or Contains():
private void button1_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
button1.Location = new Point(e.X + button1.Left - MouseDownLocation.X, e.Y + button1.Top - MouseDownLocation.Y);
Rectangle btnRC = button1.Bounds;
Rectangle pnlRC = panel1.Bounds;
// see if the rectangles INTERSECT
if (pnlRC.IntersectsWith(btnRC))
{
panel1.BackColor = Color.Green;
}
else
{
panel1.BackColor = this.BackColor;
}
// see if the panel COMPLETELY CONTAINS the button
if (pnlRC.Contains(btnRC))
{
panel1.BackColor = Color.Green;
}
else
{
panel1.BackColor = this.BackColor;
}
}
}
To check if the mouse is over the control, you can check if the Cursor.Position is in the ClientRectangle of the control, but you first need to call PointToClient method of the control to convert the cursor position relative to the panel's (0,0), for example:
var b = panel1.ClientRectangle.Contains(panel1.PointToClient(Cursor.Position));
The flag can be checked in the same event handler which is used to move the control, for example:
if(b) panel1.BackColor = Color.Red; else panel1.BackColor = Color.Gray;

How we move a label when mouse click on it and when mouse key up then stop moving inside a group box

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;

Separating mouse down and mouse click events

I have two events wired to a control. A mouse click event plays an audio file. A drag event made up from mouse down, mouse move and mouse up drags the control vertically. By dragging the control vertically the control get assigned a different song.
The problem I have is that when I drag the control the music plays. I do not want this since this is a dragging operation.
Music should play only when control is clicked and note moved.
I think that setting boundaries, e.g. when control moves only 5 pixels play sound otherwise drag control should solve the problem but I tried to create them unsuccessfully.
Can you please guide how to solve this problem preferably with code snippets.
Thank You.
The Drag
int Locked_yLoc; int index;
private void StartDrag(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
sp1.SoundLocation = this.pitch + ".wav";
sp1.Play();
tm1.Interval = 100 * this.noteDuration;
tm1.Start();
isDragging = true;
pitch = e.Y;
this.Location = new Point(this.Location.X, pitch);
}
}
private void StopDrag(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
isDragging = false;
this.Location = new Point(this.Location.X, Locked_yLoc);
pitch = index;
//lbl8.Text = pitch.ToString();
}
}
private void OnDrag(object sender, MouseEventArgs e)
{
if (isDragging)
{
this.Top = this.Top + (e.Y - pitch);
Locked_yLoc = (int)Math.Round((this.Top + (e.Y - pitch)) / 10.0) * 10;
for (int i = 0; i < yLocation.Length; i++)
{
if (Locked_yLoc == yLocation[i])
{
index = CorrPitch[i];
}
}
}
}
The Click
private void MusicNote_MouseClick(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
sp1.SoundLocation = this.pitch + ".wav";
sp1.Play();
tm1.Interval = 100 * this.noteDuration;
tm1.Start();
}
}

drag and drop picturebox between forms

what is the best way to do this y need move a picturebox and drop in the other form y used this for move my picture
private void pictureBox2_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
x = e.X;
y = e.Y;
}
}
private void pictureBox2_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
pictureBox2.Left += (e.X -x);
pictureBox2.Top += (e.Y - y);
}
}
The best way is to use the Drag and Drop eventing.
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.dragdrop.aspx
A good article describing nearly exactly what I believe you're trying to do is here:
http://ondotnet.com/pub/a/dotnet/2002/11/11/dragdrop.htm
Hope that helps.

Categories