program that contains a Rich Text Box & a scroll bar where scroll bar represents the size of font size of selected text in Rich Text Box as soon as user scrolls the scroll

Recommended Answers

All 3 Replies

If I understand, you want the size of the scroll bar to be proportional to the font size of the selected text. In a RichText control, the scroll bar (vertical) represents two things

  1. the position of the visible text in the entire RichTextBox text
  2. the "size" of the visible text relative to the entire text

Both the position and size of the scroll bar are determined for you. I don't know of any way to override this behaviour.

program that contains a Rich Text Box & a scroll bar where scroll bar represents the size of font size of selected text in Rich Text Box as soon as user scrolls the scroll

Do you have two separate controls ( a RichTextBox and a Vertical Scrollbar)?

If this is the case then, you could something like this in the RTB's scroll event handler.

  Private Sub RichTextBox1_VScroll(ByVal sender As Object, ByVal e As System.EventArgs) Handles RichTextBox1.VScroll
     VScrollBar1.Size = New Size(VScrollBar1.Width, RichTextBox1.SelectionFont.Height)
  End Sub

Or do you mean that you want to control the size of the RTB's vertical scrollbar thumb? That can be done as well but is a bit more involved. Here is a custom RTB control that acomplishes setting the thumb size when the RTB receieves a VScroll message.

   Public Class RTBwithThumbHeight
         Inherits RichTextBox

         Private Const WM_VSCROLL As Int32 = &H115

         Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
            MyBase.WndProc(m) ' Process a normal

            If m.Msg = WM_VSCROLL Then
               ' get the size of the scrollbar.  Probably could just use the client window height instead
               Dim sbi As New SCROLLBARINFO
               sbi.cbSize = Marshal.SizeOf(sbi)
               Dim res As Boolean = GetScrollBarInfo(Me.Handle, OBJID.OBJID_VSCROLL, sbi)

               ' get the scroll bar scale values
               Dim si As New SCROLLINFO
               si.cbSize = CUInt(Marshal.SizeOf(si))
               si.fMask = ScrollInfoMask.SIF_ALL 'retrieve all values
               GetScrollInfo(Me.Handle, SBOrientation.SB_VERT, si)

               ' compute font line height scaled to scroll bar dimensions
               Dim scaleFactor As Double = (si.nMax - si.nMin) / ((sbi.rcScrollBar.bottom - - (2% * SystemInformation.VerticalScrollBarArrowHeight))
               Dim HeightThumb As Double = Me.SelectionFont.Height * scaleFactor

               ' reset mask to only change the nPage value, this controls the SB Thumb size
               si.fMask = ScrollInfoMask.SIF_PAGE
               si.nPage = CUInt(HeightThumb)

               ' change scrollbar parameters and redraw it
               Dim currentthumbPos As Int32 = SetScrollInfo(Me.Handle, SBOrientation.SB_VERT, si, True)
            End If
         End Sub

         <StructLayout(LayoutKind.Sequential)> _
         Structure SCROLLINFO
             Public cbSize As UInt32
             <MarshalAs(UnmanagedType.U4)> Public fMask As ScrollInfoMask
             Public nMin As Int32
             Public nMax As Int32
             Public nPage As UInt32
             Public nPos As Int32
             Public nTrackPos As Int32
         End Structure

         Public Enum ScrollInfoMask As UInteger
             SIF_RANGE = &H1
             SIF_PAGE = &H2
             SIF_POS = &H4
             SIF_DISABLENOSCROLL = &H8
             SIF_TRACKPOS = &H10
         End Enum

         Public Enum SBOrientation As Int32
              SB_HORZ = &H0
              SB_VERT = &H1
              SB_CTL = &H2
              SB_BOTH = &H3
         End Enum

         <DllImport("user32.dll", SetLastError:=True)> _
         Public Shared Function GetScrollInfo(ByVal hWnd As IntPtr, _
                 <MarshalAs(UnmanagedType.I4)> ByVal fnBar As SBOrientation, _
                 <MarshalAs(UnmanagedType.Struct)> ByRef lpsi As SCROLLINFO) As Int32
         End Function

         <DllImport("user32.dll", SetLastError:=True, ThrowOnUnmappableChar:=True, CharSet:=CharSet.Auto)> _
         Public Shared Function SetScrollInfo(ByVal hWnd As IntPtr, _
                   <MarshalAs(UnmanagedType.I4)> ByVal nBar As SBOrientation, _
                   <MarshalAs(UnmanagedType.Struct)> ByRef lpsi As SCROLLINFO, _
                   <MarshalAs(UnmanagedType.Bool)> ByVal bRepaint As Boolean) As Int32
         End Function

         <DllImport("user32.dll", SetLastError:=True, EntryPoint:="GetScrollBarInfo")> _
         Private Shared Function GetScrollBarInfo(ByVal hWnd As IntPtr, ByVal idObject As OBJID, ByRef psbi As SCROLLBARINFO) As Boolean
         End Function

         Public Enum OBJID As UInt32
         End Enum

         <StructLayout(LayoutKind.Sequential)> _
         Public Structure SCROLLBARINFO
             Public cbSize As Int32
             Public rcScrollBar As RECT
             Public dxyLineButton As Int32
             Public xyThumbTop As Int32
             Public xyThumbBottom As Int32
             Public reserved As Int32
             <MarshalAs(UnmanagedType.ByValArray, SizeConst:=6)> _
             Public rgstate() As Int32
         End Structure

         Public Structure RECT
             Public left As Int32
             Public top As Int32
             Public right As Int32
             Public bottom As Int32
         End Structure
      End Class
Be a part of the DaniWeb community

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