Cullub
Cullub

Reputation: 3218

How to hide scroll bars without disabling them

Is there a way to scroll on a RichTextBox without the scroll bar visible? I searched Google, but only came up with the ScrollBars Property.

I am using Microsoft Visual C# Express, winforms.

EDIT:
Something that would fix my problem would be a void with a few parameters such as a RTB, which direction to scroll, and how far to go.

Upvotes: 1

Views: 1877

Answers (1)

BroVirus
BroVirus

Reputation: 164

Some steps to do:

  1. Put your RTB in a Panel (Dock: none)
  2. Call panel.width -= 20; within your code.
  3. Now we need a mouse wheel scrolling without focus, use my code below:

Here is a little workaround:

public Main()
{
    InitializeComponent();

    //Works for panels, richtextboxes, 3rd party etc..
    Application.AddMessageFilter(new ScrollableControls(richTextBox1));
}

ScrollableControls.cs:

using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;

//Let controls scroll without Focus();

namespace YOURNAMESPACE
{
    internal struct ScrollableControls : IMessageFilter
    {
        private const int WmMousewheel = 0x020A;
        private readonly Control[] _controls;

        public ScrollableControls(params Control[] controls)
        {
            _controls = controls;
        }

        bool IMessageFilter.PreFilterMessage(ref Message m)
        {
            if (m.Msg != WmMousewheel) return false;
            foreach (var item in _controls)
            {
                ScrollControl(item, ref m);
            }
            return false;
        }

        [DllImport("user32.dll")]
        private static extern int SendMessage(IntPtr hWnd, int msg, int wParam, int lParam);

        private static void ScrollControl(Control control, ref Message m)
        {
            if (control.RectangleToScreen(control.ClientRectangle).Contains(Cursor.Position) && control.Visible)
            {
                SendMessage(control.Handle, m.Msg, m.WParam.ToInt32(), m.LParam.ToInt32());
            }
        }
    }
}
  1. Keep in mind that 3rd party controls often use nested container or similiar like radScrollablePanel1.PanelContainer, so you have to call them instead.

Upvotes: 2

Related Questions