Solved

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

Posted on 1998-06-15
6
158 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
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
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: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

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

Title # Comments Views Activity
Window placement 17 74
Updates not working for MS Windows 7 12 152
VBS file using code from 2nd file (txt or vbs) 4 30
Access query that references subform 5 43
Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
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.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

914 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

12 Experts available now in Live!

Get 1:1 Help Now