Solved

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

Posted on 1998-06-15
6
162 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
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

 

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
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…

738 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