Here’s my code:
int ypos = 0;
public void X()
{
MovieItem NewMovie = new MovieItem();
NewMovie.SearchMovie(txtSearch.Text);
NewMovie.Location = new Point(0, ypos);
ypos += 196;
panel1.Controls.Add(NewMovie);
}
After running method X about 4,5 times, the 5th or 6th item added isn’t added where it should instead it is loaded wayyyy below.
Image for clarity:
How it looks CORRECTLY at the beginning:
How it looks WRONG after a bit:
What am I doing wrong that it doesn’t work that well?
EDIT:
I’ve used breakpoints and discovered that the problem occurs on the fifth movie, when the ypos variable is around 980. Why could this be? It just skips that location it seems.
ok, the problem appears when the
AutoScrollproperty of panel is set totrueand the scroll bar is not at its initial position:0. The panel is positionning the controls atypos + vertical position of scroll bar!!Either set
AutoScroll = false;before addingMovieItemthentrue;after, but this will reset the scroll bar positionor, substract the scroll bar position from
yposwhen setting the location.Both fix the problem but the 2nd is better (no scroll bar reset):