Hide scrollbars of a RichTextBox

前端 未结 1 2002
情话喂你
情话喂你 2021-01-16 16:23

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 scrol

1条回答
  •  一生所求
    2021-01-16 17:18

    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:

    0 讨论(0)
提交回复
热议问题