C# drag n drop, start drag operation in mouse_down ? conflicts with click
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
add a comment |
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
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
add a comment |
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
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
c# winforms drag-and-drop
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
add a comment |
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
add a comment |
3 Answers
3
active
oldest
votes
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);
'
add a comment |
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.
add a comment |
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.
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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);
'
add a comment |
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);
'
add a comment |
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);
'
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);
'
answered Jun 25 '13 at 9:15
JurjenJurjen
199421
199421
add a comment |
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
answered Jun 24 '13 at 5:47
No Idea For NameNo Idea For Name
9,466103256
9,466103256
add a comment |
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
answered Nov 12 '18 at 19:54
sawmesawme
1614
1614
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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