0

I have a large tile-based map created in a windows form. Currently I am working in a 100 by 100 tile map (each tile is 50x50 pixels) by created by merging tile sprites into a large image and placing it in a picturebox the same size as the form. One of the abilities of this game is to place cities on this map in the form of pictureboxes. The problem comes when I try to enter a location for placement on the map.

When I enter the desired grid location for a city when I am at the top left of the map, the city shows up as it should:
Entering coordinates without scrolling

Result of entering cooridnates without scrolling

When I scroll in any way and try to place a city at the same location the form places city picturebox way off the map and stretches the form to fit where it thinks the city picturebox goes:

Entering coordinates with scrolling

The Forms stretches and places the city way off the map

Here is the line of code I use for placing the city picturebox:

new_city.Location = new System.Drawing.Point(((add_city_x * 50)) + 20,
                                             ((add_city_y * 50)) + 20);

Where add_city_x and add_city_y are the coordinates inputted by the user, the *50 is the size of each tile, and the +20 is an offset for each tile to make room for a drop down menu.

So far I have tried:

  1. Setting the maximum size of the picturebox. The program still will stretch the form to add the picture box.

  2. Setting the border style. This had the same affect as the previous trial.

  3. Compensating for where I am in the form by dividing the add_city *50 variable and dividing it by my current location in the form. (I am not sure if I did this correctly so further advice on this procedure may be helpful).

  4. Subtracting the form location form the calculation: (add_city_x*50)-(this.Location.x*50)+20. I did the same for the y-location, this did not help with the problem at all.

From what I have observed I know that the code computes the location calculation from the perspective of where I have scrolled to in the form. For instance, If I have scrolled half way across the form vertically and horizontally and then try to add a city to the map, the form will think that the location calculation begins at the top left of where I am rather than the top left of the form, resulting in the form being stretched to fit where the program thinks the city picturebox should be added. I'm looking for the calculation to run always from the upper left of the form instead of from wherever I have scrolled to on the screen.

dovid
  • 6,354
  • 3
  • 33
  • 73
  • just so you know there is a maximum height width that you can display things in a picturebox. I suggest you start drawing portion of the image in the picturebox depending on it's dimensions and how much you scrolled the map. So if you scroll to 50 x and 40 y and your picture box is 500x600 you draw the image of 50,40 to 550,640. Like that you regen just a part of the image all the time not all. This will also be much easier to debug as you draw whast should be in that portion of screen. – Franck Nov 07 '17 at 21:08
  • See [Scrolling PictureBox](https://stackoverflow.com/q/27817333/719186) – LarsTech Nov 07 '17 at 22:17

0 Answers0