I've dynamically added 20 pictureboxes to a panel and would like to see the panel scroll when I use the mouse wheel. To implement this I have tried to set the autoscroll to true on the panel control. Here is the code. For i As Integer = 1 To 20:
Dim b As New PictureBox()
b.Image = Nothing
b.BorderStyle = BorderStyle.FixedSingle
b.Text = i.ToString()
b.Size = New Size(60, 40)
b.Location = New Point(0, (i * b.Height) - b.Height)
b.Parent = Panel1
Panel1.Controls.Add(b)
Next
I did the same thing with button control and it works just fine. For i As Integer = 1 To 100:
Dim b As New Button()
b.Text = i.ToString()
b.Size = New Size(60, 40)
b.Location = New Point(0, (i * b.Height) - b.Height)
b.Parent = Panel1
Panel1.Controls.Add(b)
Next
It works for "button" control, but not for the "picturebox" or "label" controls? How can I implementthe scrolling affect using 'mousewheel'?
The panel scrolls with the mousewheel when it or a control within it has focus. The problem you are running into is that neither the PictureBox nor the panel receives focus when you click on it. If you call select()
on the panel, you will see that the mouse wheel starts working again.
One possible solution would be to select the panel whenever the mouse cursor enters it, by handling the Control.MouseEnter event:
void panel1_MouseEnter(object sender, EventArgs e)
{
panel1.select();
}