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
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