Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / WinForm General / April 2005

Tip: Looking for answers? Try searching our database.

Drag drop between objects of same type?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Simon Rigby - 09 Apr 2005 04:03 GMT
Hi ppl,

I have a situation where a user can add a user control to another control (like a scratchpad) at runtime. I've done this by way of a context menu. The user selects to add a new item from the context menu and then clicks on the scratchpad where they want to create the new control.

All works fine.

I want a user to be able to establish a relationship between any two controls by dragging one instance to another.

The problem is that in OnDragEnter I am looking to see if the control being dragged is of the right type. Of course this means that as soon as I mouse down on the source control, the event fires and thinks I'm dragging it to itself. What I really need is someway of quizzing the source control, to make sure it isn't the same instance as the destinataion.

Some code attached.

Cheers

The following two methods are part of class TaskSketchPad
This is fired when the user adds a control to the scratchpad.

protected override void OnMouseDown(MouseEventArgs e) {
   if (e.Button == MouseButtons.Left){
       switch ( _state ){   
       case State.Inserting:
        TaskBox tb = new TaskBox("New", "");
        tb.Left = e.X;
        tb.Top = e.Y;
        tb.Width = 100;
        tb.Height = 18;
        tb.AllowDrop = true;
        tb.DragEnter += new DragEventHandler(TaskBox_DragEnter);
        this.Controls.Add(tb);
        ChangeState(State.Normal);
        break;
       }
    }
    base.OnMouseDown (e);
   }
}

The event handler

void TaskBox_DragEnter(object sender, DragEventArgs e) {
   if ( e.Data.GetDataPresent(typeof(TaskBox))) {
    e.Effect = DragDropEffects.Copy;
    TaskBox tb = (TaskBox)sender;
    tb.Text = "Copying"; // This is simply here so I can see the event firing visually
                            // The problem is that it fires on the source because the source
                            // is of the same type as the destination (and hence the if check is true
    tb.Invalidate();
   }
   else {
    e.Effect = DragDropEffects.None;
   }
}

And finally in the TaskBox class this method

protected override void OnMouseDown(MouseEventArgs e) {
   DoDragDrop(this, DragDropEffects.Copy);
   base.OnMouseDown (e);
}

So in effect when I drag any instance to another instance the source changes its text to "Copying". I need this event to fire only when the source is dragged to another instance.

I tried modiying the if statement in TaskBox_DragEnter() to
if (!e.Data.Equals(sender) && e.Data.GetDataPresent(typeof(TaskBox)))
, but no joy.

Any help greatly appreciated. 4am. I'm off to bed :)

Thanks
Simon Rigby
Scotland
Bonnie Berent [C# MVP] - 11 Apr 2005 01:43 GMT
Simon,

I think you can probably use the .Equal() method to be sure you're dragging
into a different TaskBox object. So, something like this in your DragEnter:

object o = e.Data.GetData(DataFormats.Serializable);
TaskBox tb = (TaskBox)sender;

if (o is TaskBox && !o.Equals(tb))
{
  // they're not equal, do your thing
}

~~Bonnie

> Hi ppl,
>
[quoted text clipped - 74 lines]
>
> <Id>SrIET38ZtUuYEqd5pn3s1g==</Id>
simonrigby_uk@hotmail.com - 14 Apr 2005 17:11 GMT
> Simon,
>
[quoted text clipped - 14 lines]
> >
> > I have a situation where a user can add a user control to another control (like a scratchpad) at runtime. I've done this by way of a
context menu. The user selects to add a new item from the context menu
and then clicks on the scratchpad where they want to create the new
control.

> > All works fine.
> >
> > I want a user to be able to establish a relationship between any two controls by dragging one instance to another.
> >
> > The problem is that in OnDragEnter I am looking to see if the control being dragged is of the right type. Of course this means that
as soon as I mouse down on the source control, the event fires and
thinks I'm dragging it to itself. What I really need is someway of
quizzing the source control, to make sure it isn't the same instance as
the destinataion.

> > Some code attached.
> >
[quoted text clipped - 47 lines]
> >
> > So in effect when I drag any instance to another instance the source changes its text to "Copying". I need this event to fire only
when the source is dragged to another instance.

> > I tried modiying the if statement in TaskBox_DragEnter() to
> > if (!e.Data.Equals(sender) && e.Data.GetDataPresent(typeof(TaskBox)))
[quoted text clipped - 12 lines]
> >
> > <Id>SrIET38ZtUuYEqd5pn3s1g==</Id>
simonrigby_uk@hotmail.com - 14 Apr 2005 17:12 GMT
Thanks that worked.

> Simon,
>
[quoted text clipped - 14 lines]
> >
> > I have a situation where a user can add a user control to another control (like a scratchpad) at runtime. I've done this by way of a
context menu. The user selects to add a new item from the context menu
and then clicks on the scratchpad where they want to create the new
control.

> > All works fine.
> >
> > I want a user to be able to establish a relationship between any two controls by dragging one instance to another.
> >
> > The problem is that in OnDragEnter I am looking to see if the control being dragged is of the right type. Of course this means that
as soon as I mouse down on the source control, the event fires and
thinks I'm dragging it to itself. What I really need is someway of
quizzing the source control, to make sure it isn't the same instance as
the destinataion.

> > Some code attached.
> >
[quoted text clipped - 47 lines]
> >
> > So in effect when I drag any instance to another instance the source changes its text to "Copying". I need this event to fire only
when the source is dragged to another instance.

> > I tried modiying the if statement in TaskBox_DragEnter() to
> > if (!e.Data.Equals(sender) && e.Data.GetDataPresent(typeof(TaskBox)))
[quoted text clipped - 12 lines]
> >
> > <Id>SrIET38ZtUuYEqd5pn3s1g==</Id>

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.