views:

96

answers:

1

I'm trying to write a simple text editor like DarkRoom with just a RichTextBox (or alternatively a TextBox) in it. My problem is that I can't use the mouse wheel for scrolling unless I have a vertical scrollbar. Is there any way to hide this scrollbar and still be able to scroll with the mouse wheel?

So far I have several ideas how this could be done, but no idea how to implement them.

  • re-create the scrolling code using a MouseWheel event
  • change the visual style of the scrollbar to hide it or make it less visible
  • write my own TextBox widget
  • overlap the scrollbars with something else to hide them

P.S.: Using any win32 stuff is not an option.

+1  A: 

Yes, you will have to capture the .MouseWheel and .MouseMove events. See this post.

Ok, do something like following:

  1. Add a line in form load event.

    private void Form1_Load(object sender, EventArgs e)
    {
        this.richTextBox1.MouseWheel += new MouseEventHandler(richTextBox1_MouseWheel);
    }
    
  2. Add following in mouse wheel event.

    void richTextBox1_MouseWheel(object sender, MouseEventArgs e)
    {
        if (e.Delta > 0)
        {
            //Handle mouse move upwards
            if (richTextBox1.SelectionStart > 10)
            {
                richTextBox1.SelectionStart -= 10;
                richTextBox1.ScrollToCaret();
            }
        }
        else
        {
            //Mouse move downwards.
            richTextBox1.SelectionStart += 10;
            richTextBox1.ScrollToCaret();
        }
    }
    

Let me know in either cases, if you would want the running sample of the same; or if you are not liking the solution (0:

KMan
That doesn't explain how to do the scrolling part. That's were I'm stuck right now.
Maurice Gilden
Please see my answer with edits. Code added.
KMan
That's not exactly the right scrolling behavior I was looking for. The caret position shouldn't change while scrolling. But even if I set the SelectionStart back to the original value I won't get the right behavior. It might work if I know at which character position the visible line at the top starts or the line at the bottom ends. Just increasing/decreasing the SelectionStart value by a constant number doesn't work however.While testing this I also noticed that ScrollToCaret will scroll up and down a few pixels if the caret is already visible.
Maurice Gilden
Thanks Maurice for your feedback. Actually, according to your question 'Is there any way to hide this scrollbar and still be able to use the mouse wheel?'; Since you didn't know how you would be able to do it, I wanted to help you with a start point, and I did not intend to provide you with a complete solution. Again, the constant values are just for you to understand how this works.
KMan
Ok, I should have phrased that a little different then.
Maurice Gilden