C# drag n drop, start drag operation in mouse_down ? conflicts with click










0















I have a C# WinForms application with a GridView on a Form wich shows records from the database containing blobs (files PDF/JPG/etc.) stored in a database.



I can doubleclick on a row in the grid, to write the blob to disk and open the file.
I can single-click on rows to select one or more rows (using ctrl+shift)
I can drag files onto the grid to add the files as rows to the grid (and database)



Now I want the user to be able to drag one or more rows from the grid to, for instance, the desktop or a mailclient but cannot figure out in what event to start the 'drag' operation.



When a user selects one or more file he/she does that using the left-mousebutton, dragging uses the same left-mousebutton, both events trigger the mouse-down event. How does one determine what the user is about to do?



I have tried starting the drag operation in the mouse-down event, but that doesn't work if I want to select multiple rows, every time I click on a row a drag operation starts...



how is that handled in the windows explorer for instance?
How does one detect what the user is trying to do?










share|improve this question
























  • In MouseDown you should make a note of the location, and then while you get MouseMove without a MouseUp, you can calculate the distance moved. If the mouse moves more than a certain distance (say 12 pixels in either X or Y) then start a drag.

    – Matthew Watson
    Jun 23 '13 at 20:27












  • stackoverflow.com/questions/15322342/… .. hope helps

    – matzone
    Jun 24 '13 at 1:27











  • Matthew is right, but use SystemInformation.DragSize Property (msdn.microsoft.com/en-us/library/…) instead of 12 pixels

    – Viacheslav Ivanov
    Jun 25 '13 at 9:39















0















I have a C# WinForms application with a GridView on a Form wich shows records from the database containing blobs (files PDF/JPG/etc.) stored in a database.



I can doubleclick on a row in the grid, to write the blob to disk and open the file.
I can single-click on rows to select one or more rows (using ctrl+shift)
I can drag files onto the grid to add the files as rows to the grid (and database)



Now I want the user to be able to drag one or more rows from the grid to, for instance, the desktop or a mailclient but cannot figure out in what event to start the 'drag' operation.



When a user selects one or more file he/she does that using the left-mousebutton, dragging uses the same left-mousebutton, both events trigger the mouse-down event. How does one determine what the user is about to do?



I have tried starting the drag operation in the mouse-down event, but that doesn't work if I want to select multiple rows, every time I click on a row a drag operation starts...



how is that handled in the windows explorer for instance?
How does one detect what the user is trying to do?










share|improve this question
























  • In MouseDown you should make a note of the location, and then while you get MouseMove without a MouseUp, you can calculate the distance moved. If the mouse moves more than a certain distance (say 12 pixels in either X or Y) then start a drag.

    – Matthew Watson
    Jun 23 '13 at 20:27












  • stackoverflow.com/questions/15322342/… .. hope helps

    – matzone
    Jun 24 '13 at 1:27











  • Matthew is right, but use SystemInformation.DragSize Property (msdn.microsoft.com/en-us/library/…) instead of 12 pixels

    – Viacheslav Ivanov
    Jun 25 '13 at 9:39













0












0








0


1






I have a C# WinForms application with a GridView on a Form wich shows records from the database containing blobs (files PDF/JPG/etc.) stored in a database.



I can doubleclick on a row in the grid, to write the blob to disk and open the file.
I can single-click on rows to select one or more rows (using ctrl+shift)
I can drag files onto the grid to add the files as rows to the grid (and database)



Now I want the user to be able to drag one or more rows from the grid to, for instance, the desktop or a mailclient but cannot figure out in what event to start the 'drag' operation.



When a user selects one or more file he/she does that using the left-mousebutton, dragging uses the same left-mousebutton, both events trigger the mouse-down event. How does one determine what the user is about to do?



I have tried starting the drag operation in the mouse-down event, but that doesn't work if I want to select multiple rows, every time I click on a row a drag operation starts...



how is that handled in the windows explorer for instance?
How does one detect what the user is trying to do?










share|improve this question
















I have a C# WinForms application with a GridView on a Form wich shows records from the database containing blobs (files PDF/JPG/etc.) stored in a database.



I can doubleclick on a row in the grid, to write the blob to disk and open the file.
I can single-click on rows to select one or more rows (using ctrl+shift)
I can drag files onto the grid to add the files as rows to the grid (and database)



Now I want the user to be able to drag one or more rows from the grid to, for instance, the desktop or a mailclient but cannot figure out in what event to start the 'drag' operation.



When a user selects one or more file he/she does that using the left-mousebutton, dragging uses the same left-mousebutton, both events trigger the mouse-down event. How does one determine what the user is about to do?



I have tried starting the drag operation in the mouse-down event, but that doesn't work if I want to select multiple rows, every time I click on a row a drag operation starts...



how is that handled in the windows explorer for instance?
How does one detect what the user is trying to do?







c# winforms drag-and-drop






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 25 '14 at 17:13









Kara

3,956104252




3,956104252










asked Jun 23 '13 at 20:17









JurjenJurjen

199421




199421












  • In MouseDown you should make a note of the location, and then while you get MouseMove without a MouseUp, you can calculate the distance moved. If the mouse moves more than a certain distance (say 12 pixels in either X or Y) then start a drag.

    – Matthew Watson
    Jun 23 '13 at 20:27












  • stackoverflow.com/questions/15322342/… .. hope helps

    – matzone
    Jun 24 '13 at 1:27











  • Matthew is right, but use SystemInformation.DragSize Property (msdn.microsoft.com/en-us/library/…) instead of 12 pixels

    – Viacheslav Ivanov
    Jun 25 '13 at 9:39

















  • In MouseDown you should make a note of the location, and then while you get MouseMove without a MouseUp, you can calculate the distance moved. If the mouse moves more than a certain distance (say 12 pixels in either X or Y) then start a drag.

    – Matthew Watson
    Jun 23 '13 at 20:27












  • stackoverflow.com/questions/15322342/… .. hope helps

    – matzone
    Jun 24 '13 at 1:27











  • Matthew is right, but use SystemInformation.DragSize Property (msdn.microsoft.com/en-us/library/…) instead of 12 pixels

    – Viacheslav Ivanov
    Jun 25 '13 at 9:39
















In MouseDown you should make a note of the location, and then while you get MouseMove without a MouseUp, you can calculate the distance moved. If the mouse moves more than a certain distance (say 12 pixels in either X or Y) then start a drag.

– Matthew Watson
Jun 23 '13 at 20:27






In MouseDown you should make a note of the location, and then while you get MouseMove without a MouseUp, you can calculate the distance moved. If the mouse moves more than a certain distance (say 12 pixels in either X or Y) then start a drag.

– Matthew Watson
Jun 23 '13 at 20:27














stackoverflow.com/questions/15322342/… .. hope helps

– matzone
Jun 24 '13 at 1:27





stackoverflow.com/questions/15322342/… .. hope helps

– matzone
Jun 24 '13 at 1:27













Matthew is right, but use SystemInformation.DragSize Property (msdn.microsoft.com/en-us/library/…) instead of 12 pixels

– Viacheslav Ivanov
Jun 25 '13 at 9:39





Matthew is right, but use SystemInformation.DragSize Property (msdn.microsoft.com/en-us/library/…) instead of 12 pixels

– Viacheslav Ivanov
Jun 25 '13 at 9:39












3 Answers
3






active

oldest

votes


















6














I have got it working now. However I did not use a timer as suggested.



In Mouse-Down I set a flag and store X,Y point, then in mouse-up I reset the flag and in Mouse-move I calculate movement based on stored X,Y poiny, when movement is more that 10pixels in X or Y direction I start drag-operation.



Here's the code.



'



 private bool DraggingFromGrid = false;
private System.Drawing.Point DraggingStartPoint = new System.Drawing.Point( );

void GridControlBrowser_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)

if (e.Button == System.Windows.Forms.MouseButtons.Left)

DraggingFromGrid = true;
DraggingStartPoint = new System.Drawing.Point(e.X, e.Y);



void GridControlBrowser_MouseUp(object sender, MouseEventArgs e)

if (DraggingFromGrid)

DraggingFromGrid = false;



void GridControlBrowser_MouseMove(object sender, MouseEventArgs e)

if (DraggingFromGrid)

if (System.Math.Abs(e.X - DraggingStartPoint.X) > 10


private void StartDragging()

DraggingFromGrid = false;

// create files
var _criteria = this.GetSelectionFromGrid();
var _files = new List<string>();

... retrieve filenames and store in _files List ...

var _data = new DataObject(DataFormats.FileDrop, _files.ToArray());

DoDragDrop(_data, DragDropEffects.Copy);



'






share|improve this answer






























    0














    there is more then one way to do that. I strongly suggest you try some of them until you decide what's best for you. for example, you can use the mouse_down event to start the drag and drop by using a timer. if the user is clicking the mouse more then 0.5 seconds, u start the drag. the mouse_up event kill the drag/dropping.
    another way is making the drag and drop only with some key pressed while clicking the mouse.






    share|improve this answer






























      0














      I had a similar issue in that adding dragging to a form was interfering with existing double click functionality. I found a very simple solution:



      void GridControlBrowser_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)

      if (e.Clicks != 2)

      StartDragging();




      This cancels dragging if there is a double click so that double clicking continues to work.






      share|improve this answer






















        Your Answer






        StackExchange.ifUsing("editor", function ()
        StackExchange.using("externalEditor", function ()
        StackExchange.using("snippets", function ()
        StackExchange.snippets.init();
        );
        );
        , "code-snippets");

        StackExchange.ready(function()
        var channelOptions =
        tags: "".split(" "),
        id: "1"
        ;
        initTagRenderer("".split(" "), "".split(" "), channelOptions);

        StackExchange.using("externalEditor", function()
        // Have to fire editor after snippets, if snippets enabled
        if (StackExchange.settings.snippets.snippetsEnabled)
        StackExchange.using("snippets", function()
        createEditor();
        );

        else
        createEditor();

        );

        function createEditor()
        StackExchange.prepareEditor(
        heartbeatType: 'answer',
        autoActivateHeartbeat: false,
        convertImagesToLinks: true,
        noModals: true,
        showLowRepImageUploadWarning: true,
        reputationToPostImages: 10,
        bindNavPrevention: true,
        postfix: "",
        imageUploader:
        brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
        contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
        allowUrls: true
        ,
        onDemand: true,
        discardSelector: ".discard-answer"
        ,immediatelyShowMarkdownHelp:true
        );



        );













        draft saved

        draft discarded


















        StackExchange.ready(
        function ()
        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f17264910%2fc-sharp-drag-n-drop-start-drag-operation-in-mouse-down-conflicts-with-click%23new-answer', 'question_page');

        );

        Post as a guest















        Required, but never shown

























        3 Answers
        3






        active

        oldest

        votes








        3 Answers
        3






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes









        6














        I have got it working now. However I did not use a timer as suggested.



        In Mouse-Down I set a flag and store X,Y point, then in mouse-up I reset the flag and in Mouse-move I calculate movement based on stored X,Y poiny, when movement is more that 10pixels in X or Y direction I start drag-operation.



        Here's the code.



        '



         private bool DraggingFromGrid = false;
        private System.Drawing.Point DraggingStartPoint = new System.Drawing.Point( );

        void GridControlBrowser_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)

        if (e.Button == System.Windows.Forms.MouseButtons.Left)

        DraggingFromGrid = true;
        DraggingStartPoint = new System.Drawing.Point(e.X, e.Y);



        void GridControlBrowser_MouseUp(object sender, MouseEventArgs e)

        if (DraggingFromGrid)

        DraggingFromGrid = false;



        void GridControlBrowser_MouseMove(object sender, MouseEventArgs e)

        if (DraggingFromGrid)

        if (System.Math.Abs(e.X - DraggingStartPoint.X) > 10


        private void StartDragging()

        DraggingFromGrid = false;

        // create files
        var _criteria = this.GetSelectionFromGrid();
        var _files = new List<string>();

        ... retrieve filenames and store in _files List ...

        var _data = new DataObject(DataFormats.FileDrop, _files.ToArray());

        DoDragDrop(_data, DragDropEffects.Copy);



        '






        share|improve this answer



























          6














          I have got it working now. However I did not use a timer as suggested.



          In Mouse-Down I set a flag and store X,Y point, then in mouse-up I reset the flag and in Mouse-move I calculate movement based on stored X,Y poiny, when movement is more that 10pixels in X or Y direction I start drag-operation.



          Here's the code.



          '



           private bool DraggingFromGrid = false;
          private System.Drawing.Point DraggingStartPoint = new System.Drawing.Point( );

          void GridControlBrowser_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)

          if (e.Button == System.Windows.Forms.MouseButtons.Left)

          DraggingFromGrid = true;
          DraggingStartPoint = new System.Drawing.Point(e.X, e.Y);



          void GridControlBrowser_MouseUp(object sender, MouseEventArgs e)

          if (DraggingFromGrid)

          DraggingFromGrid = false;



          void GridControlBrowser_MouseMove(object sender, MouseEventArgs e)

          if (DraggingFromGrid)

          if (System.Math.Abs(e.X - DraggingStartPoint.X) > 10


          private void StartDragging()

          DraggingFromGrid = false;

          // create files
          var _criteria = this.GetSelectionFromGrid();
          var _files = new List<string>();

          ... retrieve filenames and store in _files List ...

          var _data = new DataObject(DataFormats.FileDrop, _files.ToArray());

          DoDragDrop(_data, DragDropEffects.Copy);



          '






          share|improve this answer

























            6












            6








            6







            I have got it working now. However I did not use a timer as suggested.



            In Mouse-Down I set a flag and store X,Y point, then in mouse-up I reset the flag and in Mouse-move I calculate movement based on stored X,Y poiny, when movement is more that 10pixels in X or Y direction I start drag-operation.



            Here's the code.



            '



             private bool DraggingFromGrid = false;
            private System.Drawing.Point DraggingStartPoint = new System.Drawing.Point( );

            void GridControlBrowser_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)

            if (e.Button == System.Windows.Forms.MouseButtons.Left)

            DraggingFromGrid = true;
            DraggingStartPoint = new System.Drawing.Point(e.X, e.Y);



            void GridControlBrowser_MouseUp(object sender, MouseEventArgs e)

            if (DraggingFromGrid)

            DraggingFromGrid = false;



            void GridControlBrowser_MouseMove(object sender, MouseEventArgs e)

            if (DraggingFromGrid)

            if (System.Math.Abs(e.X - DraggingStartPoint.X) > 10


            private void StartDragging()

            DraggingFromGrid = false;

            // create files
            var _criteria = this.GetSelectionFromGrid();
            var _files = new List<string>();

            ... retrieve filenames and store in _files List ...

            var _data = new DataObject(DataFormats.FileDrop, _files.ToArray());

            DoDragDrop(_data, DragDropEffects.Copy);



            '






            share|improve this answer













            I have got it working now. However I did not use a timer as suggested.



            In Mouse-Down I set a flag and store X,Y point, then in mouse-up I reset the flag and in Mouse-move I calculate movement based on stored X,Y poiny, when movement is more that 10pixels in X or Y direction I start drag-operation.



            Here's the code.



            '



             private bool DraggingFromGrid = false;
            private System.Drawing.Point DraggingStartPoint = new System.Drawing.Point( );

            void GridControlBrowser_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)

            if (e.Button == System.Windows.Forms.MouseButtons.Left)

            DraggingFromGrid = true;
            DraggingStartPoint = new System.Drawing.Point(e.X, e.Y);



            void GridControlBrowser_MouseUp(object sender, MouseEventArgs e)

            if (DraggingFromGrid)

            DraggingFromGrid = false;



            void GridControlBrowser_MouseMove(object sender, MouseEventArgs e)

            if (DraggingFromGrid)

            if (System.Math.Abs(e.X - DraggingStartPoint.X) > 10


            private void StartDragging()

            DraggingFromGrid = false;

            // create files
            var _criteria = this.GetSelectionFromGrid();
            var _files = new List<string>();

            ... retrieve filenames and store in _files List ...

            var _data = new DataObject(DataFormats.FileDrop, _files.ToArray());

            DoDragDrop(_data, DragDropEffects.Copy);



            '







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Jun 25 '13 at 9:15









            JurjenJurjen

            199421




            199421























                0














                there is more then one way to do that. I strongly suggest you try some of them until you decide what's best for you. for example, you can use the mouse_down event to start the drag and drop by using a timer. if the user is clicking the mouse more then 0.5 seconds, u start the drag. the mouse_up event kill the drag/dropping.
                another way is making the drag and drop only with some key pressed while clicking the mouse.






                share|improve this answer



























                  0














                  there is more then one way to do that. I strongly suggest you try some of them until you decide what's best for you. for example, you can use the mouse_down event to start the drag and drop by using a timer. if the user is clicking the mouse more then 0.5 seconds, u start the drag. the mouse_up event kill the drag/dropping.
                  another way is making the drag and drop only with some key pressed while clicking the mouse.






                  share|improve this answer

























                    0












                    0








                    0







                    there is more then one way to do that. I strongly suggest you try some of them until you decide what's best for you. for example, you can use the mouse_down event to start the drag and drop by using a timer. if the user is clicking the mouse more then 0.5 seconds, u start the drag. the mouse_up event kill the drag/dropping.
                    another way is making the drag and drop only with some key pressed while clicking the mouse.






                    share|improve this answer













                    there is more then one way to do that. I strongly suggest you try some of them until you decide what's best for you. for example, you can use the mouse_down event to start the drag and drop by using a timer. if the user is clicking the mouse more then 0.5 seconds, u start the drag. the mouse_up event kill the drag/dropping.
                    another way is making the drag and drop only with some key pressed while clicking the mouse.







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Jun 24 '13 at 5:47









                    No Idea For NameNo Idea For Name

                    9,466103256




                    9,466103256





















                        0














                        I had a similar issue in that adding dragging to a form was interfering with existing double click functionality. I found a very simple solution:



                        void GridControlBrowser_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)

                        if (e.Clicks != 2)

                        StartDragging();




                        This cancels dragging if there is a double click so that double clicking continues to work.






                        share|improve this answer



























                          0














                          I had a similar issue in that adding dragging to a form was interfering with existing double click functionality. I found a very simple solution:



                          void GridControlBrowser_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)

                          if (e.Clicks != 2)

                          StartDragging();




                          This cancels dragging if there is a double click so that double clicking continues to work.






                          share|improve this answer

























                            0












                            0








                            0







                            I had a similar issue in that adding dragging to a form was interfering with existing double click functionality. I found a very simple solution:



                            void GridControlBrowser_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)

                            if (e.Clicks != 2)

                            StartDragging();




                            This cancels dragging if there is a double click so that double clicking continues to work.






                            share|improve this answer













                            I had a similar issue in that adding dragging to a form was interfering with existing double click functionality. I found a very simple solution:



                            void GridControlBrowser_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)

                            if (e.Clicks != 2)

                            StartDragging();




                            This cancels dragging if there is a double click so that double clicking continues to work.







                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Nov 12 '18 at 19:54









                            sawmesawme

                            1614




                            1614



























                                draft saved

                                draft discarded
















































                                Thanks for contributing an answer to Stack Overflow!


                                • Please be sure to answer the question. Provide details and share your research!

                                But avoid


                                • Asking for help, clarification, or responding to other answers.

                                • Making statements based on opinion; back them up with references or personal experience.

                                To learn more, see our tips on writing great answers.




                                draft saved


                                draft discarded














                                StackExchange.ready(
                                function ()
                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f17264910%2fc-sharp-drag-n-drop-start-drag-operation-in-mouse-down-conflicts-with-click%23new-answer', 'question_page');

                                );

                                Post as a guest















                                Required, but never shown





















































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown

































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown







                                Popular posts from this blog

                                How to how show current date and time by default on contact form 7 in WordPress without taking input from user in datetimepicker

                                Syphilis

                                Darth Vader #20