Drag and Drop

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • =?Utf-8?B?VHJlY2l1cw==?=

    Drag and Drop

    Hello, Newsgroupians:

    I've been looking on the Internet for some resources to create a simple
    drag-and-drop application, but they all seem too complicated or not what I
    need.

    I have created a Form with a ToolStrip that has a couple of buttons at the
    top. What I want to do is allow the user to drag one of the button's from
    the toolstrip to the client area. Once the user let's go, I want to create a
    button in the client area at that point. This mimics Visual Studio when
    visually creating Forms. Does anyone have any short code or resources that
    could allow me to do this?

    Thank you.


    Trecius
  • cfps.Christian

    #2
    Re: Drag and Drop

    On Nov 5, 11:19 am, Trecius <Trec...@discus sions.microsoft .comwrote:
    Hello, Newsgroupians:
    >
    I've been looking on the Internet for some resources to create a simple
    drag-and-drop application, but they all seem too complicated or not what I
    need.
    >
    I have created a Form with a ToolStrip that has a couple of buttons at the
    top. What I want to do is allow the user to drag one of the button's from
    the toolstrip to the client area. Once the user let's go, I want to create a
    button in the client area at that point. This mimics Visual Studio when
    visually creating Forms. Does anyone have any short code or resources that
    could allow me to do this?
    >
    Thank you.
    >
    Trecius
    Someone else might be able to come up with something more graceful but
    I would just do it all manually.

    OnMouseDown = set bMoving = true;
    OnMouseMove = if (bMoving) { (copy control and set location = mouse.X
    and mouse.Y) }
    OnMouseUp = Create new control on form at Mouse.X and Mouse.Y set
    bMoving = false;

    Comment

    • Andy Mueller

      #3
      Re: Drag and Drop

      Trecius wrote:
      Hello, Newsgroupians:
      >
      I've been looking on the Internet for some resources to create a simple
      drag-and-drop application, but they all seem too complicated or not what I
      need.
      Here are some simple explanations:




      >
      I have created a Form with a ToolStrip that has a couple of buttons at the
      top. What I want to do is allow the user to drag one of the button's from
      the toolstrip to the client area. Once the user let's go, I want to create a
      button in the client area at that point. This mimics Visual Studio when
      visually creating Forms. Does anyone have any short code or resources that
      could allow me to do this?
      Here's some sample code:

      using System.Windows. Forms;
      using System.Drawing;
      using System;

      namespace WindowsApplicat ion5
      {
      public partial class Form1 : Form
      {
      public Form1()
      {
      InitializeCompo nent();

      newToolStripBut ton.MouseDown += newToolStripBut ton_MouseDown;
      newToolStripBut ton.MouseUp += newToolStripBut ton_MouseUp;
      newToolStripBut ton.MouseMove += newToolStripBut ton_MouseMove;
      newToolStripBut ton.MouseLeave += newToolStripBut ton_MouseLeave;

      AllowDrop = true;// just for demo purposes :-)
      }
      void newToolStripBut ton_MouseLeave( object sender, EventArgs e)
      {
      isDragging = false;
      }
      void newToolStripBut ton_MouseMove(o bject sender, MouseEventArgs e)
      {
      if(!isDragging)
      return;
      Size dragSize = SystemInformati on.DragSize;
      int dx = Math.Abs(dragPo int.X - e.X);
      int dy = Math.Abs(dragPo int.Y - e.Y);

      if(dx < dragSize.Width || dy < dragSize.Height )
      return;
      IDataObject data = new DataObject();
      data.SetData(ne wToolStripButto n);// D&D data
      DragDropEffects res = DoDragDrop(data , DragDropEffects .All
      | DragDropEffects .Link);
      }
      void newToolStripBut ton_MouseUp(obj ect sender, MouseEventArgs e)
      {
      isDragging = false;
      }
      void newToolStripBut ton_MouseDown(o bject sender, MouseEventArgs e)
      {
      isDragging = true;
      dragPoint = new Point(e.X, e.Y);
      }
      private bool isDragging = false;
      private Point dragPoint = new Point();

      protected override void OnDragOver(Drag EventArgs drgevent)
      {
      if (drgevent.Data. GetDataPresent( typeof(ToolStri pButton)))
      drgevent.Effect = DragDropEffects .Link;
      }
      protected override void OnDragDrop(Drag EventArgs drgevent)
      {
      if (!drgevent.Data .GetDataPresent (typeof(ToolStr ipButton)))
      return;
      MessageBox.Show (((ToolStripBut ton)
      drgevent.Data.G etData(typeof (ToolStripButto n))).Text);
      }
      }
      }


      HTH,
      Andy

      --
      You can email me by removing the NOSPAM parts below:
      xmen40NOSPAM@gm xNOSPAM.net

      Comment

      Working...