473,701 Members | 2,732 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

3 New Member
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 3814
3 New Member
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
  3. i global int, initial value=0
  5. musteri=new Bitmap("f:musteri_button.jpg");
  6. PictureBox [] node; //global
  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. }
  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
4,871 Recognized Expert Specialist
An if statement runs only once and you want to run a loop. HTH.
Nov 16 '07 #3
7,872 Recognized Expert Expert
Couldn't u assign a mouse click event to the picturebox itself?
Nov 16 '07 #4
3 New Member
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

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

Similar topics

by: Tim Bücker | last post by:
Hello. I want to render in a picturebox (pbD3DSurface) using DirectDraw. This is working more or less but the coordinates are always screen oriented and not pictureBox oriented. x = 10; //+this.Location.X+this.pbD3DSurface.Location.X; y = 10; //+this.pbD3DSurface.Location.Y+this.Location.Y; back.DrawCircle(x, y, 5); //back = surface
by: akh | last post by:
I want to use de Drag and Drop ´s event to move a picture box from a form and within a Picture Box. But I have behaviour if the MyPBox As PictureBox as the Globale varible or not Thanks for your help, Here is the code.
by: Christopher Kurtis Koeber | last post by:
Dear All, This may sound like an elementary question but how do you implement scrollbars for the Picturebox control. Do I have to create my own code to do this or is there some property that I can set? Thank you very much for your time and consideration in this matter! Christopher Koeber
by: kalp suth via DotNetMonster.com | last post by:
I want to create arrows using lines on a picture in the picture box. On clicking the button "btnShowAll", the image is loaded and the lines drawn. "RGSShowAll()" calls "DrawObjs()" which does the actual drawing work. But after displaying all the arrows in a flick, the arrows disappers. I have debugged and came to know that the "Paint" event of the picturebox is refreshing the image, so the work done on the CreateGraphics created object is...
by: Tom | last post by:
I have a picturebox on my VB.NET form. The picturebox size mode is set to stretched. I then load an image into that form and display it. As the user moves the mouse over the form, I want to get and display (in the status bar) the image coordinates of the mouse location. However, if I use the picturebox's MouseMove event, I am getting the coordinates of the mouse over the PICTUREBOX, not the actual image underneath that (which is stretched)....
by: toby | last post by:
Hi there, Is it possible to create an array of picturebox controls during run-time. I wish to create a new image/picturebox everytime a user clicks the button on a form, and they need to be objects so that the user can move/drag them around. The only examples I can find dont work as I am using VB Express (2005?)
by: Steven Garrad | last post by:
Hi All, I have a pictureBox control inside of a panel control. The pictureBox is larger than the panel control and I have the panel control set true for AutoScroll so the panel displays scrolling bars. I would like the user to be able to click and drag the image and for it to move around within the panel control. How would I do this?
by: Jim McGivney | last post by:
In C# on Form1 I genetate an array of PictureBoxes and populate each with an image as seen in the code below. Later on I want to access a specific PictureBox to change its image, but I keep getting the error "The name 'PictureBox1' does not exist in the current context" The code I use to try to access the PictureBox is "PictureBox1.Image = HoldBitMap; " where kount is an integer. I know the answer is probably in using the Controls...
by: AWW | last post by:
XP VB 2005 running an example from help that creates a picturebox in code - the picturebox is not created. If I comment out the "Dim Box as New PictureBox" and create it in Design mode - the example works. I have tried Show, Enabled, Visible, and even BackColor. Why does a help example not work? and why does Design creation work? Thanks - I hope.
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.