Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How do I find the range of characters visible in RichTextBox

Posted on 2001-06-29
7
Medium Priority
?
273 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 800 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

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

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

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

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
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

721 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