I feel stupid asking this, but could someone just tell me how I would use the drag and drop feature? I want the users to be able to drag a bunch of files over into a MDI form
Printable View
I feel stupid asking this, but could someone just tell me how I would use the drag and drop feature? I want the users to be able to drag a bunch of files over into a MDI form
http://www.vbforums.com/search.php?s...der=descending ;)
:D :bigyello:
HI,
Not sure what you are asking.
You don't drop files on to forms as such. You load files into your application and then display their contents, e.g. data in a Datagrid or text in a TextBox etc.
I do not think there is any automatic drag/drop in .NET (there was in VB6) and it has to be coded. To give you a start, the following creates the collect & drag part for a TextBox. Try the MSDN for the remaining code to prepare the target
If you want to drag and drop the contents of a TextBox, etc, you:
set the AllowDrop property of the target to true
write code to detect the intention to drag/drop e.g. in the MouseMove event of the source TextBox
If e.Button=0 then exit sub (if no button is pressed)
Create a new DataObject instance to hold the contents of the source and exit if no data to be dragged
Dim txtTemp as TextBoxBase=DirectCast(sender, TextBoxBase)
if txtTemp.TextLength = 0 then Exit Sub
if e.x>=0 and e.x < txtTemp.Width and e.y>=0 and e.y<tbase.height Exit Sub (check if cursor is still inside control's borders)
Dim datTemp as new DataObject()
if txtTemp.SelectionLength>0 then
datTemp.SetData(dataFormats.text, txtTemp.SelectedText
Else
datTemp.SetData(dataFormats.text, txtTemp.Text
end if
start the drag operation & wait until it is completed
Dim effect as DragDropEffects = DragDropEffects.Copy or DragDropEffects.Move
effect=txtTemp.DoDragDrop(datTemp, effect)
Delete source contents if required
If effect = DragDropEffects.Move Then
If txtTemp.SelectionLength > 0 then
txtTemp.Text = ""
Else
txtTemp.Text = ""
End if
End if
Hope I have not done any typos!!
HI,
I've just seen Edneeis' response. That thread resolution seems a lot simpler than mine. Let us know if it works.
Check the links I provided in here :
http://www.vbforums.com/showthread.p...hreadid=279378
Thanks all... kinda busy today but will look at this over the weekend again:)
Edneeis, I almost forgot there is a search button:D thanks
I actually wrote a utiiliity using the drag/drop functionality. I will post my code in the morning when I get to work.
Basically though, you set the control property "allow drop" to true
and in the "dragLeave" or dragdrop event, that's where you get the action.
would you post it then?:DQuote:
Originally posted by thephantom
I actually wrote a utiiliity using the drag/drop functionality. I will post my code in the morning when I get to work.
Basically though, you set the control property "allow drop" to true
and in the "dragLeave" or dragdrop event, that's where you get the action.
sorry it took so long to post. I couldn't locate the file. When anyone downloads this file, you'll need to go through and alter some of the code because it was written with some hard-coding.
thanks, got it working:)Quote:
Originally posted by thephantom
sorry it took so long to post. I couldn't locate the file. When anyone downloads this file, you'll need to go through and alter some of the code because it was written with some hard-coding.
Here is a sample project I ran across on the VB Resource Kit as well.
Hi Phantom,
I downloaded the Drag drop project you quoted and, of course it works fine .......BUT Could you please tell me how the drag event of txtLowerRight is fired?
The code which is fired is in the sub
VB Code:
Private Sub TextBoxLowerRight_DragDrop(ByVal sender As System.Object, ByVal e As System.Windows.Forms
How is this sub called?
highlight the "Source Text" and drag that text over it. It gets fired after the DragEnter event is fired. If e.Effect isnt modified or is set to e.Effect = DragDropEffects.None in the DragEnter event, then the DragDrop event wont be firedQuote:
Originally posted by taxes
Hi Phantom,
I downloaded the Drag drop project you quoted and, of course it works fine .......BUT Could you please tell me how the drag event of txtLowerRight is fired?
The code which is fired is in the sub
VB Code:
Private Sub TextBoxLowerRight_DragDrop(ByVal sender As System.Object, ByVal e As System.Windows.Forms
How is this sub called?
thanks phantom for the sample
Hi,
"highlight the "Source Text" and drag that text over it. It gets fired after the DragEnter event is fired. If e.Effect isnt modified or is set to e.Effect = DragDropEffects.None in the DragEnter event, then the DragDrop event wont be fired"
Sorry, I did not make myself clear but I have just noticed the answer!!
I was wondering why the drag/drop method of txtLowerRight fired the sub TextBoxLowerRight_DragDrop but I now notice that the handle of the sub refers to txtLowerRight and looking at other threads I notice that you can have more than one event firing a sub by adding handles.
Many thanks. I'm learning slowly (and sometimes painfully:eek: :eek: :eek:
:D sorry I didnt understand your question either....Quote:
Originally posted by taxes
Hi,
"highlight the "Source Text" and drag that text over it. It gets fired after the DragEnter event is fired. If e.Effect isnt modified or is set to e.Effect = DragDropEffects.None in the DragEnter event, then the DragDrop event wont be fired"
Sorry, I did not make myself clear but I have just noticed the answer!!
I was wondering why the drag/drop method of txtLowerRight fired the sub TextBoxLowerRight_DragDrop but I now notice that the handle of the sub refers to txtLowerRight and looking at other threads I notice that you can have more than one event firing a sub by adding handles.
Many thanks. I'm learning slowly (and sometimes painfully:eek: :eek: :eek:
btw if you have one sub which handles more than one controls' event, try to pick a general name for it. For example if txtRight_TextChanged handles the event for 3 textboxes then I would say you better change the sub name to something else, like txtPositions_TextChanged