How to replicate the ItemDrag event

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Tom P.

    How to replicate the ItemDrag event

    I have a bunch of colors as preferences in my application (Title
    color, text color, border color, etc...). I'd like the option to copy
    a color from one parameter to another by dragging the first color and
    dropping it in the second color parameters space.
    My question is how do I replicate the ItemDrag event that starts the
    whole thing off? I'm stuck using either Buttons or PictureBoxes (due
    to the need to display the selected color to the user). So how do I
    replicate the ItemDrag event in an object that doesn't support it?

    Tom P.
  • Pavel Minaev

    #2
    Re: How to replicate the ItemDrag event

    On Aug 1, 4:04 pm, "Tom P." <padilla.he...@ gmail.comwrote:
    I have a bunch of colors as preferences in my application (Title
    color, text color, border color, etc...). I'd like the option to copy
    a color from one parameter to another by dragging the first color and
    dropping it in the second color parameters space.
    My question is how do I replicate the ItemDrag event that starts the
    whole thing off? I'm stuck using either Buttons or PictureBoxes (due
    to the need to display the selected color to the user). So how do I
    replicate the ItemDrag event in an object that doesn't support it?
    I'm not sure what you mean by "replicatin g ItemDrag". ItemDrag is a
    special event for controls which are containers of items (which are
    not controls), and you have to detect dragging of a particular item.
    For plain controls, you can just use MouseDown/MouseMove as usual -
    basically, you should detect that user has pressed the mouse button
    down, and has moved the mouse while still holding the button so that
    the distance passed by mouse cursor is larger than
    SystemInformati on.DragSize. At that moment, you can invoke DoDragDrop,
    and the rest is all the same.

    Comment

    • Tom P.

      #3
      Re: How to replicate the ItemDrag event

      On Aug 1, 7:43 am, Pavel Minaev <int...@gmail.c omwrote:
      On Aug 1, 4:04 pm, "Tom P." <padilla.he...@ gmail.comwrote:
      >
      I have a bunch of colors as preferences in my application (Title
      color, text color, border color, etc...). I'd like the option to copy
      a color from one parameter to another by dragging the first color and
      dropping it in the second color parameters space.
      My question is how do I replicate the ItemDrag event that starts the
      whole thing off? I'm stuck using either Buttons or PictureBoxes (due
      to the need to display the selected color to the user). So how do I
      replicate the ItemDrag event in an object that doesn't support it?
      >
      I'm not sure what you mean by "replicatin g ItemDrag". ItemDrag is a
      special event for controls which are containers of items (which are
      not controls), and you have to detect dragging of a particular item.
      For plain controls, you can just use MouseDown/MouseMove as usual -
      basically, you should detect that user has pressed the mouse button
      down, and has moved the mouse while still holding the button so that
      the distance passed by mouse cursor is larger than
      SystemInformati on.DragSize. At that moment, you can invoke DoDragDrop,
      and the rest is all the same.
      The steps you outlined are what I meant. I didn't realise I could
      invoke DoDragDrop when not in a dragdrop event, I'll have to see if
      there's enough information at that time. Now to figure out how to tell
      if the mouse has moved farther than DragSize (Basically how to compare
      a linear measurement to a co-ordinate measurement).

      But thanks for the DoDragDrop tip. I just didn't realize.

      Tom P.

      Comment

      • Pavel Minaev

        #4
        Re: How to replicate the ItemDrag event

        On Aug 1, 4:54 pm, "Tom P." <padilla.he...@ gmail.comwrote:
        The steps you outlined are what I meant. I didn't realise I could
        invoke DoDragDrop when not in a dragdrop event, I'll have to see if
        there's enough information at that time. Now to figure out how to tell
        if the mouse has moved farther than DragSize (Basically how to compare
        a linear measurement to a co-ordinate measurement).
        DragSize is actually an instance of struct Size, so it defines Width
        and Height of the drag rectangle - if horizontal mouse delta is larger
        than Width, or if vertical delta is larger than Height, then it's time
        to start dragging.

        Comment

        • Tom P.

          #5
          Re: How to replicate the ItemDrag event

          On Aug 1, 8:56 am, Pavel Minaev <int...@gmail.c omwrote:
          DragSize is actually an instance of struct Size, so it defines Width
          and Height of the drag rectangle - if horizontal mouse delta is larger
          than Width, or if vertical delta is larger than Height, then it's time
          to start dragging.
          I got it!

          Thanks for the help. I had to do a (old.X - new.X >= DragSize.Width)
          || (old.Y - new.Y >= DragSize.Height ).

          And the dragdrop was exciting. (I am posting it for those looking to
          figure this bloody technology out. It was hard enough to find I hope
          this helps others.)

          Once you have established that you are dragging (see above) you do the
          following:

          DataObject tempDataObject = new DataObject(this .BackColor);
          DoDragDrop(temp DataObject, DragDropEffects .Copy);

          This creates a DataObject and stores the data you want to pass to the
          drop target in it (in my case a Color object). This is the only method
          that you look at from the "draggers" point of view. The other events
          are fired by the Drop target (don't forget the drop target could be
          the same as the object that started the drag so it might look
          confusing).

          Then, in the DragEnter and DragOver events you check your object and
          make sure you are accepting the data that is being dragged by checking
          the DataFormats like so...

          List<stringlstT est;

          lstTest = new List<string>(dr gevent.Data.Get Formats());

          if (lstTest.Contai ns("FileDrop") )
          {
          //If you want to do different things based on different keys then
          check those too..
          if ((drgevent.KeyS tate & 8) == 8)
          {
          drgevent.Effect = DragDropEffects .Copy;
          }
          else
          {
          drgevent.Effect = DragDropEffects .Move;
          }

          }

          Then, in the DragDrop event you get the object out of the DragObject
          structure...

          List<stringItem Formats;

          ItemFormats = new List<string>(dr gevent.Data.Get Formats());

          if (ItemFormats.Co ntains("System. Drawing.Color") )
          {
          this.BackColor = (Color)drgevent .Data.GetData(t ypeof(Color));
          }


          Then process the data any way you feel you need to.

          Hope this helps others.
          Tom P.

          Comment

          • Pavel Minaev

            #6
            Re: How to replicate the ItemDrag event

            On Aug 1, 9:11 pm, "Tom P." <padilla.he...@ gmail.comwrote:
            if (ItemFormats.Co ntains("System. Drawing.Color") )
            {
                 this.BackColor = (Color)drgevent .Data.GetData(t ypeof(Color));
            >
            }
            Note that IDataObject.Get Data() returns null if such type was not
            found, so you typically just ask for the type and check for null - no
            need to do GetFormats()/Contains(). If you need to check whether the
            data is of a given format without retrieving it,
            IDataObject.Get DataPresent() is probably still more efficient.

            Comment

            Working...