Windows Develop Bookmark and Share   
 index > Windows Forms General > C# richTextBox Control Scroll bar Issue
 

C# richTextBox Control Scroll bar Issue

Is there an easy way to determine if the scroll bar of a richtextbox control is at the bottom?

Regards
pauldevinedotnet  Friday, September 18, 2009 5:53 AM
I quickly glanced the properties and methods of the RichTextBox control, but I could not find a .Net way of doing it. Therefore, I put this code together. It should accomplish what you need.

A note: Under Windows 7, I found the function GetScrollInfo() in user32.dll even though MSDN online states that the function belongs to comctl32.dll. Just a heads up in case it fails in Vista or XP.

using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    class Win32ScrollBars
    {
        public enum ScrollBarDisposition
        {
            Horizontal = 0,
            Vertical = 1
        }
        [Flags]
        private enum SBInfoFlags : uint
        {
            Range = 0x1,
            Page = 0x2,
            Position = 0x4,
            TrackPosition = 0x10,
            All = Range | Page | Position | TrackPosition
        }
        [StructLayout(LayoutKind.Sequential)]
        public struct SCROLLINFO
        {
            public uint size;
            public uint mask;
            public int min;
            public int max;
            public uint page;
            public int pos;
            public int trackPos;
        }
        [DllImport("user32.dll")]
        private static extern bool GetScrollInfo(IntPtr hWnd, ScrollBarDisposition bar, IntPtr lpsi);
        public static SCROLLINFO GetScrollBarInfo(Control control, ScrollBarDisposition sbDisp)
        {
            SCROLLINFO info = new SCROLLINFO();
            info.size = (uint)Marshal.SizeOf(typeof(SCROLLINFO));
            info.mask = (uint)SBInfoFlags.All;
            IntPtr infoPtr = Marshal.AllocHGlobal((int)info.size);
            Marshal.StructureToPtr(info, infoPtr, false);
            GetScrollInfo(control.Handle, sbDisp, infoPtr);
            info = (SCROLLINFO)Marshal.PtrToStructure(infoPtr, typeof(SCROLLINFO));
            Marshal.FreeHGlobal(infoPtr);
            return info;
        }
    }
}


The above class should provide you with the information of the scrollbar of your choice. I did the following to achieve what you needed.

        private void Form1_Load(object sender, EventArgs e)
        {
            richTextBox1.Text = "Some really long textj so I get a vertical scroll bar.";
            richTextBox1.VScroll += new EventHandler(richTextBox1_VScroll);
        }

        void richTextBox1_VScroll(object sender, EventArgs e)
        {
            Win32ScrollBars.SCROLLINFO info = Win32ScrollBars.GetScrollBarInfo(richTextBox1, Win32ScrollBars.ScrollBarDisposition.Vertical);
            if (info.pos + info.page == info.max)
            {
                MessageBox.Show("The scrollbar is at the bottom!");
            }
        }

This seems to work as required.
MCP
webJose  Monday, September 21, 2009 3:27 AM
If you use WordWrap you should not see it, and you can always set which ones you want to show.

RichTextBoxScrollBars.Both()
RichTextBoxScrollBars.ForcedBoth()
RichTextBoxScrollBars.ForcedHorizontal()
RichTextBoxScrollBars.ForcedVertical()
RichTextBoxScrollBars.Horizontal()
RichTextBoxScrollBars.Vertical()
RichTextBoxScrollBars.None()

VB.NET to C# http://www.developerfusion.com/tools/convert/vb-to-csharp/
Se3ker385  Friday, September 18, 2009 7:15 AM
Thanks, but not quite what i meant. If the scroll bar is visible I need to detect if the user has move the slider all the way to the bottom of the richtextbox and just wondering if there was a quick way of determining this.
pauldevinedotnet  Sunday, September 20, 2009 11:58 PM
I quickly glanced the properties and methods of the RichTextBox control, but I could not find a .Net way of doing it. Therefore, I put this code together. It should accomplish what you need.

A note: Under Windows 7, I found the function GetScrollInfo() in user32.dll even though MSDN online states that the function belongs to comctl32.dll. Just a heads up in case it fails in Vista or XP.

using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    class Win32ScrollBars
    {
        public enum ScrollBarDisposition
        {
            Horizontal = 0,
            Vertical = 1
        }
        [Flags]
        private enum SBInfoFlags : uint
        {
            Range = 0x1,
            Page = 0x2,
            Position = 0x4,
            TrackPosition = 0x10,
            All = Range | Page | Position | TrackPosition
        }
        [StructLayout(LayoutKind.Sequential)]
        public struct SCROLLINFO
        {
            public uint size;
            public uint mask;
            public int min;
            public int max;
            public uint page;
            public int pos;
            public int trackPos;
        }
        [DllImport("user32.dll")]
        private static extern bool GetScrollInfo(IntPtr hWnd, ScrollBarDisposition bar, IntPtr lpsi);
        public static SCROLLINFO GetScrollBarInfo(Control control, ScrollBarDisposition sbDisp)
        {
            SCROLLINFO info = new SCROLLINFO();
            info.size = (uint)Marshal.SizeOf(typeof(SCROLLINFO));
            info.mask = (uint)SBInfoFlags.All;
            IntPtr infoPtr = Marshal.AllocHGlobal((int)info.size);
            Marshal.StructureToPtr(info, infoPtr, false);
            GetScrollInfo(control.Handle, sbDisp, infoPtr);
            info = (SCROLLINFO)Marshal.PtrToStructure(infoPtr, typeof(SCROLLINFO));
            Marshal.FreeHGlobal(infoPtr);
            return info;
        }
    }
}


The above class should provide you with the information of the scrollbar of your choice. I did the following to achieve what you needed.

        private void Form1_Load(object sender, EventArgs e)
        {
            richTextBox1.Text = "Some really long textj so I get a vertical scroll bar.";
            richTextBox1.VScroll += new EventHandler(richTextBox1_VScroll);
        }

        void richTextBox1_VScroll(object sender, EventArgs e)
        {
            Win32ScrollBars.SCROLLINFO info = Win32ScrollBars.GetScrollBarInfo(richTextBox1, Win32ScrollBars.ScrollBarDisposition.Vertical);
            if (info.pos + info.page == info.max)
            {
                MessageBox.Show("The scrollbar is at the bottom!");
            }
        }

This seems to work as required.
MCP
webJose  Monday, September 21, 2009 3:27 AM

You can use google to search for other answers

Custom Search

More Threads

• multiline problem
• binding a datatable to a datagrid.
• change location ar tun time
• How to update richtextbox dynamically with multithreading in windows forms using C#??
• Error creating window handle in VS.NET 2005 C#
• Form load is very slow
• Windows forms, timers and message pumps?
• How to add VB Thunder Combo box inside dotnet dialog?
• Why does ATL contorl receive WM_DESTROY mesage when it's loaded from a form in MDI App?
• Activex .ocx to .NET