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. C#
  4. Using drag and drop in c#

Using drag and drop in c#

Scheduled Pinned Locked Moved C#
questioncsharphelp
11 Posts 2 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.
  • P Phanindra Kumar

    Hi, I am trying to drag files from windows explorer and openFileDialog box to the datagridview in c# 2005. I am able to drag files from windows explorer but i am not able to drag files from the openFileDialog box which invoked by my application through a button.Why it is happening like this and how can i fix this ? Thanks in Advance Phanindra...

    J Offline
    J Offline
    jjansen
    wrote on last edited by
    #2

    The OpenFileDialog is most probably a modal dialog. Your application will not react to mouse or keyboard input or, in this case, to darg-and-drop events. Is there a reason why you won't let the user select files in the OpenFileDialog and wait till he/she presses OK? -- modified at 8:26 Monday 3rd July, 2006 On second thought, what I wrote above is complete nonsense :rolleyes:. I just remembered I did something similar in one of my applications. You can drag files out of your OpenFileDialog just the same as out of, say, Explorer. You can use one and the same DragDrop event handler. Maybe you should post the code of your DragDrop event handler so we can take a look.

    P 1 Reply Last reply
    0
    • J jjansen

      The OpenFileDialog is most probably a modal dialog. Your application will not react to mouse or keyboard input or, in this case, to darg-and-drop events. Is there a reason why you won't let the user select files in the OpenFileDialog and wait till he/she presses OK? -- modified at 8:26 Monday 3rd July, 2006 On second thought, what I wrote above is complete nonsense :rolleyes:. I just remembered I did something similar in one of my applications. You can drag files out of your OpenFileDialog just the same as out of, say, Explorer. You can use one and the same DragDrop event handler. Maybe you should post the code of your DragDrop event handler so we can take a look.

      P Offline
      P Offline
      Phanindra Kumar
      wrote on last edited by
      #3

      Hai, Thanks for ur reply. Here is the code for the drag drop event handler for the datagridview. private void dataGridView1_DragEnter(object sender, DragEventArgs e) { e.Effect = DragDropEffects.Move; } private void dataGridView1_DragDrop(object sender, DragEventArgs e) { string[] files = (string[])e.Data.GetData(DataFormats.FileDrop); addFiles(files);//this method adds files to the datagridview } and i set datagridview property AllowDrop to true what else i should do. Phanindra...

      J 1 Reply Last reply
      0
      • P Phanindra Kumar

        Hai, Thanks for ur reply. Here is the code for the drag drop event handler for the datagridview. private void dataGridView1_DragEnter(object sender, DragEventArgs e) { e.Effect = DragDropEffects.Move; } private void dataGridView1_DragDrop(object sender, DragEventArgs e) { string[] files = (string[])e.Data.GetData(DataFormats.FileDrop); addFiles(files);//this method adds files to the datagridview } and i set datagridview property AllowDrop to true what else i should do. Phanindra...

        J Offline
        J Offline
        jjansen
        wrote on last edited by
        #4

        I can see nothing wrong with this code. And you say that this code DOES work when you drag from Windows Explorer and NOT from the OpenFileDialog? Have you placed a breakpoint in the method dataGridView1_DragDrop to see whether this method gets called at all. I just want to be certain the problem does not occur further on, for instance in "addFiles".

        P 1 Reply Last reply
        0
        • J jjansen

          I can see nothing wrong with this code. And you say that this code DOES work when you drag from Windows Explorer and NOT from the OpenFileDialog? Have you placed a breakpoint in the method dataGridView1_DragDrop to see whether this method gets called at all. I just want to be certain the problem does not occur further on, for instance in "addFiles".

          P Offline
          P Offline
          Phanindra Kumar
          wrote on last edited by
          #5

          Yeah, i placed a breakpoint in the dataGridView1_DragDrop method and tested it. But the method is not invoking when files are dragged and dropped from the openFileDialog box. Phanindra...

          J 1 Reply Last reply
          0
          • P Phanindra Kumar

            Yeah, i placed a breakpoint in the dataGridView1_DragDrop method and tested it. But the method is not invoking when files are dragged and dropped from the openFileDialog box. Phanindra...

            J Offline
            J Offline
            jjansen
            wrote on last edited by
            #6

            Does your DragEnter event handler get invoked? What kind of cursor do you get when you drag from the OpenFileDialog and what kind when you drag from Windows Explorer?

            P 1 Reply Last reply
            0
            • J jjansen

              Does your DragEnter event handler get invoked? What kind of cursor do you get when you drag from the OpenFileDialog and what kind when you drag from Windows Explorer?

              P Offline
              P Offline
              Phanindra Kumar
              wrote on last edited by
              #7

              no DragEnter event handler is not invoked for openFileDialog Box. I am getting a rectangle cursor with the pointer when dragged from windows explorer and default cursor like circle with a bar in it when dragged from openFileDialog box Phanindra...

              J 1 Reply Last reply
              0
              • P Phanindra Kumar

                no DragEnter event handler is not invoked for openFileDialog Box. I am getting a rectangle cursor with the pointer when dragged from windows explorer and default cursor like circle with a bar in it when dragged from openFileDialog box Phanindra...

                J Offline
                J Offline
                jjansen
                wrote on last edited by
                #8

                I think I know what's going on. When I tested my own application I noticed something strange. When I drag from Explorer, the individual controls in my application react to drag-drop events. However, when I drag from an OpenFileDialog the controls do not respond, but the Form they are on does! (I should mention that in my case the Form happens to have AllowDrop set to true.) What you can do is to add event handlers for DragEnter and DragDrop to your application Form. In these event handlers check which control the mouse cursor is on (e.g. using the position given in the DragEventArgs) and re-route the event to that control.

                P 1 Reply Last reply
                0
                • J jjansen

                  I think I know what's going on. When I tested my own application I noticed something strange. When I drag from Explorer, the individual controls in my application react to drag-drop events. However, when I drag from an OpenFileDialog the controls do not respond, but the Form they are on does! (I should mention that in my case the Form happens to have AllowDrop set to true.) What you can do is to add event handlers for DragEnter and DragDrop to your application Form. In these event handlers check which control the mouse cursor is on (e.g. using the position given in the DragEventArgs) and re-route the event to that control.

                  P Offline
                  P Offline
                  Phanindra Kumar
                  wrote on last edited by
                  #9

                  Actually the thing is here that i am using windows user control. But still DragEnter and DragDrop events are not invoked in my application. I made AllwDrop of user control to True. But no use. Phanindra...

                  J 1 Reply Last reply
                  0
                  • P Phanindra Kumar

                    Actually the thing is here that i am using windows user control. But still DragEnter and DragDrop events are not invoked in my application. I made AllwDrop of user control to True. But no use. Phanindra...

                    J Offline
                    J Offline
                    jjansen
                    wrote on last edited by
                    #10

                    It seems the application Form is the only "control" that is able to receive drag-drop events when an OpenFileDialog is open (with the OpenFileDialog belonging to that application). If you do not "own" this Form, for instance if you are developing a user control for distribution, then I'm afraid you can't do what you intent to do. Sorry :^).

                    P 1 Reply Last reply
                    0
                    • J jjansen

                      It seems the application Form is the only "control" that is able to receive drag-drop events when an OpenFileDialog is open (with the OpenFileDialog belonging to that application). If you do not "own" this Form, for instance if you are developing a user control for distribution, then I'm afraid you can't do what you intent to do. Sorry :^).

                      P Offline
                      P Offline
                      Phanindra Kumar
                      wrote on last edited by
                      #11

                      Thats OK. Thanks U. Thanks for ur cooperation:) Phanindra...

                      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