Reputation: 1244
I am building an image viewer with scrollable thumbnails. I have a form with a panel and autoscroll set to true on the panel. I load the panel with pictureboxes, one for each image in a folder; these are the thumbnails, and are scrollable in the panel once the panel has several pictureboxes in it.
I can click one or more pictureboxes (thumbnails) and put a border around each picturebox that is clicked. I at first used BorderStyle = BorderStyle.Fixed3D, but that thin border is inadequate. So, now I am setting a border on a picturebox by painting a rectangle on the picturebox:
private void SetBorder(PictureBox pb)
{
var color = ColorTranslator.FromHtml("#ff9900");
var rc = pb.ClientRectangle;
rc.Inflate(-1, -1);
ControlPaint.DrawBorder(pb.CreateGraphics(), rc, color, 3, ButtonBorderStyle.Solid, color, 3, ButtonBorderStyle.Solid, color, 3, ButtonBorderStyle.Solid, color, 3, ButtonBorderStyle.Solid);
}
This looks a lot better than the lame Fixed3D borderstyle, but I am having issue when scrolling the images ONLY when using the track part of the scrollbar (the area outside of scrollbar, either left or right). Clicking and dragging the scrollbar itself, or using the arrows works fine (I repaint from panel scroll event, see further below). But when clicking the track, the borders are not repainted when the pictureboxes are scrolled back from being scrolled out of view. For example, I click a couple thumbnails and set borders:
The borders look good, but if I scroll to the right (clicking the track), then back to the left, what was covered is not repainted. For example:
As I mentioned above, when clicking the scrollbar arrows, or dragging the scrollbar, I am repainting the borders in the panel scroll event:
private void panel1_Scroll(object sender, ScrollEventArgs e)
{
SetBorders(panel1);
}
However, when clicking in the track, that does not seem to raise the Scroll event of the Panel.
I forgot to mention, SetBorders (plural) is another method (I didn't include this method in the question) that loops thru the pictureboxes in the panel, and for each one that should be repainted, it calls SetBorder (method included above) and passes given picturebox...
Just discovered that the issue is present also when using mousewheel to scroll.
Any ideas?
Upvotes: 1
Views: 535
Reputation: 7204
Set all the picboxes paint event
to the same sub:
pictureBox1.Paint += pictureBox_Paint;
pictureBox2.Paint += pictureBox_Paint;
....
....
or after adding the picboxes
to panel
:
var children = panel1.Controls.OfType<Control>();
foreach( Control child in children ) {
( (PictureBox)child ).Paint += pictureBox_Paint;
}
and in the event:
private void pictureBox_Paint( object sender, PaintEventArgs e ) {
PictureBox picbox = (PictureBox)sender;
var color = ColorTranslator.FromHtml( "#ff9900" );
var rc = picbox.ClientRectangle;
rc.Inflate( -1, -1 );
ControlPaint.DrawBorder( e.Graphics, rc, color, 3, ButtonBorderStyle.Solid, color, 3,
ButtonBorderStyle.Solid, color, 3, ButtonBorderStyle.Solid,
color, 3, ButtonBorderStyle.Solid );
}
No need to use panel1_Scroll
event any more!
Upvotes: 1