Adding clickable boxes inside picturebox c#

133 views Asked by At

So I have a picture box that I need to draw rectangles or declare regions either or, so that the user can click on an area of the picture and it do something. I have searched and searched about this and have come to the conclusion that I do either need rectangles to target or a region. I really don't want a visible item over the map if that simplifies it, just a place for the user to click and it do an action.

On top of this the map will be changing and when it does I need to change the boxes to other locations. Below is what I came up with and its not working. I am open to other ways as well.

    public Rectangle Location1;
    public Rectangle Location2;

    public String CharacterLocation == "WorldMap";

private void GenerateRegions ()
    {
        Pen blankPen = new Pen(Color.Transparent, 3);

        if (CharacterLocation == "WorldMap")
        {
            Rectangle[] rects =
        {
             Location1 = new Rectangle(100, 200, 250,  50),
             Location2 = new Rectangle(50, 100, 250,  50)

        };

            MapBox.CreateGraphics().DrawRectangles(blankPen, rects);
        }
    }
   private void MapBox_MouseDown(object sender, MouseEventArgs e)
    {
        if (Location1.Contains(e.Location))
       {

       }
   }
1

There are 1 answers

0
Morrg On

OK so here is what got it going for me. Its drawing the rectangles at the wrong spot, but I assume its cause the image is zoomed.

       private void GenerateRegions ()
    {

        MapBox.Invalidate();
        Pen blankPen = new Pen(Color.Transparent, 3);
        Console.WriteLine(CharacterLocation);

        if (CharacterLocation == "WorldMap")
        {
            Console.WriteLine(CharacterLocation);

            Rectangle[] rects =
        {
             Location1 = new Rectangle(64, 25, 20,  20),
             Location2 = new Rectangle(68, 70, 20,  20)

        };

            using (var g = Graphics.FromImage(MapBox.Image))
            {
                g.DrawRectangles(blankPen, rects);
                MapBox.Refresh();
            }
        }
   private void MapBox_MouseDown(object sender, MouseEventArgs e)
    {
        if (Location1.Contains(e.Location))
       {
            arealbl.Text = "You clicked it 1";
        }
        if (Location2.Contains(e.Location))
        {
            arealbl.Text = "You clicked it 2";
        }

}