Drag & Drop - Performing "Copy" by default

B

Barry Moon

Hi

I have (two) tree view controls that can work in either "read-only" or
"normal" mode. In "normal mode", items can be moved between the trees
using drag & drop. When a tree is in "read only" mode, items can only
be copied from it, not moved.

However, to get the copy operation to work, the user must press Ctrl.
Is there a way to start a drag operation with the "Copy" effect active
by default?

I'd like behaviour similar to Windows Explorer - e.g., dragging a file
to the same drive will perform a "move" operation, but dragging to
another drive will perform a "copy" by default, without the user
necessarily having to press Ctrl.

I'd appreciate any help with this.

Cheers,
Barry
 
G

Guest

Hi

You can use the keycode property to assign a default ctrl-pressed state. Use
this only inside the event and do not forget to reassign the keycode to
normal after the event is serviced.

You will get a list of all keycodes in MSDN help.

Hope this suffices your requirement.

Regards
Amey
 
B

Barry Moon

Hi Amey

That sounds like a good idea, but I'm not sure which KeyCode property
you're referring to.

It seems to me that I should doing this work either in the initial
"DoDragDrop" call, or perhaps in the subsequent "treeView_GiveFeedback"
event handler. However, neither of these methods seem to provide a
KeyCode property, or any other way of specifying the keyboard state.

I hope I'm missing something because your solution sounds promising.
Are you able to expand on your suggestion?

Thanks very much in advance.

Cheers,
Barry
 
B

Barry Moon

Hi

I never managed to get this working, but it seems to me that it
shouldn't be a difficult thing to do.

Does anyone else have any suggestions?

Thanks in advance,
Barry
 
J

JP

Barry,
In the DoDragDrop, set the desired AllowedEffects ... ie
dim dEff as DragDropEffects
If not ctl.IsReadOnly Then 'or whatever your test would be
dEff = DragDropEffects.Copy Or DragDropEffects.Move
Else
dEff = DragDropEffects.Copy
End If
Dim res As DragDropEffects = .DoDragDrop(Dobj, dEff)

Good Luck,
Jim Parsells
 
B

Barry Moon

Hi Jim

Thanks for that. Unfortunately, that's what my code's already doing.

The problem is that when I specify that "copy" is the only operation
allowed, the drag operation starts off in a disabled state - because
"move", the default operation, isn't allowed.

When the user presses CTRL (i.e. switches to "copy" mode), the drag
operation becomes enabled again - because "copy" IS allowed.

What I want is for the drag operation to start in the "copy" mode by
default when copy is the only operation allowed, without the user
having to press CTRL.

Thanks again for your help.
Barry
 
J

JP

How about this?
Public Class Form1
Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

Public Sub New()
MyBase.New()

'This call is required by the Windows Form Designer.
InitializeComponent()

'Add any initialization after the InitializeComponent() call

End Sub

'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As
Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub

'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer

'NOTE: The following procedure is required by the Windows Form
Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents tvRO As System.Windows.Forms.TreeView
Friend WithEvents tvRW As System.Windows.Forms.TreeView
Friend WithEvents Label1 As System.Windows.Forms.Label
Friend WithEvents Label2 As System.Windows.Forms.Label
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()
Me.tvRO = New System.Windows.Forms.TreeView
Me.tvRW = New System.Windows.Forms.TreeView
Me.Label1 = New System.Windows.Forms.Label
Me.Label2 = New System.Windows.Forms.Label
Me.SuspendLayout()
'
'tvRO
'
Me.tvRO.ImageIndex = -1
Me.tvRO.Location = New System.Drawing.Point(152, 48)
Me.tvRO.Name = "tvRO"
Me.tvRO.Nodes.AddRange(New System.Windows.Forms.TreeNode() {New
System.Windows.Forms.TreeNode("RORoot", New
System.Windows.Forms.TreeNode() {New
System.Windows.Forms.TreeNode("ROChild")})})
Me.tvRO.SelectedImageIndex = -1
Me.tvRO.Size = New System.Drawing.Size(96, 176)
Me.tvRO.TabIndex = 0
'
'tvRW
'
Me.tvRW.AllowDrop = True
Me.tvRW.ImageIndex = -1
Me.tvRW.Location = New System.Drawing.Point(16, 48)
Me.tvRW.Name = "tvRW"
Me.tvRW.Nodes.AddRange(New System.Windows.Forms.TreeNode() {New
System.Windows.Forms.TreeNode("RWRoot", New
System.Windows.Forms.TreeNode() {New
System.Windows.Forms.TreeNode("RWChild")})})
Me.tvRW.SelectedImageIndex = -1
Me.tvRW.Size = New System.Drawing.Size(96, 176)
Me.tvRW.TabIndex = 1
'
'Label1
'
Me.Label1.Location = New System.Drawing.Point(164, 16)
Me.Label1.Name = "Label1"
Me.Label1.Size = New System.Drawing.Size(72, 16)
Me.Label1.TabIndex = 2
Me.Label1.Text = "RO"
Me.Label1.TextAlign = System.Drawing.ContentAlignment.TopCenter
'
'Label2
'
Me.Label2.Location = New System.Drawing.Point(32, 16)
Me.Label2.Name = "Label2"
Me.Label2.Size = New System.Drawing.Size(72, 16)
Me.Label2.TabIndex = 3
Me.Label2.Text = "RW"
Me.Label2.TextAlign = System.Drawing.ContentAlignment.TopCenter
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(6, 15)
Me.ClientSize = New System.Drawing.Size(292, 267)
Me.Controls.Add(Me.Label2)
Me.Controls.Add(Me.Label1)
Me.Controls.Add(Me.tvRW)
Me.Controls.Add(Me.tvRO)
Me.Name = "Form1"
Me.Text = "Form1"
Me.ResumeLayout(False)

End Sub

#End Region

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
tvRO.Nodes(0).Expand()
tvRW.Nodes(0).Expand()
End Sub

Private Sub tvRO_ItemDrag(ByVal sender As Object, ByVal e As
System.Windows.Forms.ItemDragEventArgs) Handles tvRO.ItemDrag
Debug.WriteLine("Doing RO DoDragDrop")
DoDragDrop(tvRO.SelectedNode, DragDropEffects.Copy)
End Sub

Private Sub tvRW_DragEnter(ByVal sender As Object, ByVal e As
System.Windows.Forms.DragEventArgs) Handles tvRW.DragEnter
Debug.WriteLine("Enter RW DragEnter")
e.Effect = e.AllowedEffect
End Sub

Private Sub tvRW_DragOver(ByVal sender As Object, ByVal e As
System.Windows.Forms.DragEventArgs) Handles tvRW.DragOver
Debug.WriteLine("Enter DragOver")
Dim tn As TreeNode
Dim ptClient As System.Drawing.Point = tvRW.PointToClient(New
System.Drawing.Point(e.X, e.Y))
tn = tvRW.GetNodeAt(ptClient)
If Not IsNothing(tn) Then
e.Effect = e.AllowedEffect
Else
e.Effect = DragDropEffects.None
End If
End Sub

Private Sub tvRW_DragLeave(ByVal sender As Object, ByVal e As
System.EventArgs) Handles tvRW.DragLeave
Debug.WriteLine("Enter RW DragLeave")
End Sub

Private Sub tvRW_DragDrop(ByVal sender As Object, ByVal e As
System.Windows.Forms.DragEventArgs) Handles tvRW.DragDrop
Debug.WriteLine("Enter RW DragDrop")
Dim tn As TreeNode
Dim ptClient As System.Drawing.Point = tvRW.PointToClient(New
System.Drawing.Point(e.X, e.Y))
tn = tvRW.GetNodeAt(ptClient)
If Not IsNothing(tn) Then
Debug.WriteLine("Adding Node " &
e.Data.GetData("System.Windows.Forms.TreeNode").text)
tn.Nodes.Add(New
TreeNode(e.Data.GetData("System.Windows.Forms.TreeNode").text))
tn.Expand()
End If
End Sub
End Class
 
B

Barry Moon

Hi Jim

Thanks a lot for your reply; that code certainly seems to do the trick
- it looks like it's the "AllowedEffect" property that I need to be
using.

Thanks very much again.

Cheers,
Barry
 

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