A
A
ase20152019-05-25 21:48:26
C++ / C#
ase2015, 2019-05-25 21:48:26

How to make a selection of an element in a picturebox (there is a code, you need to look at what the error is)?

There is a picturebox with a picture, there are some elements on it (I set the coordinates of the element) and when you click in this area, a message should appear like "well done" The
code below, but it does not work, help me fix it:

public Form4() 
{ 
InitializeComponent(); 

objects = new List<MapObject>(); 

objects.Add(new MapObject() 
{ 
Name = "Парусник", 
X = 193, 
Y = 35, 
Width = 32, 
Height = 40 
}); 
} 
List<MapObject> objects; 
private void pictureBox4_MouseClick(object sender, MouseEventArgs e) 
{ 

int currentX = e.X; 
int currentY = e.Y; 
for (int i = 0; i < objects.Count; i++) 
{ 
if(currentX>(objects[i].X-objects[i].Width/2)& currentX < (objects[i].X + objects[i].Width / 2) 
& currentY > (objects[i].Y - objects[i].Height / 2) & currentY < (objects[i].Y + objects[i].Height / 2)) 
{ 
MessageBox.Show("ghg"); 
} 
} 
}

and here is the code from the class created separately:
class MapObject 
{ 
public string Name; 

public int X; 
public int Y; 

public int Width; 
public int Height; 
}

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question