Calculating the maximum number of visible characters in a single RichTextBox line

2 posts, 0 answers
  1. Erik
    Erik avatar
    1 posts
    Member since:
    Jul 2014

    Posted 12 Sep 2014 Link to this post

    I am trying to create a fixed 'character ' based ruler using the RichTextBox control.   The RTB control
    will only contain one line of text that will not wrap, thus I am looking for a way to programmatically calculated the maximum number of visible characters in a RTB control based on the RTB width and the style (monospaced).

    MSDN example:

    Also is there a way to turn off the RTB scrollbars?



  2. Dess
    Dess avatar
    2423 posts

    Posted 17 Sep 2014 Link to this post

    Hello Erik,

    Thank you for writing.

    I am not sure if the RadRichTextBox is the appropriate control for your specific case. You can use the RadTextBox instead. Thus, you will be able to display a single row plain text and no scroll bars will be displayed. I would like to note that the Graphics.MeasureString method is suitable to calculate the exact SizeF required for a specific string. However, when you have a specific width, you can not determine a definite maximum number of characters that can fit in the given width because of the specificity of each character. For example in width X you can fit 10 characters 'w' or 20 characters of 'i' just because 'w' is much wider than 'i'. You can just use the SizeF result returned by the Graphics.MeasureString method in order to determine whether the currently available string needs bigger width than the control's width.

    As to the scroll bars question, you can turn off the vertical RadRichTextBox's scroll bar by manipulating the RadRichTextBox.DocumentView.Scroller.VScrollState property.

    I hope this information helps. Should you have further questions, I would be glad to help.

    Check out Telerik Analytics, the service which allows developers to discover app usage patterns, analyze user data, log exceptions, solve problems and profile application performance at run time. Watch the videos and start improving your app based on facts, not hunches.
Back to Top