I've just started coding in c# and I really dont understand some concepts and examples online. My goal is just to make the code below scrollable via mouse scroll.
Can someone help me by poining out my mistakes and the solutions need?
using System.Windows.Forms;
using System.Collections.Generic;
using System.Drawing;
namespace ScrollablePanel {
public class Application {
private static Panel panel = new Panel();
static void Main(string[] args ) {
Form form = new Form();
panel.AutoSize = true;
panel.AutoScroll = true;
List<Label> labels = new List<Label>();
for (int i=0; i<100; i++) {
labels.Add(new Label());
labels[i].Text = i.ToString() + ". This is a Label.";
labels[i].Location = new Point(10, 10 + i * 30);
panel.Controls.Add(labels[i]);
}
form.Controls.Add(panel);
panel.MouseWheel += new MouseEventHandler(panelMouseWheelFocus);
form.ShowDialog();
}
static void panelMouseWheelFocus(object sender, MouseEventArgs e) {
panel.Focus();
}
}
}
The solution given there is to have the panel focus but as you can see in the code above I did that and nothing happens.
My code works but the scrolling part is missing, even though the height of the panel exceeds the height of the form, scrolling is still disabled.