0

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.

Sinatr
  • 20,892
  • 15
  • 90
  • 319
noobprog
  • 327
  • 1
  • 4
  • 16
  • Possible [duplicate](https://stackoverflow.com/q/1600664/1997232). – Sinatr Dec 03 '20 at 08:27
  • Is your code works? Do you get exception or unexpected behavior or simply requesting [codereview](https://codereview.stackexchange.com/)? – Sinatr Dec 03 '20 at 08:29
  • 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. – noobprog Dec 03 '20 at 08:29
  • yes 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. – noobprog Dec 03 '20 at 08:31
  • Ok, now the question is more clear, you are using [this answer](https://stackoverflow.com/a/4766428/1997232). Your mistake is that you are subscribing multiple panels to same event handler, so you have to use `sender` inside to focus that specific panel. – Sinatr Dec 03 '20 at 08:32
  • Hmm...why are you doing everything in `Main`? You are lucky you are using `ShowDialog` otherwise the UI wouldn't update properly not to mention your app would exit immediately –  Dec 03 '20 at 08:33
  • hi @MickyD same with python, I know the difference between Show and ShowDialog. – noobprog Dec 03 '20 at 08:35
  • hi @Sinatr I have only 1 panel. – noobprog Dec 03 '20 at 08:36
  • 1
    Ow, right. I was too fast at judging, somehow `panel.Controls.Add(labels[i]);` line makes bad trick with my brains. – Sinatr Dec 03 '20 at 08:39
  • Ok, found an issue. You are setting `panel.AutoScroll = true` and it should be `form.AutoScroll = true`. – Sinatr Dec 03 '20 at 08:44
  • 1
    Setting `panel.AutoSize = true;`, the container is enlarged when you add new Controls, so the Scrollbars are never actually needed. As mentioned above, make the Form AutoScroll. -- This code is quite weird anyway; what kind of application is running this? Not WinForms. – Jimi Dec 03 '20 at 09:55
  • thank you for your help guys. i know what to do now. :) – noobprog Dec 07 '20 at 00:08

0 Answers0