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 bar.help????
ajitjhajjar 0 Newbie Poster
Dani AI
Generated
Short summary and practical guidance. The phrase "scroll bar represents the size of font size" can mean two different UIs: either a separate slider/scrollbar whose thumb or value maps to the selected text's font size, or changing the RichTextBox's built‑in vertical scrollbar thumb to reflect font size. As pointed out, the native RTB scrollbar is designed to represent the viewport (position/visible page), not an arbitrary metric. shows two routes: bind a separate control to the font size (simple), or dive into native message/API work to change the control's thumb (advanced).
Recommended (robust) approach: use a separate TrackBar or VScrollBar as a font‑size control and synchronize it with selection changes. Key points:
- Present and store font sizes in points (8..72 is common). Update the slider when selection changes; if the selection spans multiple sizes
SelectionFontwill be null — treat that as a mixed state (disable the control, show "mixed", or pick a default). - When the slider moves, apply a new Font with the chosen point size to the selection/caret. Keep UI updates batched to avoid flicker.
- Map between points and pixels only when needed (display or custom layout). Example conversion:
' points -> pixels (use current DPI) pixels = points * (graphics.DpiY / 72.0F)
Cautions about modifying the native RTB scrollbar: it is achievable via low‑level message handling and platform APIs, but it is fragile — dependent on RichEdit version, theming, DPI, and can introduce redraw/reentrancy problems and accessibility regressions. If the goal is clearer user feedback, consider an overlay indicator, a floating tooltip, or a dedicated slider instead of altering the built‑in thumb.
Practical extras: prefer TrackBar semantics for changing a numeric property, keep min/max configurable, handle caret movement and SelectionChanged events to keep the UI in sync, and batch updates (suspend redraw) during programmatic changes to avoid flicker and excessive undo entries.
Recommended Answers
Jump to Post— Reverend Jim 6,665If 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
- the position of the visible text in the entire RichTextBox text
- the "size" of the visible text …
All 3 Replies
Reverend Jim 6,665 Hi, I'm Jim, one of DaniWeb's moderators. Moderator Featured Poster
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
- the position of the visible text in the entire RichTextBox text
- 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.
TnTinMN 418 Practically a Master Poster
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 bar.help????
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 - sbi.rcScrollBar.top) - (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
SIF_ALL = (SIF_RANGE Or SIF_PAGE Or SIF_POS Or SIF_TRACKPOS)
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
OBJID_HSCROLL = &HFFFFFFFAL
OBJID_VSCROLL = &HFFFFFFFBL
OBJID_CLIENT = &HFFFFFFFCL
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
Reverend Jim 6,665 Hi, I'm Jim, one of DaniWeb's moderators. Moderator Featured Poster
We're into expert territory here. One endorsement coming up.
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.