I'm having a bit of trouble making drag and drop a button in a panel and panel to recognize it and display Message with buttons name
So far I managed the part of dragging and dropping and recognizing however I'm missing the visual style of dragging, when I press with mouse it will just sit on the same place, it won't follow cursor. How do I make it follow the mouse?
public Form1()
{
InitializeComponent();
panel1.AllowDrop = true;
panel1.DragEnter += panel_DragEnter;
panel1.DragDrop += panel_DragDrop;
button1.MouseDown += button1_MouseDown;
}
private void button1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
button1.DoDragDrop(button1.Text, DragDropEffects.Copy | DragDropEffects.Move);
button1.Location= new Point(e.X, e.Y);
}
private void panel_DragEnter(object sender, System.Windows.Forms.DragEventArgs e)
{
e.Effect = DragDropEffects.Move;
if (e.Data.GetDataPresent(DataFormats.Text))
e.Effect = DragDropEffects.Copy;
else
e.Effect = DragDropEffects.None;
}
private void panel_DragDrop(object sender, System.Windows.Forms.DragEventArgs e)
{
MessageBox.Show(e.Data.GetData(DataFormats.Text).ToString());
}
You'll have to keep in mind that the DoDragDrop method doesn't return the Position you dropped the object. The DragDrop event handles that.
To move the control while you're dragging you use the DragOver event of the panel. In the implementation you need to compensate for the fact that the X and Y coordinates in the EventArgs are Screen based while you need Clientbased coordinates to correctly position the control. The PointToClient is instrumental in that:
private void panel_DragOver(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(typeof(Button).FullName))
{
var draggedButton = (Button)e.Data.GetData(typeof(Button).FullName);
var screenpos = new Point(e.X, e.Y);
var clientPos = panel1.PointToClient(screenpos);
// calc offset
draggedButton.Location = new Point(
clientPos.X + panel1.Left,
clientPos.Y + panel1.Top);
}
}
Notice that your Data now contains the actual Button, instead of only the text. This makes that you can dragdrop multiple buttons, not only button1.
Your DragDrop event should now look like this:
private void panel_DragDrop(object sender, System.Windows.Forms.DragEventArgs e)
{
if (e.Data.GetDataPresent(typeof(Button).FullName))
{
var draggedButton = (Button)e.Data.GetData(typeof(Button).FullName);
MessageBox.Show(draggedButton.Text);
var screenpos = new Point(e.X, e.Y);
var clientPos = panel1.PointToClient(screenpos);
draggedButton.Location = new Point(
clientPos.X + panel1.Left,
clientPos.Y + panel1.Top);
}
}
And DragEnter only slightly changed so it could handle the Button control instead of the text:
private void panel_DragEnter(object sender, System.Windows.Forms.DragEventArgs e)
{
e.Effect = DragDropEffects.Move;
if (e.Data.GetDataPresent(typeof(Button).FullName)) // button
e.Effect = DragDropEffects.Copy;
else
e.Effect = DragDropEffects.None;
}
And finally to get it all started and wired up the constructor code and the MouseDown implementation of the button:
public Form1()
{
InitializeComponent();
panel1.AllowDrop = true;
panel1.DragEnter += panel_DragEnter;
panel1.DragDrop += panel_DragDrop;
panel1.DragOver += panel_DragOver;
button1.MouseDown += button1_MouseDown;
}
private void button1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
button1.DoDragDrop(button1, DragDropEffects.Copy | DragDropEffects.Move);
}
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 want to drag a label2 and drop it into pictureBox1, however it didn't work and i don't know what the problem is
private void pictureBox1_DragDrop(object sender, DragEventArgs e)
{
pictureBox1.AllowDrop = true;
if (CurrentDrag.Equals("clock1"))
{
Label pnlDroggedTheme = (Label)sender;
pnlDroggedTheme.Height = pictureBox1.Height;
pnlDroggedTheme.Width = pictureBox1.Width;
label2.Visible = false;
correct++;
label1.Text = correct.ToString();
MessageBox.Show(" cerrect answer");
}
else
wronge++;
label1.Text = wronge.ToString();
MessageBox.Show("wronge answer");
}
this the DragEnter method for pictureBox1
private void pictureBox1_DragEnter(object sender, DragEventArgs e)
{
here it should copy data if the dropped element's data is from type string which is the label
if (e.Data.GetDataPresent(typeof(System.String)))
{
e.Effect = DragDropEffects.Copy;
}
else
{
e.Effect = DragDropEffects.None;
}
}
the label dragging works just fine but i can't drop it in the picturebox
private void label2_MouseDown(object sender, MouseEventArgs e)
{
CurrentDrag = "clock1";
Label labelV = (Label)sender;
labelV .DoDragDrop(labelV .BackColor, DragDropEffects.Copy);
}
You are making the drag drop event check if the type of data is System.String and perform no action if it isn't, Yet you are doing a drag drop for System.Color. What I think you want to do
Label labelV = (Label)sender;
labelV .DoDragDrop(labelV.Text, DragDropEffects.Copy);
You should either do that or modify your drag drop event to accept System.Color.
Also, you should move your pictureBox1.AllowDrop = true; to the constructor or Form_Load or just set the property to true at design time.
I want to drag panels from one tableLayoutPanel to another. I also want the panels to be copied, not moved; that is, I want them to be copied (from tableLayoutPanel1 to tableLayoutPanel2), leaving the item in tableLayoutPanel1.
Can I do this? If you can give me an idea, it will be great. Thank you
public Form1()
{
InitializeComponent();
panel1.AllowDrop = true;
panel2.AllowDrop = true;
panel3.AllowDrop = true;
panel1.DragEnter += panel_DragEnter;
panel2.DragEnter += panel_DragEnter;
panel1.DragDrop += panel_DragDrop;
panel2.DragDrop += panel_DragDrop;
}
private void panel3_MouseDown(object sender, MouseEventArgs e)
{
DoDragDrop(panel3, DragDropEffects.Copy);
}
private void panel3_MouseMove(object sender, MouseEventArgs e)
{
DoDragDrop(panel3, DragDropEffects.Copy);
}
private void panel_DragEnter(object sender, DragEventArgs e)
{
e.Effect = DragDropEffects.Copy;
}
private void panel_DragDrop(object sender, DragEventArgs e)
{
((Panel)e. Data . GetData(typeof(Panel))).Parent = (Panel)sender;
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
}
You need to create a new panel in the MouseDown handler, set its properties like those of the original, add it to the form and use DoDragDrop with this new panel.
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.
I have this code for dragging on a Panel but its not doing the thing.
I have to choose if I will just Drag&drop or Resize.
I think theres something wrong with my code here in form load.
Anyway I have 5 labels here and a panel named as label1, label2, label3, label4, label5 on panel1.
private void form_Load(object sender, EventArgs e)
{
//for drag and drop
//this.panel1.AllowDrop = true; // or Allow drop in the panel.
foreach (Control c in this.panel1.Controls)
{
c.MouseDown += new MouseEventHandler(c_MouseDown);
}
this.panel1.DragOver += new DragEventHandler(panel1_DragOver);
this.panel1.DragDrop += new DragEventHandler(panel1_DragDrop);
//end of drag and drop
}
void c_MouseDown(object sender, MouseEventArgs e)
{
Control c = sender as Control;
c.DoDragDrop(c, DragDropEffects.Move);
}
void panel1_DragDrop(object sender, DragEventArgs e)
{
Control c = e.Data.GetData(e.Data.GetFormats()[0]) as Control;
lblResizeAmtWord.Visible = false;
if (c != null)
{
c.Location = this.panel1.PointToClient(new Point(e.X, e.Y));
//this.panel1.Controls.Add(c); //disable if already on the panel
}
}
void panel1_DragOver(object sender, DragEventArgs e)
{
e.Effect = DragDropEffects.Move;
}
I used the MouseDown, Up and Move Event of the Control I want to move. Let's say my control name is ctrlToMove.
private Point _Offset = Point.Empty;
private void ctrlToMove_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
_Offset = new Point(e.X, e.Y);
}
}
private void ctrlToMove_MouseUp(object sender, MouseEventArgs e)
{
_Offset = Point.Empty;
}
private void ctrlToMove_MouseMove(object sender, MouseEventArgs e)
{
if (_Offset != Point.Empty)
{
Point newlocation = ctrlToMove.Location;
newlocation.X += e.X - _Offset.X;
newlocation.Y += e.Y - _Offset.Y;
ctrlToMove.Location = newlocation;
}
}