Solved

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

Posted on 1998-06-15
6
157 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
Comment Utility
Dim szStr as string
SendMessage(text1.hwnd, EM_GETLINE, 2, szStr)
0
 

Author Comment

by:oreg
Comment Utility
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
Comment Utility
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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 

Author Comment

by:oreg
Comment Utility
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
Comment Utility
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
Comment Utility
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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
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…
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…

743 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

17 Experts available now in Live!

Get 1:1 Help Now