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.
Related
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.
I have a PictureBox1 draggable with this code:
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
x = e.X;
y = e.Y;
}
}
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
if (pictureBox1.Left + pictureBox1.Width> panel1.Width)
pictureBox1.Left += (e.X - x);
}
}
But I can't get bound restrictions,
I just want move the Picture inside a Panel, like this:
Example
Any ideas?
Thanks
Try this:
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
int left = 0;
if (pictureBox1.Left >= 0 && pictureBox1.Right <= panel1.Width)
left = pictureBox1.Left + (e.X - x);
left = Math.Min(panel1.Width - pictureBox1.Width , left);
left = Math.Max(0, left);
pictureBox1.Left = left;
}
}
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();
}
}
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);
}
}
I am trying to write a program which will allow a user to drag and drop images into the program and then be able to select the image, move it about, re-size it, crop it etc.
So far I have created a windows form which consists of a panel. A user can drag a picture file onto the panel and a picturebox will be created at the coordinates of the mouse when it is dropped and an image is loaded in the picturebox. I can add several images in this fashion.
Now I want to allow the user to manipulate and move about the images that they have dropped into the panel.
I have tried searching for solutions but cant seem to find an answer which I understand.
Any help is much appreciated..
This is my current code
private void panel1_DragEnter(object sender, DragEventArgs e)
{
e.Effect = DragDropEffects.All;
}
private void panel1_DragDrop(object sender, DragEventArgs e)
{
String[] imagePaths = (String[])e.Data.GetData(DataFormats.FileDrop);
foreach (string path in imagePaths)
{
Point point = panel1.PointToClient(Cursor.Position);
PictureBox pb = new PictureBox();
pb.ImageLocation = path;
pb.Left = point.X;
pb.Top = point.Y;
panel1.Controls.Add(pb);
//g.DrawImage(Image.FromFile(path), point);
}
}
You can get the mouse position when the user initially clicks and then track the mouse position in the PictureBox's MouseMove event. You can attach these handlers to multiple PictureBoxes.
private int xPos;
private int yPos;
private void pb_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
xPos = e.X;
yPos = e.Y;
}
}
private void pb_MouseMove(object sender, 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);
}
}
}
For dynamic PictureBoxes you can attach the handlers like this
PictureBox dpb = new PictureBox();
dpb.MouseDown += pb_MouseDown;
dbp.MouseMove += pb_MouseMove;
//fill the rest of the properties...