c# - Why does my WinForms context menu not appear where the mouse is? -


in application have datagridview meant configuring options. idea can enter whatever text want in first column, if right click give explicitly supported values. need textbox rather dropdown list because need support editing invalid (or old) configurations.

what want user right click in field name column , have context menu valid based on type of configuration is. therefore, coded following event

    private void grvfielddata_cellmouseclick(object sender, datagridviewcellmouseeventargs e)     {         // if right click on field name column, create context menu          //   recognized options field         if (e.button == mousebuttons.right && grvfielddata.columns[e.columnindex].name == "clmfieldname")         {             contextmenu menu = new contextmenu();              if (_supporteddatagrids.containskey((cmbdatagrid.selecteditem datagridfieldlist).gridname))             {                 // loop through fields , add them context menu                 list<string> fields = _supporteddatagrids[((cmbdatagrid.selecteditem datagridfieldlist).gridname)];                 fields.sort();                  foreach (string field in fields)                     menu.menuitems.add(new menuitem(field));                  // make sure there @ least 1 field before displaying context menu                 if (menu.menuitems.count > 0)                     menu.show(this, e.location, leftrightalignment.right);             }         }     } 

this works "correctly", context menu appearing @ top of form, not mouse pointer is. if change show() call use datagridview instead of form, have same issue instead appears @ top-left hand corner of grid, not mouse is.

oddly enough, if change event mouseclick event (instead of cellmouseclick event) works , context menu appears mouse pointer is. problem option user might not right clicking on cell selected, means when click on menu item, selected cell changed , not cell right clicked on.

does have hints why context menus created cellmouseclick not showing @ correct spot?

 menu.show(this, e.location, leftrightalignment.right); 

the 2nd argument mouse location, relative cell's upper left corner. programmed, make offset relative this, form, make menu appear in upper left corner of form. use dgv 1st argument doesn't work either, in upper left corner of grid.

a couple of ways fix this, easy way:

 point pos = this.pointtoclient(cursor.position);  menu.show(this, pos, leftrightalignment.right); 

you can arbitrarily replace this grvfielddata.


Comments

Popular posts from this blog

linux - Mailx and Gmail nss config dir -

c# - Is it possible to remove an existing registration from Autofac container builder? -

php - Mysql PK and FK char(36) vs int(10) -