i've looked around quite while trying find working solution resorting asking question:
i have datagridview in dialog form in app, want contextmenu appear on right-click of cell.
i have right-click , contextmenu appearing fine, no matter solution on stackexchange attempt, offset quite alot.
is form and/or it's parent? or stupidly missing here?
thanks jamie
form.cs
private void datagridcontents_cellmousedown(object sender, datagridviewcellmouseeventargs e) { if (e.button == mousebuttons.right) { if (e.rowindex > -1 && e.columnindex > -1) { debug.writeline("cell right clicked!"); datagridviewcell cell = (sender datagridview)[e.columnindex, e.rowindex]; contextcell.show(cell.datagridview, pointtoclient(cursor.position)); if (!cell.selected) { cell.datagridview.clearselection(); cell.datagridview.currentcell = cell; cell.selected = true; } } } } edit
sorry, have tried:
new point(e.x, e.y)new point(e.location.x, e.location.y)new point(mouseposition.x, mouseposition.y)pointtoclient(e.x, e.y)new point(cursor.position.x, cursor.position.y)control.mousepositioncursor.position
and few others.
edit 2
this mean offset -- of solutions cause offset vary in magnitudes (some far away etc) -- offset actual cursor.
edit 3
my contextcell new contextmenu()
option 1: simple solution showing context menu rows assigning context menu rowtemplate.contextmenustrip property of datagridview:
datagridview1.rowtemplate.contextmenustrip = contextmenustrip1; option 2: if want select cell before showing contextmenustrip, it's enough handle cellcontextmenustripneeded event:
private void datagridview1_cellcontextmenustripneeded(object sender, datagridviewcellcontextmenustripneededeventargs e) { if (e.rowindex > -1 && e.columnindex > -1) { datagridview1.currentcell = datagridview1.rows[e.rowindex].cells[e.columnindex]; e.contextmenustrip = contextmenustrip1; } } what mistake?
you calculating mouse position on datagridview in wrong way. using pointtoclient means this.pointtoclient, while need use method of datagridview, example datagridview1.pointtoclient:
mycontextmenu.show(datagridview1,datagridview1.pointtoclient(cursor.position)); just information can show contextmenu using code , there no need use contextmenustrip.
but recommend use contextmenustrip.

Comments
Post a Comment