Richtextbox with Drag and Drop

Ask questions about creating Graphical User Interfaces (GUI) in PowerShell and using WinForms controls.
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
Locked
User avatar
daniel kusnir
Posts: 39
Meble kuchenne na zamówienie - na wymiar - Wrocław
Joined: Thu May 25, 2017 8:22 am

Richtextbox with Drag and Drop

Post by daniel kusnir »

Hello

do you know how is it possible to wire drag& Drop with Richtextbox ?

According MS doc, https://docs.microsoft.com/en-us/dotnet ... l#see-also
its needed to add DragEnter and DragDrop event + set AllowDrop = True. However this does not work for me, even if i added DragOver event as well. It seems like the form does not detect events when added to code just like that

so at the beginning, i added allowdrop to the load
  1. $formDragDropFiles_Load = { $richtextbox1.AllowDrop = $true }
Then, i created 3 events ( because 2 isnt just enought ;) )
  1. $richTextBox1_DragDrop = [System.Windows.Forms.DragEventHandler]{
  2.  
  3.     #Event Argument: $_ = [System.Windows.Forms.DragEventArgs]
  4.  
  5.     #Process the dropped files
  6.  
  7.     $files = $_.Data.GetData([System.Windows.Forms.DataFormats]::FileDrop)
  8.  
  9.     if ($files)
  10.  
  11.     {
  12.  
  13.        foreach ($file in $files)
  14.  
  15.        { $richTextBox1.Text = Get-Content $file }
  16.  
  17.     }
  18.  
  19. }
  20.  
  21.  
  22.  
  23. $richTextBox1_DragOver = [System.Windows.Forms.DragEventHandler]{
  24.  
  25.     #Event Argument: $_ = [System.Windows.Forms.DragEventArgs]
  26.  
  27.     if ($_.Data.GetDataPresent([Windows.Forms.DataFormats]::FileDrop)){
  28.  
  29.         $_.Effect = 'Copy' }
  30.  
  31.     else{
  32.  
  33.         $_.Effect = 'None' }
  34.  
  35. }
  36.  
  37.  
  38.  
  39. $richTextBox1_DragEnter = [System.Windows.Forms.DragEventHandler]{
  40.  
  41.     if ($_.Data.GetDataPresent([windows.forms.dataformats]::Text)){
  42.  
  43.         $_.Effect = 'Copy' }
  44.  
  45.     else{
  46.  
  47.         $_.Effect = 'None' }
  48.  
  49. }
Attachments
formDragAndDrop.psf
Form Drag & Drop
(18.02 KiB) Downloaded 9 times

jvierra
Posts: 14358
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Richtextbox with Drag and Drop

Post by jvierra »

You have forgotten to add the events to the control.
Attachments
formDragAndDrop.psf
(19.3 KiB) Downloaded 10 times

User avatar
daniel kusnir
Posts: 39
Joined: Thu May 25, 2017 8:22 am

Re: Richtextbox with Drag and Drop

Post by daniel kusnir »

i see now, thank you very much

Locked