Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

How do I find the range of characters visible in RichTextBox

Posted on 2001-06-29
7
Medium Priority
?
276 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 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

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

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.

Question has a verified solution.

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

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses

783 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