Solved

How do I find the range of characters visible in RichTextBox

Posted on 2001-06-29
7
261 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
  • 4
  • 2
7 Comments
 
LVL 27

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 27

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 27

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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 27

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
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…

863 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

26 Experts available now in Live!

Get 1:1 Help Now