469,602 Members | 1,741 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,602 developers. It's quick & easy.

How to get location of a picturebox by clicking on it in C#?

3
Hi, i have a problem about picturebox control. if you may help me, i will be so happy. i have a picturebox named pic_map, and i added a button named customer_button, my wish is to add a new small picturebox on the map_picture and the new added picturebox should have click event to get the location of it when it is clicked.

i have written some code in C# but when clicking the small picturebox it just gets the location of the last added one, not the one that i clicked on .

here is my code:

pic_map: main picturebox

i global int, initial value=0
Expand|Select|Wrap|Line Numbers
  1. private void pic_map_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
  2. {
  3.        x=e.X;
  4.        y=e.Y;
  5.       if((e.Button==MouseButtons.Left)&&(tag==1))
  6.      {
  7.            i=i+1;
  8. musteri=new Bitmap("f:musteri_button.jpg");
  9. //                    node[j]=new PictureBox();
  10. //                    node[j].Image=musteri;
  11. }
Nov 16 '07 #1
4 3495
munibe
3
Hi, i have a problem about picturebox control. if you may help me, i will be so happy. i have a picturebox named pic_map, and i added a button named btn_customer to my form, my wish is to add new small picturebox to the coordinates that the user click on the pic_map and the new added picturebox should have click event to get the location of it when it is clicked and also be a member of the picturebox array.

after clicking the btn_customer, the coordinate thet user click on the pic_map there should be added a new picturebox with a tag number increasing 1 every time.

i have written some code in C# but when clicking one of the small picturebox it just gets the location of the last added one, not the one that i clicked on .

here is my code:

Expand|Select|Wrap|Line Numbers
  1. pic_map: main picturebox
  2.  
  3. i global int, initial value=0
  4.  
  5. musteri=new Bitmap("f:musteri_button.jpg");
  6. PictureBox [] node; //global
  7.  
  8. private void pic_map_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
  9. {
  10.        x=e.X;
  11.        y=e.Y;
  12. node=new PictureBox[50];
  13.       if((e.Button==MouseButtons.Left)&&(tag==1))
  14.      {
  15.          i=i+1;
  16.         node[j]=new PictureBox();
  17.         node[j].Image=musteri;
  18.        node[i].Size=new System.Drawing.Size(36,26);
  19.        node[i].Location=new System.Drawing.Point(x,y);
  20.        node[i].Visible=true;
  21.        node[i].Name="node"+i;
  22.        node[i].Click += new System.EventHandler(this.node_Click);
  23.        node[i].Tag=i;
  24.        this.pic_map.Controls.Add(node[i]);
  25. private void btn_customer_Click(object sender, System.EventArgs e)
  26. {
  27.     tag=1;
  28. }
  29.  
  30. private void node_Click(object sender, EventArgs e)
  31. {
  32. int a,b;   
  33.    a=node[Convert.ToUInt32(((PictureBox)sender).Tag)].Location.X;
  34.    b=node[Convert.ToUInt32(((PictureBox)sender).Tag)].Location.Y;
  35. }
Please help me, thanks for your consider....
Nov 16 '07 #2
kenobewan
4,871 Expert 4TB
An if statement runs only once and you want to run a loop. HTH.
Nov 16 '07 #3
Plater
7,872 Expert 4TB
Couldn't u assign a mouse click event to the picturebox itself?
Nov 16 '07 #4
munibe
3
An if statement runs only once and you want to run a loop. HTH.
Thanks, i have tried to use a loop, but could'nt find a way to get the clicked location while in the loop, if i define the x, y coordinate by manuel it works, but it does'nt allow getting the coordinates from picturebox while looping, or i could'nt find a way...

if you have an idea please confirm, thanks again
Nov 16 '07 #5

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

reply views Thread by Tim Bücker | last post: by
reply views Thread by akh | last post: by
5 posts views Thread by Christopher Kurtis Koeber | last post: by
2 posts views Thread by kalp suth via DotNetMonster.com | last post: by
5 posts views Thread by toby | last post: by
1 post views Thread by Steven Garrad | last post: by
reply views Thread by guiromero | last post: by
reply views Thread by devrayhaan | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.