0

In my code I would like that the PictureBox.Location changes when a certain event is triggered. That is not the problem though, the problem is that I dont understand what the difference between setting the Location in the Designer to 310;12 and with System.Drawing.Point(310;12). In my case System.Drawing.Point(310;12) sets the position of the image way too far to the right. The point that I then tried out and fits with the designers Point is 232;12.

What is causing this and is there a way to fix it?

EDIT: Here is the code for changing the Location of the PB:

this.ball.Location = new Point(
    232,
    12
    );

EDIT: Here are my steps that I did:
1. Create a Form
2. Create 2 buttons
3. Create a PictureBox and set its location to 310;12 in the designer
4. Trying to rearrange the picturebox with the buttons; one that sets the location to somewhere else and one that sets the picturebox back to its original location.

Ian H.
  • 3,840
  • 4
  • 30
  • 60
  • 1
    Do show us the actual code. Do you also change the parent of the PB? Locations are always relative to the control's parent control/form. – TaW Nov 19 '15 at 18:12
  • @TaW see my edit. I dont think that I set a parent for the PB so I guess its the main Form1. – Ian H. Nov 19 '15 at 18:16
  • Declare your program [dpiAware](http://stackoverflow.com/a/13228495/17034) and try again. – Hans Passant Nov 19 '15 at 18:25
  • The code line is not the problem. something else is going on, e.g. with the PB's Size, SizeMode, Image. Test these by setting a BorderStyle for testing and report back. Maybe you should show us the full event and tell us more about it.. – TaW Nov 19 '15 at 18:25
  • @TaW The SizeMode is Zoom, the Size is 150;150 and the Image's Size is 150;150. I set a BorderStyle, but found out nothing, the program still seems to equalize 310;12 in the designer and 232;12 in the button_click event. – Ian H. Nov 19 '15 at 18:44
  • Hm, set a breakpoint right before you set the location the 1st time in code and check its value then... – TaW Nov 19 '15 at 18:50
  • @TaW Apparently it is set to `{X = 232 Y = 10}`. – Ian H. Nov 19 '15 at 18:52
  • Then I suspect docking and/or anchoring to be the culprit. – TaW Nov 19 '15 at 19:06
  • @TaW Anchor is Top-Left and I dont have a dock. – Ian H. Nov 19 '15 at 19:13
  • Well, I'm out of ideas atm. Usually Hans' ideas are spot on; I can't see how it (dpi awareness) applies here, though. One thing is for sure however: Hardcoding any such values into a program is not a robust design. So the obvious workaround may also be the better way to do it: store the 1st location at startup and use it.. Not an explanation but my best advice. (It is hard to say more without the whole code.) – TaW Nov 19 '15 at 19:31

1 Answers1

0

The Location is always relative to the parent. Maybe when you set the location in the designer, you accidentally placed the picturebox in a parent (such as a groupbox) without noticing.

We can't really help you without you giving us more details. Such as, did you set the location in the designer, modify it in the code, and upon modification it suddenly failed?

We need to know the steps you took to reproduce the problem.

CodingMadeEasy
  • 2,257
  • 4
  • 19
  • 31