Drag drop between objects of same type?

  • Thread starter Simon Rigby via .NET 247
  • Start date
S

Simon Rigby via .NET 247

Hi ppl,

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

All works fine.

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

The problem is that in OnDragEnter I am looking to see if thecontrol being dragged is of the right type. Of course this meansthat as soon as I mouse down on the source control, the eventfires and thinks I'm dragging it to itself. What I really needis someway of quizzing the source control, to make sure it isn'tthe 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 theevent firing visually
// The problem is that it fires onthe source because the source
// is of the same type as thedestination (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 thesource changes its text to "Copying". I need this event to fireonly 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
 
G

Guest

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
 
S

simonrigby_uk

Bonnie said:
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
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.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.source changes its text to "Copying". I need this event to fire only
when the source is dragged to another instance.
 
S

simonrigby_uk

Thanks that worked.
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
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.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.source changes its text to "Copying". I need this event to fire only
when the source is dragged to another instance.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top