Scroll Richtextbox explicitly using userdefined Scrollbar in C#.  
Author Message
Sindhu4solution





PostPosted: Windows Forms General, Scroll Richtextbox explicitly using userdefined Scrollbar in C#. Top

hi,

I came across a problem, in which we need to use our own userdefined scroll bar to scroll the richtextbox. After trying a lot.. i found this solution, and this works great. This program allow you to create your own custom scroll bar with 4 images. TOP scroll, BOTTOM scroll, base scroll and a image which moves in the center of the base Scroll image. so that u can move the image by clicking the top and bottom of the images which will make the richtextbox scroll bar to scroll simultaneously.

picturebox2 -- to scroll upwards

picturebox3 -- to scroll downwards

centerimage -- center image which moves in our user defined scrollbar.

The code goes here in C# for vertical scrolling.

using System.Runtime.InteropServices;

using System;

using System.Drawing;

using System.Collections;

using System.ComponentModel;

using System.Windows.Forms;

private bool mouse_is_down=false;

int mypostionx, mypostiony;

[DllImport("User32.dll", CharSet=CharSet.Auto, EntryPoint="SendMessage")]

static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam,IntPtr lParam);

private void centerimage_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)

{

mypostionx= scroll.Location.X ;

mypostiony= scroll.Location.Y;

}

private void centerimage_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)

{

if ( mouse_is_down )

{

int current_x = mypostionx;

int current_y = mypostiony;

scroll.Location= new Point(560,mypostiony);

mouse_is_down = false;

}

}

private void pictureBox2_Click(object sender, System.EventArgs e)

{

mouse_is_down=true;

mypostiony =scroll.Location.Y;

mypostiony= mypostiony - 7;

scroll_MouseMove(this,es );

// to make the richtextbox to scroll upwards

const int WM_VSCROLL = 277;

const int SB_LINEUP = 0;

IntPtr ptrWparam = new IntPtr(SB_LINEUP);

IntPtr ptrLparam = new IntPtr(0);

SendMessage(exRichTextBox1.Handle, WM_VSCROLL, ptrWparam, ptrLparam);

}

private void pictureBox3_Click(object sender, System.EventArgs e)

{

mouse_is_down=true;

System.Windows.Forms.MouseEventArgs es = null ;

mypostiony =scroll.Location.Y;

mypostiony= mypostiony + 7;

scroll_MouseMove(this,es);

// to make the richtextbox to scroll downwards

const int WM_VSCROLL = 277;

const int SB_LINEDOWN =1;

IntPtr ptrWparam = new IntPtr(SB_LINEDOWN);

IntPtr ptrLparam = new IntPtr(0);

SendMessage(exRichTextBox1.Handle, WM_VSCROLL, ptrWparam, ptrLparam);

}

}




Windows Forms29