In my project, I need a stand alone scroll bar for my text box. So I have created a customized scroll bar. Any idea How to use my customized scroll bar(both Horizontal and Vertical) in the text box instead of built-in scroll bar?

I have created a control by placing a text box and a vertical scroll bar. When I scroll the scroll bar, the contents in the text box should be scrolled. How can I scroll the text box by scrolling my standalone scroll bar?
Please check the below sample code.

public partial class EditControl : Control
{
    int BORDERWIDTH = SystemInformation.Border3DSize.Width;
    int SCROLLBARWIDTH = SystemInformation.VerticalScrollBarWidth;
    CustomTextBox editCtrl;
    VScrollBar vScrollBar = null;
    public EditControl()
    {
        InitializeComponent();
        editCtrl = new CustomTextBox();
        this.Width = 200 + SCROLLBARWIDTH;
        this.Height = 140;

        editCtrl.Width = this.Width - SCROLLBARWIDTH;
        editCtrl.Height = this.Height;
        editCtrl.Multiline = true;
        editCtrl.Left = Left;

        vScrollBar = new VScrollBar();
        vScrollBar.Height = this.Height;
        vScrollBar.Location = new Point(editCtrl.Width, 1);
        vScrollBar.Scroll += new ScrollEventHandler(vScrollBar_Scroll);
        this.Controls.Add(editCtrl);
        this.Controls.Add(vScrollBar);
    }

    private void vScrollBar_Scroll(object sender, ScrollEventArgs e)
    {
        **//Code to scroll the text box
        //editCtrl.ScrollTo(position);**
    }
    protected override void OnResize(EventArgs e)
    {
        base.OnResize(e);
        editCtrl.Width = this.Width - SCROLLBARWIDTH;
        editCtrl.Height = this.Height;
    }
    public partial class CustomTextBox : TextBox
    {
        public CustomTextBox()
        {
            //InitializeComponent();
        }
        public void ScrollTo(int Position)
        {
           **//Code to scroll the contents.**
        } 
    }
}
}

The problem is solved. I have masked the original scrollbar of textbox by my customized scrollbar. GetScrollInfo API is used to synch the scrollbars.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.