Results 1 to 15 of 15

Thread: Form Drag And Drop From Computer Help!

  1. #1

    Thread Starter
    Member
    Join Date
    May 2012
    Posts
    38

    Exclamation Form Drag And Drop From Computer Help!

    I am in process of making a advanced note pad and have figured and most of the things i need out but i need help making a drag and drop system so i can take a .txt file from my computer and drag it on the program and drop it and have the program load the .txt file.
    Please help!

    P.S : it is a tabbed notepad
    Code for open:
    Code:
            Dim OPF As New OpenFileDialog
            Dim AllText As String = "", LineOfText As String = ""
            OPF.Filter = "Text Document(*.txt*)|*.txt|All Files|*.*"
            OPF.ShowDialog()
            UseWaitCursor = True
            Try
    
                FileOpen(1, OPF.FileName, OpenMode.Input)
                Do Until EOF(1)
                    LineOfText = LineInput(1)
                    AllText = AllText & LineOfText & vbCrLf
                Loop
                UseWaitCursor = True
                CType(TabControl1.SelectedTab.Controls.Item(0), RichTextBox).Text = AllText
                UseWaitCursor = False
            Catch
    
            Finally
                FileClose(1)
            End Try

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,481

    Re: Form Drag And Drop From Computer Help!

    here's an example with a standard textbox:

    vb Code:
    1. Public Class Form1
    2.  
    3.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    4.         TextBox1.AllowDrop = True
    5.     End Sub
    6.  
    7.     Private Sub TextBox1_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles TextBox1.DragDrop
    8.         If e.Data.GetDataPresent(DataFormats.FileDrop) Then
    9.             Dim file As String = DirectCast(e.Data.GetData(DataFormats.FileDrop), String()).First
    10.             TextBox1.Text = IO.File.ReadAllText(file)
    11.         End If
    12.     End Sub
    13.  
    14.     Private Sub TextBox1_DragOver(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles TextBox1.DragOver
    15.         If e.Data.GetDataPresent(DataFormats.FileDrop) Then
    16.             e.Effect = DragDropEffects.All
    17.         End If
    18.     End Sub
    19.  
    20. End Class

  3. #3

    Thread Starter
    Member
    Join Date
    May 2012
    Posts
    38

    Exclamation Re: Form Drag And Drop From Computer Help!

    I NEED it to be for richtextbox using tabcontrol. The above reply did not help me.
    Please help!

  4. #4
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,481

    Re: Form Drag And Drop From Computer Help!

    the same principles apply for a rtb as for a textbox. whether the rtb is in a tabpage or not is irrelevant

  5. #5

    Thread Starter
    Member
    Join Date
    May 2012
    Posts
    38

    Re: Form Drag And Drop From Computer Help!

    well,i am a beginner (kinda) (1 year) and i would love if you could show the actual code with the richtextbox in use. I would REALLLLLY appreciate it.

  6. #6
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,481

    Re: Form Drag And Drop From Computer Help!

    is it plain text or rtf?

  7. #7

    Thread Starter
    Member
    Join Date
    May 2012
    Posts
    38

    Re: Form Drag And Drop From Computer Help!

    Quote Originally Posted by .paul. View Post
    is it plain text or rtf?
    should be plain text.

  8. #8
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,481

    Re: Form Drag And Drop From Computer Help!

    vb Code:
    1. Public Class Form1
    2.  
    3.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    4.         For Each tp As TabPage In TabControl1.TabPages
    5.             Dim rtb As RichTextBox = DirectCast(tp.Controls(0), RichTextBox)
    6.             rtb.AllowDrop = True
    7.             AddHandler rtb.DragEnter, AddressOf rtb_DragEnter
    8.             AddHandler rtb.DragDrop, AddressOf rtb_DragDrop
    9.         Next
    10.     End Sub
    11.  
    12.     Private Sub rtb_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs)
    13.         Dim rtb As RichTextBox = DirectCast(sender, RichTextBox)
    14.         If e.Data.GetDataPresent(DataFormats.FileDrop) Then
    15.             Dim file As String = DirectCast(e.Data.GetData(DataFormats.FileDrop), String()).First
    16.             rtb.Text = IO.File.ReadAllText(file)
    17.             'for rtf, use this instead:
    18.             'rtb.LoadFile(file)
    19.         End If
    20.     End Sub
    21.  
    22.     Private Sub rtb_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs)
    23.         If e.Data.GetDataPresent(DataFormats.FileDrop) Then
    24.             e.Effect = DragDropEffects.All
    25.         End If
    26.     End Sub
    27.  
    28. End Class

  9. #9

    Thread Starter
    Member
    Join Date
    May 2012
    Posts
    38

    Re: Form Drag And Drop From Computer Help!

    Quote Originally Posted by .paul. View Post
    vb Code:
    1. Public Class Form1
    2.  
    3.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    4.         For Each tp As TabPage In TabControl1.TabPages
    5.             Dim rtb As RichTextBox = DirectCast(tp.Controls(0), RichTextBox)
    6.             rtb.AllowDrop = True
    7.             AddHandler rtb.DragEnter, AddressOf rtb_DragEnter
    8.             AddHandler rtb.DragDrop, AddressOf rtb_DragDrop
    9.         Next
    10.     End Sub
    11.  
    12.     Private Sub rtb_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs)
    13.         Dim rtb As RichTextBox = DirectCast(sender, RichTextBox)
    14.         If e.Data.GetDataPresent(DataFormats.FileDrop) Then
    15.             Dim file As String = DirectCast(e.Data.GetData(DataFormats.FileDrop), String()).First
    16.             rtb.Text = IO.File.ReadAllText(file)
    17.             'for rtf, use this instead:
    18.             'rtb.LoadFile(file)
    19.         End If
    20.     End Sub
    21.  
    22.     Private Sub rtb_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs)
    23.         If e.Data.GetDataPresent(DataFormats.FileDrop) Then
    24.             e.Effect = DragDropEffects.All
    25.         End If
    26.     End Sub
    27.  
    28. End Class
    I am getting errors here:
    Code:
                Dim file As String = DirectCast(e.Data.GetData(DataFormats.FileDrop), String()).First
    and here:
    Code:
     Dim rtb As RichTextBox = DirectCast(tp.Controls(0), RichTextBox)

    Please help!

  10. #10
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,481

    Re: Form Drag And Drop From Computer Help!

    which framework are you targetting?

    what controls do you have on your tabpages? if the control at index 0 is not a rtb, you'll get an error

  11. #11

    Thread Starter
    Member
    Join Date
    May 2012
    Posts
    38

    Re: Form Drag And Drop From Computer Help!

    Quote Originally Posted by .paul. View Post
    which framework are you targetting?

    what controls do you have on your tabpages? if the control at index 0 is not a rtb, you'll get an error
    Targeting: 4.0
    My control is: 0
    Errors: Yes

  12. #12
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,481

    Re: Form Drag And Drop From Computer Help!

    do you have a rtb at index 0 on every tabpage?
    for the other error, try this instead:

    vb Code:
    1. Dim file As String = DirectCast(e.Data.GetData(DataFormats.FileDrop), String())(0)

  13. #13
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,481

    Re: Form Drag And Drop From Computer Help!

    also it'd be helpful to provide the error messages

  14. #14

    Thread Starter
    Member
    Join Date
    May 2012
    Posts
    38

    Re: Form Drag And Drop From Computer Help!

    Quote Originally Posted by .paul. View Post
    also it'd be helpful to provide the error messages
    ok, i fixed every error now but when i have multiple tabs open and try to drag a .txt into any page besides page 1 it dosnt work.

  15. #15
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,481

    Re: Form Drag And Drop From Computer Help!

    it should drop only on the currently selected tab's rtb

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width