0

I have a panel (with autoscrolling) that contains randomly placed UserControls, I want to save the locations of these controls and load them back at a later time so they are placed exactly where they were before.

What's the proper way to do this in .NET? At the moment this is what I'm saving to the database as X,Y:

X: Math.Abs(panel.AutoScrollPosition.X) + control.Location.X;

Y: Math.Abs(panel.AutoScrollPosition.Y) + control.Location.Y;

And when I load the control I do: control.Location = new System.Drawing.Point(X, Y);

But I think I'm missing something, because of the way the AutoScroll behaves in .NET. Sometimes I find the controls misplaced (unlike their old position) after loading.

Been boggling my mind for a while now, I really hope I'd find some information here.

prettyvoid
  • 3,446
  • 6
  • 36
  • 60

3 Answers3

0

Not sure on all the configuration you are using, but make sure the incremental steps for the scroll bars are whole numbers. Next make sure that the controls are getting added back to the panel control tree instead of the parent form and set the location.

0

You could use a app.config file to save those settings, so when you need them back you just make a call to the settings for the key inside app.config.

I think there is not a proper way, whatever you feel better it works, but if you are going to have dynamically created controls you could have a database as you have now. But, if there are going to be just a few of them, a app.config file will work better.

Camilo Aguilar
  • 129
  • 1
  • 11
0

I presume you want to restore the controls to their current visual position after scrolling and that is why you take account of the auto-scroll? When you say the controls are misplaced, have they moved relative to each other, or are the whole lot 'scrolled' to the wrong position?

Are you sure about the Math.Abs? This seems somewhat odd; I would try with just adding Location.X and AutoScrollPosition.X (or use -AutoScrollPosition.X)

I would also check on restore that the AutoScrollPosition is currently 0.

Stuart Whitehouse
  • 1,421
  • 18
  • 30