C# TableLayoutPanel and and Mouse Scroll.

I have a simple problem with my windows application developed in .NET. I have a TableLayoutPanel which contains a RichTextBoxcontrol. I am not able to scroll the TableLayoutPanel.

I made a search in Google and found that many people has the same problem like [1] [2] The idea for to resolving this issue cams from [1] blog.

In fact the Panel can’t have the focus itself, only an item placed inside the panel can have the focus. The panel will only receive the MouseWheel event once something is placed inside it and that thing has the focus. Simply hoevering over the panel and moving the mouse wheel will send the event to the form, not to the TableLayoutPanel.

That’s I implemented a MouseWheel Event in the RichTextBox placed inside the panel. In the implementation I Update the drawing of the TableLayoutPanel (parent) based upon the mouse wheel scrolling.

this.richTextBox.MouseWheel +=
         new System.Windows.Forms.MouseEventHandler(richTextBox_MouseWheel);
void richTextBox_MouseWheel(object sender, System.Windows.Forms.MouseEventArgs e)
{
    if (!(this.Parent == null) &&
         (this.Parent.GetType() == typeof(TableLayoutPanel)))
    {

        TableLayoutPanel parentPanel = (TableLayoutPanel)this.Parent;
        if (e.Delta != 0)
        {
            int newVerticalScrollValue = parentPanel.VerticalScroll.Value + e.Delta;
            if (newVerticalScrollValue > parentPanel.VerticalScroll.Maximum)
                newVerticalScrollValue = parentPanel.VerticalScroll.Maximum;

            if (newVerticalScrollValue < parentPanel.VerticalScroll.Minimum)
                newVerticalScrollValue = parentPanel.VerticalScroll.Minimum;

            parentPanel.VerticalScroll.Enabled = true;
            parentPanel.VerticalScroll.Value = newVerticalScrollValue;
        }
    }
}

This solve the problem!

Excample

TableLayoutPanelTest

References

  1. Soul Reaver ,Problem with scrolling with TableLayoutPanel,Jan 03, 2007
  2. Jabber Infovish ,Mouse scroll on TableLayoutPanel breaks layout.,April 27, 2011
Leave a comment ?

15 Comments.

  1. Greetings! This is my 1st comment here so I just wanted to give a quick shout out and tell you I really enjoy reading your articles. Can you recommend any other blogs/websites/forums that cover the same subjects? Appreciate it!

  2. I have a limited account and an administrator account on Windows XP. I would like to import all of my Firefox settings from my Limited Account to my Admin Account so they are exactly the same..

  3. “Our real problem is not our strength today; it is the vital necessity of action today to ensure our strength tomorrow.” – Dwight D. Eisenhower

  4. I am really impressed with your writing skills and also with the layout on your blog. Is this a paid theme or did you modify it yourself? Either way keep up the nice quality writing, it’s rare to see a nice blog like this one today..

  5. Wow, amazing weblog structure! How long have you been blogging for? you made running a blog look easy. The overall glance of your web site is great, as smartly as the content!

  6. naturally like your web site however you need to test the spelling on several of your posts. Many of them are rife with spelling issues and I in finding it very troublesome to tell the truth nevertheless I’ll surely come again again.

  7. I just like the helpful info you provide on your articles. I’ll bookmark your blog and take a look at again right here regularly. I am moderately sure I will be informed a lot of new stuff right here! Best of luck for the following!

  8. It’s actually a cool and helpful piece of information. I’m happy that you simply shared this useful information with us. Please keep us informed like this. Thank you for sharing.

  9. I actually wanted to compose a small note to thank you for some of the pleasant secrets you are showing on this website.

  10. This is not really onlly one of your reports I’ve look at, and you just never stop to astonish me personally. Many thanks, and I will enjoy reading more.

  11. I believe that is among the so much important info for me. And i’m happy studying your article. However want to statement on some general things, The website style is perfect, the articles is actually great : D. Excellent process, cheers

  12. Hi there there administrator, I actually wished to firmly make a immediate remark to mention that I favored your specific blog post. Thanks!

  13. Hello There. I discovered your blog the use of msn. This is an extremely well written article. I’ll be sure to bookmark it and come back to read more of your helpful information. Thanks for the post. I’ll definitely comeback.

  14. Your blog won’t display appropriately on my iphone4 – you might wanna try and repair that

  15. That’s really thiiknng out of the box. Thanks!

Leave a Comment


NOTE - You can use these HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

Captcha Captcha Reload