Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Finding the source control on ta dragdrop event... 2

Status
Not open for further replies.

mmilan

Programmer
Jan 22, 2002
839
GB
Hi everyone...

I have a Winforms application in VB2005, and I have a group of textboxes (one for each month in the year) which obviously accept text. What I am trying to do is to allow the user to drag the content of one textbox into another (copy operation).

Now, the basic mechanics of that are all very well, but what I also need to do is to to check that the source control in this dragdrop is another one of my boxes. Afterall, I wouldn't want people to be dropping stuff from anywhere into my boxes...

The event args passed on the DragDrop event don't seem to support a means of telling where the data came from - which is a bit naff. I know that they have to cater for the fact that the data might come from another application, but surely an "InternalSource as control" property wouldn't have been so much to ask - it could be null for external sources...

So - anyone got any ideas how I can check where the data came from?

At the moment I'm considering passing a reference to the source control itself as the data, rather than merely it's text property. I could then do a if A is B test, but surely there has to be a more elegant solution?

Anyone?

lol

mmilan
 
I've not needed to do drag and drop since I started using vs2k5 so I'm not sure how it handles it. Just off the fly you might see if it passes an unused property like a tag and use that. You may have already thought of that or it may not have one, but I thought it was worth mentioning.

-I hate Microsoft!
-Forever and always forward.
-My kingdom for a edit button!
 
Here's a way which isn't very elegant, but will probably work.

1. Keep a variable to hold your text being dragged.
2. When you perform your .DoDragDrop on your textboxes, set that variable to the .Text of the TextBox
3. When your DragEnter event is raised, compare the data being dragged over to the variable you created. If they are the same, then you are good to go. If not, then it came from some other place.
 
I've not needed to do drag and drop since I started using vs2k5 so I'm not sure how it handles it. Just off the fly you might see if it passes an unused property like a tag and use that. You may have already thought of that or it may not have one, but I thought it was worth mentioning.

-I hate Microsoft!
-Forever and always forward.
-My kingdom for a edit button!

Thanks for that... I didn't notice anything in the event argument that I could use, but as I said, there's always the option of passing a reference to the source control itself...

Just seems odd to me that they don't provide something on the event argument object for it - I would have thought most people would be interested in checking that the dropped data comes from a proper source.

mmilan
 
Here's a way which isn't very elegant, but will probably work.

1. Keep a variable to hold your text being dragged.
2. When you perform your .DoDragDrop on your textboxes, set that variable to the .Text of the TextBox
3. When your DragEnter event is raised, compare the data being dragged over to the variable you created. If they are the same, then you are good to go. If not, then it came from some other place.

The problem is that this doesn't confirm that the information came from an approved source - just that the information being dropped has the same content as a proper source control at the time the that control last did a dragdrop...

I'm thinking of using the something like txt1.DoDragDrop(txt1, GetType(System.Windows.Forms.TextBox)) - and then using the "Is" operator on the DragDrop event of the destination control to compare against a list of allowed sources...

Thanks anyway chaps...

Martin
 
The problem is that this doesn't confirm that the information came from an approved source

What's the difference? If someone somehow happens to have the same string in another application and for some reason attempts to drag it to one of your textboxes, then the end result is the same.
 
Couldn't you just have a form level variable?

[tt]Private DragDropStartedByMe As Boolean = False[/tt]

When the Drag starts set this variable to True

When the user attempts to Drop, check this variable, if it is False disallow the Drop, otherwise allow the Drop and set the variable to False.


It is the method I use and I have never had any problems with it.


Hope this helps.

[vampire][bat]
 

What's the difference? If someone somehow happens to have the same string in another application and for some reason attempts to drag it to one of your textboxes, then the end result is the same.

For the particular application I have in mind (just letting you copy the values around) then I don't suppose it matters, but there's also the time difference to be taken into account... Just because a piece of data was valid five minutes ago (when you saved the value) it doesn't mean is valid now when someone comes along to drop it in your box...

I take your point though - it gets me around this particular problem... However, I am leaving my current employer in the next few weeks, and the lad taking over my role is not not very experienced - I would therefore like to set a consciencious example of making sure that the data comes from somewhere I am interested in listening to...

Thanks for your thoughts.

Martin.
 
Hmm...

I like EarthAndFire's solution better than the others - including my own...

I'm feeling a little thick for not thinking of that myself... It has the virtue of being fairly simple so the new chap can follow it was well...

Cheers,

Martin.
 
Just seems odd to me that they don't provide something on the event argument object for it - I would have thought most people would be interested in checking that the dropped data comes from a proper source.
It isn't that strange to me, but that is likely because of when/how I learned. It has actually been years since I've had a project where I wanted any drag/drop functionality. I was using Borland C++ Builder 4&5. It would pass a pointer to the object. I was doing more that dealing with just the text though so I needed to check the type and cast it as the correct object anyway.

That should really work because something from else where should be a different type. If you are really concerned you should be able to inherit your own text box and check if the type is not a text box, but your custom text box instead.

-I hate Microsoft!
-Forever and always forward.
-My kingdom for a edit button!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top