Solved

How do I find the range of characters visible in RichTextBox

Posted on 2001-06-29
7
269 Views
Last Modified: 2008-02-26
I need to know, at any given time, what is the range of characters currently VISIBLE in my RichTextBox window.
I don't want the user to have to select anything, or click anything except the scroll bars to view the file.

thanks,

russ
0
Comment
Question by:RUSS_EMI
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
7 Comments
 
LVL 28

Expert Comment

by:Ark
ID: 6240306
Hi
'====Bas module code===

Private Type POINTAPI
    X As Long
    Y As Long
End Type

Public Type CharRange
   cpMin          As Long
   cpMax          As Long
End Type

Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Private Const EM_CHARFROMPOS& = &HD7


Public Function GetCharrange(rtb As RichTextBox) As CharRange
   Dim pt As POINTAPI
   GetCharrange.cpMin = SendMessage(rtb.hwnd, EM_CHARFROMPOS, 0&, pt)
   pt.X = rtb.Width \ Screen.TwipsPerPixelX
   pt.Y = rtb.Height \ Screen.TwipsPerPixelY
   GetCharrange.cpMax = SendMessage(rtb.hwnd, EM_CHARFROMPOS, 0&, pt)
End Function

'====Form code===


Private Sub Command1_Click()
  Dim cr As CharRange
  cr = GetCharrange(RichTextBox1)
  MsgBox "Currently visible characters" & vbCrLf & "from " & cr.cpMin & " to " & cr.cpMax
End Sub


Cheers
0
 
LVL 28

Expert Comment

by:Ark
ID: 6240330
Hi
For some reason above function get one string more then contain in RichTextBox. Change:

pt.Y = rtb.Height \ Screen.TwipsPerPixelY - rtb.Font.Size * 1.4

Cheers
0
 
LVL 28

Accepted Solution

by:
Ark earned 200 total points
ID: 6240334
OK, more correct code:

Private Type POINTAPI
    X As Long
    Y As Long
End Type

Public Type CharRange
   cpMin          As Long
   cpMax          As Long
End Type

Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Private Const EM_CHARFROMPOS& = &HD7
Const EM_LINEINDEX = &HBB
Const EM_LINELENGTH = &HC1
Const WM_USER = &H400
Const EM_EXLINEFROMCHAR = (WM_USER + 54)

Public Function GetCharrange(rtb As RichTextBox) As CharRange
   Dim pt As POINTAPI
   Dim pos As Long
   Dim ret As Long
   GetCharrange.cpMin = SendMessage(rtb.hwnd, EM_CHARFROMPOS, 0&, pt)
   pt.X = rtb.Width \ Screen.TwipsPerPixelX
   pt.Y = rtb.Height \ Screen.TwipsPerPixelY
   pos = SendMessage(rtb.hwnd, EM_CHARFROMPOS, 0&, pt)
   ret = SendMessage(rtb.hwnd, EM_EXLINEFROMCHAR, 0, ByVal pos)
   ret = SendMessage(rtb.hwnd, EM_LINEINDEX, ret, ByVal 0&)
   ret = SendMessage(rtb.hwnd, EM_LINELENGTH, ret, ByVal 0&)
   GetCharrange.cpMax = pos - ret
End Function

Cheers
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:RUSS_EMI
ID: 6240845
Ark,
   Thank you for your information.  Just a few specifics before I close this out and give you the 200 points.

The complete project is to have an .mp3 while a certain portion of text is visible, and stop when it's not.

If you have any ideas regarding this specifically, please share them.  I will increase the points, if necessary.

Russ
0
 
LVL 4

Expert Comment

by:amit_panjwani
ID: 6240989
watching ...
0
 

Author Comment

by:RUSS_EMI
ID: 6241188
Thanks, I got the rest by experimentation.

Russ
0
 
LVL 28

Expert Comment

by:Ark
ID: 6241567
Thanks for points, glad I could help you.
You can use InStr function to determine Special text position and compare it with cr.cpMin and cr.cpMax.
Or, reverse, you can get visible text:
rtb.Selstrat = cr.cpMin
rtb.SelLength = cr.cpMax - cr.cpMin
sTemp = rtb.SelText
and compare sTemp with sSpecText

Cheers

0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Suggested Courses
Course of the Month4 days, 1 hour left to enroll

630 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