0

I have a Custom panel which contains 4 labels (in example pasted, I have erased definition of those 2 I didn't used). This custom panel is added to a panel dynamically, as many records I have in an array. The parent panel, has the Horizontal scroll enabled = false, as I should scroll only vertically. Everything works fine, as I can use mouse on vertical scrollbar and I can scroll the panel container. But when I want to use mouse wheel to scroll all the custom panels (child objects) it does not do anything. I have tried many of the solutions that works for other on this site, but none of them works for me and I don't know why.

I read about the panel has to have focus to be able to scroll, and I will have to pass the OnMouseWheel event to parent, in child. But I am not able to do it, I don't know how to do it.

my custom panel (child):

public class PlaylistRecords : Panel
{
    public Label lblRecordNumber { get; private set; }

    private Label lblRecordName;

    public static int RecordNumber { get; set; }
    public static String RecordName { get; set; }

    public PlaylistRecords(
            int RecordNumber, 
            String RecordName
            )
    {
        InitializeComponent();
        this.Size = new System.Drawing.Size(800,50);
        this.BackColor = System.Drawing.Color.FromArgb(20,20,20);

        PlaylistRecords.RecordNumber = RecordNumber;
        PlaylistRecords.RecordName = RecordName;

        this.lblRecordNumber.Text = PlaylistRecords.RecordNumber.ToString()+".";
        this.lblRecordName.Text = PlaylistRecords.RecordName;

        this.lblRecordNumber.Location = new System.Drawing.Point(2, (int)(this.Height - this.lblRecordNumber.Height) / 2);
        this.lblRecordName.Location = new System.Drawing.Point(
                    this.lblRecordNumber.Location.X+ this.lblRecordNumber.Width+2, 
                    (int)(this.Height - this.lblRecordName.Height) / 2);

    }

    private void InitializeComponent()
    {
        this.lblRecordNumber = new myLabel();
        this.lblRecordName = new myLabel();
        this.lblRegistPath = new myLabel();

        this.SuspendLayout();
        // 
        // lblRecordNumber
        // 
        this.lblRecordNumber.Name = "lblRecordNumber";
        this.lblRecordNumber.Size = new System.Drawing.Size(50, 23);

        // 
        // lblRecordName
        // 
        this.lblRecordName.Name = "lblRecordName";
        this.lblRecordName.Size = new System.Drawing.Size(150, 23);

        // 
        // PlaylistRecords
        // 
        this.Controls.Add(this.lblRecordNumber);
        this.Controls.Add(this.lblRecordName);
        this.ResumeLayout(false);

    }
}
Vali Maties
  • 379
  • 1
  • 2
  • 21
  • 1
    Windows versions older than Win10 send the mouse wheel notification to the control that has the focus. Key problem here is that none of the controls you use can get the focus. So trying to pass the message from child to parent is not a solution either. Creating your own grid control is never not a mistake. Consider a ListView. Or use a panel that can get the focus: https://stackoverflow.com/questions/3562235/panel-not-getting-focus – Hans Passant Jul 20 '19 at 13:44

2 Answers2

1
this.SetStyle(ControlStyles.Selectable, true);
this.TabStop = true;

These two lines of code written in constructor of child control (panel) is doing the job.

Thank you @Hans Passant for your comment. :)

Vali Maties
  • 379
  • 1
  • 2
  • 21
0

Well if a control is not accepting events then they would be passed to parent

lblRecordNumber.Enabled = false;
lblRecordName.Enabled = false;

alternatively you can pass the scroll event to containing control referred to as Parent

internal class myLabel : Label
{
    const int WM_MOUSEWHEEL = 0x020A;

    protected override void WndProc(ref Message m)
    {

        if (m.Msg == WM_MOUSEWHEEL)
            m.HWnd = this.Parent.Handle; 

        base.WndProc(ref m);
    }
}

and use that control instead.

Margus
  • 19,694
  • 14
  • 55
  • 103
  • lblRecordNumber and lblRecordName are Label based controls. They don't have "IsEnabled" property. At least VS2019 does not show me in intellisense :( Also, your solution overriding WndProc event does not work. It does not do anything :( – Vali Maties Jul 20 '19 at 14:16
  • 1
    @ValiMaties My mistake, writing from memory, it is called "Enabled" instead. – Margus Jul 20 '19 at 14:19