c# - How determine where a mouse is wherever it's clicked -


i'm making chess game in c#, , i'm using 64 pictureboxs display board. now, i'm trying figure out how determine user clicks allow user move pieces clicking different tiles/pictureboxes. i'm new c#, i'm trying detecting mouse whenever it's clicked. i've been using:

void mainformclick(object sender, eventargs e){             point mouseposition = picturebox1.pointtoclient(cursor.position); //picturebox1 in upper-left             int indexclicked = (mouseposition.x / tilesize) + (8 * (mouseposition.y / tilesize));             if (indexclicked >= 0 && indexclicked <= 63){                 //do code things?             }             this.text = "chess - " + indexclicked;         } 

i assume isn't working because user isn't clicking on form, on pictureboxes. i'd rather not make 64 picturebox*click methods, there other way should doing or method should using?

edit: looked @ how control under mouse cursor. code needs changed little better determine picturebox mouse on when it's clicked, real problem doesn't appear clicking on pictureboxes runs above method. i'm looking way run block of code whenever mouse clicked, regardless of control or part of form it's clicked on.

i use normal click event. pictureboxes have click event can bind to. i'm not sure how you're adding them, 64 of them i'd add them dynamically in loop. create each one, bind click event common handler. name each control in way makes easy figure out located. in handler object reference picturebox clicked , can figure out there.


Comments

Popular posts from this blog

dns - How To Use Custom Nameserver On Free Cloudflare? -

python - Pygame screen.blit not working -

c# - Web API response xml language -