RichTextBox.GetCharIndexFromPosition returns wrong value

Posted on 2008-11-18
Last Modified: 2012-05-05
I have a RichTextBox in a Form that I populate line-by-line (due to formatting, etc.) from my Form_Load function.

While loading, I use GetCharIndexFromPosition function and if the RichTextBox height can accommodates all text lines then the above function always returns the correct CharIndex. If however the RichTextBox height can't accommodate all text lines then for lines past the end of viewable area the GetCharIndexFromPosition returns wrong CharIndex values and i get scrambled text.

As part of trying to resolve the problem I made the Form height (in IDE) fairly small and then in Form_Load function I first changed the height (to accommodate all text lines) and I still get the wrong outcome, i.e. all lines past the original Form size appear wrong. If I then reload the contents, after the form is shown with the new height, then I get the same (bad) result for all lines past the end of viewable are, same as previously. If I bypass that function and always use .TextLength (as I can while loading but not always) then everything is shown properly so I'm sure it's something related to that function that's causing the problem.

Any ideas as to what could be the problem?

Question by:gem56
    LVL 14

    Expert Comment

    I think the problem with the GetCharIndexFromPosition method is that it uses a Screen Point to know where to go.   Since lines below the visible area of the RichTextBox are not on the screen and invisible they do not correlate to a screen point.  Is it possible to Scroll the RichTextBox down as enough lines are added to go below view?   Maybe scrolling the last character into view may provide a point to reference to add more text.

    Author Comment

    I think you may be right about the Screen Point.

    To answer your question, at times it may be possible to scroll down but that would place too much of a limitation on what I'm trying to achieve.

    Accepted Solution

    hi jjardine,
    I think that I've resolved the problem and eve though I'm not seeing any visible problems I'm not absolutely sure if it has any other implications.

    In the function where I add a NewLine to the RichTextBox I also increase the height of the RichTextBox.

    Do you see any obvious implications?

      Dim pt As System.Drawing.Point = GetPositionFromCharIndex(.TextLength)
      If (.Height < pt.Y + 50) Then .Height = pt.Y + 50

    Open in new window


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Top 6 Sources for Identifying Threat Actor TTPs

    Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

    I'm currently working for a company where I have to upgrade over 50 VB6 programs to VB.NET 2008.  So far I'm about half way through, and I've learned quite a few tricks that drastically improve the performance of VB.NET apps. Because there are a…
    The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (…
    Hi everyone! This is Experts Exchange customer support.  This quick video will show you how to change your primary email address.  If you have any questions, then please Write a Comment below!
    This video discusses moving either the default database or any database to a new volume.

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    13 Experts available now in Live!

    Get 1:1 Help Now