Solved

I am lazy, 125 easy points :)  [line from a text box]

Posted on 1998-06-15
6
163 Views
Last Modified: 2010-05-03
I want a function (or api call, I believe there is one) to grab a specific line from a rich text box.

For example, you are happily typing along, and just to be annoying I wanted this program to pop up a message box whenever you pressed enter at the end of a line and display the line you just finished typing....

1 : This is the first line (enter)
2 : this is the second line (enter)
blah blah blah

After I type the first line "This is the first line" and then pressed enter, a message box would appear with the line I just finished typing "This is the first line" and my cursor would be sitting on the next line like normal, nothing has changed.

Wow, that was a mouthful.  I do not think this is two hard, but I am lazy and working on other things, so hopefully someone can get some easy points.  This is in VB5 Professional.

Thanks,
oreg
0
Comment
Question by:oreg
[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
6 Comments
 
LVL 8

Expert Comment

by:MikeP090797
ID: 1463345
Dim szStr as string
SendMessage(text1.hwnd, EM_GETLINE, 2, szStr)
0
 

Author Comment

by:oreg
ID: 1463346
Hi, that answer actually does nothing.  Nothing is saved in the
string variable...at least not when I tested it on a RichTextBox
(never tried textbox, but that is not what I want).
0
 

Expert Comment

by:DanielJenkins
ID: 1463347
Dim pos As Integer

Private Sub Form_Load()
   pos = 0
End Sub

Private Sub rtBox_KeyPress(KeyAscii As Integer)
   If KeyAscii = 13 Then
      msgbox Mid$(rtBox.Text, pos + 1, Len(rtBox.Text) - pos)
      pos = Len(rtBox.Text) + 2
   End If
End Sub

0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:oreg
ID: 1463348
This would work only in the specific case I gave above, the real task is to grab a line from the text box....any given line at a time...the example was just meant to show one possible use.

0
 
LVL 9

Accepted Solution

by:
Dalin earned 200 total points
ID: 1463349
oreg,
See if I undersatnd what you need: Let me know if you have questions
Regards
Dalin

'This routine grabs a line (you specify the line number) of a 'textbox

'Declare

          Const EM_GETLINE = &HC4
          Const EM_LINELENGTH = &HC1
          Const EM_LINEINDEX = &HBB

          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 Declare Sub RtlMoveMemory Lib "KERNEL32" (lpvDest As Any, lpvSource As Any, ByVal cbCopy As Long)


          Sub TB_GetLine(ByVal hWnd As Long, ByVal whichLine As Long, Line As String)
              Dim length As Long, bArr() As Byte, bArr2() As Byte, lc As Long

              lc = SendMessage(hWnd, EM_LINEINDEX, whichLine, ByVal 0&)
              length = SendMessage(hWnd, EM_LINELENGTH, lc, ByVal 0&)
              If length > 0 Then
                  ReDim bArr(length + 1) As Byte, bArr2(length - 1) As Byte
                  Call RtlMoveMemory(bArr(0), length, 2)
                  Call SendMessage(hWnd, EM_GETLINE, whichLine, bArr(0))
                  Call RtlMoveMemory(bArr2(0), bArr(0), length)
                  Line = StrConv(bArr2, vbUnicode)
              Else
                  Line = "" 
              End If
          End Sub

To read the 5th line and put in the string of lString

Dim lString As String
Call TB_GetLine( Text1.hWnd, 5, lString )

MsgBox "The 5th Line is " & lString, vbOKOnly, "Read a Line"
0
 

Author Comment

by:oreg
ID: 1463350
That code is amazing, I tried to break it but I couldn't :)  Thank you very much, I will throw in a few more points for you since this works so well.  If you have a chance later, could you briefly walk me through it (the API calls that is).

Thanks again,
oreg.
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

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 …
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 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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses
Course of the Month3 days, 17 hours 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