Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. drag and drop problem... vb .net

drag and drop problem... vb .net

Scheduled Pinned Locked Moved Visual Basic
csharphelptutorialquestion
12 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    daxfrost
    wrote on last edited by
    #1

    Please can someone tell me how to drag a file from my explorer window on my form and load the file into a rich text box that i have created at runtime? thank you, in advance

    P S 2 Replies Last reply
    0
    • D daxfrost

      Please can someone tell me how to drag a file from my explorer window on my form and load the file into a rich text box that i have created at runtime? thank you, in advance

      P Offline
      P Offline
      PandemoniumPasha
      wrote on last edited by
      #2

      hi, to do this, you need to set the allowdrop porperty of the control to true. then you need to write two event-handlers to handle the events fired when you do drag & drop operation. below is a code sample in C# but you can easily convert it to VB //you need to set the property and assign eventhandlers this.richtextbox1.Allow = true; this.richtextbox1.DragDrop += new DragEventHandler(richtextbox1_DragDrop); this.richtextbox1.DragEnter += new DragEventHandler(richtextbox1_DragEnter); //then write the event handlers private void richtextbox1_DragEnter(object sender, DragEventArgs e) { if(e.Data.GetDataPresent(DataFormats.FileDrop)) { e.Effect = DragDropEffects.Copy; } else { e.Effect = DragDropEffects.None; } } private void richtextbox1_DragDrop(object sender, DragEventArgs e) { if(e.Data.GetDataPresent(DataFormats.FileDrop)) { string[] file = (string[])e.Data.GetData(DataFormats.FileDrop); fileName = file[0]; //Now that you have the file name of the file, you can read it and display its contents } } hope this helps. regards :)

      1 Reply Last reply
      0
      • D daxfrost

        Please can someone tell me how to drag a file from my explorer window on my form and load the file into a rich text box that i have created at runtime? thank you, in advance

        S Offline
        S Offline
        Sonia Gupta
        wrote on last edited by
        #3

        on formload textbox1.dragdrop=true Private Sub TextBox1_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles TextBox1.DragDrop If (System.IO.File.Exists(CType(e.Data.GetData(DataFormats.FileDrop), Array).GetValue(0).ToString()) = True) Then If (System.IO.Path.GetExtension(CType(e.Data.GetData(DataFormats.FileDrop), Array).GetValue(0).ToString()) = ".txt") Then TextBox1.Text = System.IO.File.ReadAllText(CType(e.Data.GetData(DataFormats.FileDrop), Array).GetValue(0).ToString()) Else MessageBox.Show("select the .txt extention file") End If End If End Sub Private Sub TextBox1_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles TextBox1.DragEnter If (e.Data.GetDataPresent(DataFormats.FileDrop)) Then e.Effect = DragDropEffects.Copy End If End Sub Hope this may help You.:)

        D 1 Reply Last reply
        0
        • S Sonia Gupta

          on formload textbox1.dragdrop=true Private Sub TextBox1_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles TextBox1.DragDrop If (System.IO.File.Exists(CType(e.Data.GetData(DataFormats.FileDrop), Array).GetValue(0).ToString()) = True) Then If (System.IO.Path.GetExtension(CType(e.Data.GetData(DataFormats.FileDrop), Array).GetValue(0).ToString()) = ".txt") Then TextBox1.Text = System.IO.File.ReadAllText(CType(e.Data.GetData(DataFormats.FileDrop), Array).GetValue(0).ToString()) Else MessageBox.Show("select the .txt extention file") End If End If End Sub Private Sub TextBox1_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles TextBox1.DragEnter If (e.Data.GetDataPresent(DataFormats.FileDrop)) Then e.Effect = DragDropEffects.Copy End If End Sub Hope this may help You.:)

          D Offline
          D Offline
          daxfrost
          wrote on last edited by
          #4

          Thanks for the code... technically that should work, but I create my rich text box at runtime, so i cant call its events, or atleast... i am unsure of how to! :confused: please help? thanx guys!

          S D 2 Replies Last reply
          0
          • D daxfrost

            Thanks for the code... technically that should work, but I create my rich text box at runtime, so i cant call its events, or atleast... i am unsure of how to! :confused: please help? thanx guys!

            S Offline
            S Offline
            Sonia Gupta
            wrote on last edited by
            #5

            DaxBooysen wrote:

            i am unsure of how to! please help?

            replace textbox with Richedittextbox Actually i tried it with textbox.It is very simple. replace textbox with richedittextbox

            D 1 Reply Last reply
            0
            • S Sonia Gupta

              DaxBooysen wrote:

              i am unsure of how to! please help?

              replace textbox with Richedittextbox Actually i tried it with textbox.It is very simple. replace textbox with richedittextbox

              D Offline
              D Offline
              daxfrost
              wrote on last edited by
              #6

              I have no problem with that, its the fact that i create it at RUNTIME... i dont place it on my form at design time, so when i try make an event for a text box that has not been created yet i cant! know what i mean? thanx again!:confused:

              S 1 Reply Last reply
              0
              • D daxfrost

                I have no problem with that, its the fact that i create it at RUNTIME... i dont place it on my form at design time, so when i try make an event for a text box that has not been created yet i cant! know what i mean? thanx again!:confused:

                S Offline
                S Offline
                Sonia Gupta
                wrote on last edited by
                #7

                ok What code u r writing?

                D 1 Reply Last reply
                0
                • S Sonia Gupta

                  ok What code u r writing?

                  D Offline
                  D Offline
                  daxfrost
                  wrote on last edited by
                  #8

                  i create a rtf box in many places and set it as a child control of a tab control... but i call it up like this when i want to change its values... Dim rtf As New RichTextBox() rtf = TABS.TabPages(TABS.SelectedIndex).Controls.Item(0) and so you see... since its created at runtime... how do i call its events? thanx, dax :)

                  S C 2 Replies Last reply
                  0
                  • D daxfrost

                    i create a rtf box in many places and set it as a child control of a tab control... but i call it up like this when i want to change its values... Dim rtf As New RichTextBox() rtf = TABS.TabPages(TABS.SelectedIndex).Controls.Item(0) and so you see... since its created at runtime... how do i call its events? thanx, dax :)

                    S Offline
                    S Offline
                    Sonia Gupta
                    wrote on last edited by
                    #9

                    then write the code on each control which is provided to u.

                    1 Reply Last reply
                    0
                    • D daxfrost

                      i create a rtf box in many places and set it as a child control of a tab control... but i call it up like this when i want to change its values... Dim rtf As New RichTextBox() rtf = TABS.TabPages(TABS.SelectedIndex).Controls.Item(0) and so you see... since its created at runtime... how do i call its events? thanx, dax :)

                      C Offline
                      C Offline
                      Christian Graus
                      wrote on last edited by
                      #10

                      DaxBooysen wrote:

                      Dim rtf As New RichTextBox() rtf = TABS.TabPages(TABS.SelectedIndex).Controls.Item(0)

                      Doesn't this create a control, then discard it for one that's already within the tab page ? Either way, you can attach events to a control in code, at least you can in C#.

                      Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )

                      1 Reply Last reply
                      0
                      • D daxfrost

                        Thanks for the code... technically that should work, but I create my rich text box at runtime, so i cant call its events, or atleast... i am unsure of how to! :confused: please help? thanx guys!

                        D Offline
                        D Offline
                        Dave Kreskowiak
                        wrote on last edited by
                        #11

                        If you create a control at runtime, you have to wire up the events yourself at runtime, just like the designer generated code does when you drop a control on the form. Lookup AddHandler and RemoveHandler on MSDN and it'll show you how to do this...

                        A guide to posting questions on CodeProject[^]
                        Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
                             2006, 2007

                        D 1 Reply Last reply
                        0
                        • D Dave Kreskowiak

                          If you create a control at runtime, you have to wire up the events yourself at runtime, just like the designer generated code does when you drop a control on the form. Lookup AddHandler and RemoveHandler on MSDN and it'll show you how to do this...

                          A guide to posting questions on CodeProject[^]
                          Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
                               2006, 2007

                          D Offline
                          D Offline
                          daxfrost
                          wrote on last edited by
                          #12

                          THANX!!! you rock man! that worked! :-D

                          1 Reply Last reply
                          0
                          Reply
                          • Reply as topic
                          Log in to reply
                          • Oldest to Newest
                          • Newest to Oldest
                          • Most Votes


                          • Login

                          • Don't have an account? Register

                          • Login or register to search.
                          • First post
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • World
                          • Users
                          • Groups