{"id":121,"date":"2014-02-09T16:41:14","date_gmt":"2014-02-09T21:41:14","guid":{"rendered":"http:\/\/www.johnfperry.com\/?p=121"},"modified":"2020-10-19T11:25:09","modified_gmt":"2020-10-19T15:25:09","slug":"vb-code","status":"publish","type":"post","link":"https:\/\/johnfperry.com\/?p=121","title":{"rendered":"VB code"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">DragDrop ideas<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><pre class=\"brush: vb; title: ; notranslate\" title=\"\">\nprivate void treeView_DragDrop(object sender, System.Windows.Forms.DragEventArgs e)\n    {\n        TreeNode NewNode;\n\n        if(e.Data.GetDataPresent(\"System.Windows.Forms.TreeNode\", false))\n        {\n            NewNode = (TreeNode)e.Data.GetData(\"System.Windows.Forms.TreeNode\");\n            if (!(sender as TreeView).Nodes.Contains(NewNode))\/\/Edit: add this if you don't want to add the same one again.\n            {\n                 (sender as TreeView).Nodes.Add((TreeNode) NewNode.Clone());\n                 NewNode.Remove(); \/\/Edit: add this if you want to remove original one.\n            }\n        }\n    }\n<\/pre><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Dragging Files<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\"><a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/aa289508(v=vs.71).aspx\">Drag and drop<\/a> is used pervasively in Windows for moving or copying files. Windows Explorer fully supports drag and drop, and for many users this is the preferred method of working with files. In addition, many users are accustomed to dropping files onto an application to open them \u2014 for example, <em>dragging and dropping a .doc file onto Microsoft Word.<\/em><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In this example drag and drop is used to populate a&nbsp;<b>ListBox<\/b>&nbsp;control with a list of files dragged from Windows Explorer.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><b>To enable drag and drop for a file<\/b><\/p>\n\n\n\n<ol class=\"wp-block-list\" type=\"1\"><li>Add a&nbsp;<b>ListBox<\/b>&nbsp;control to a form and set its&nbsp;<b>AllowDrop<\/b>&nbsp;property to&nbsp;<b>True<\/b>.<\/li><li>Add the following code:\n<div id=\"code-snippet-3\">\n<div>\n<div id=\"CodeSnippetContainerCode_0c44cefb-8c89-4a7f-a1a4-5435eafd0c90\" dir=\"ltr\">\n<div>\n<pre><pre class=\"brush: vb; title: ; notranslate\" title=\"\">Private Sub ListBox1_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles ListBox1.DragEnter\n    If e.Data.GetDataPresent(DataFormats.FileDrop) Then\n        e.Effect = DragDropEffects.All\n    End If\nEnd Sub\n\n\nPrivate Sub ListBox1_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles ListBox1.DragDrop\n    If e.Data.GetDataPresent(DataFormats.FileDrop) Then\n        Dim MyFiles() As String\n        Dim i As Integer\n\n        ' Assign the files to an array.\n        MyFiles = e.Data.GetData(DataFormats.FileDrop)\n        ' Loop through the array and add the files to the list.\n        For i = 0 To MyFiles.Length - 1\n            ListBox1.Items.Add(MyFiles(i))\n        Next\n    End If\nEnd Sub<\/pre><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/li><\/ol>\n\n\n\n<p class=\"wp-block-paragraph\">You may notice that in the&nbsp;<b>DragEnter<\/b>&nbsp;event the&nbsp;<b>Effect<\/b>&nbsp;is set to&nbsp;<b>DragDropEffects.All<\/b>. Because the files themselves are not actually being moved or copied, it does not really matter which<b>AllowedEffects<\/b>&nbsp;were set by the source, so specifying&nbsp;<b>All<\/b>&nbsp;means that dropping is enabled for any&nbsp;<b>FileDrop<\/b>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In the above example the&nbsp;<b>FileDrop<\/b>&nbsp;format contains the full path for each file being dropped. Rather than populating a list, you could just as easily perform other operations on the files \u2014 for example, opening them in MDI (multiple-document interface) document windows.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p class=\"wp-block-paragraph\"><a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/aa289508(v=vs.71).aspx#vbtchimpdragdropanchor3\">What&#8217;s Changed from Visual Basic 6.0<\/a><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n","protected":false},"excerpt":{"rendered":"<p>DragDrop ideas private void treeView_DragDrop(object sender, System.Windows.Forms.DragEventArgs e) { TreeNode NewNode; if(e.Data.GetDataPresent(&#8220;System.Windows.Forms.TreeNode&#8221;, false)) { NewNode = (TreeNode)e.Data.GetData(&#8220;System.Windows.Forms.TreeNode&#8221;); if (!(sender as&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_feature_clip_id":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_post_was_ever_published":false},"categories":[3,13],"tags":[],"class_list":["post-121","post","type-post","status-publish","format-standard","hentry","category-coding","category-vb"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/johnfperry.com\/index.php?rest_route=\/wp\/v2\/posts\/121","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/johnfperry.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/johnfperry.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/johnfperry.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/johnfperry.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=121"}],"version-history":[{"count":1,"href":"https:\/\/johnfperry.com\/index.php?rest_route=\/wp\/v2\/posts\/121\/revisions"}],"predecessor-version":[{"id":644,"href":"https:\/\/johnfperry.com\/index.php?rest_route=\/wp\/v2\/posts\/121\/revisions\/644"}],"wp:attachment":[{"href":"https:\/\/johnfperry.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=121"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/johnfperry.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=121"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/johnfperry.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=121"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}