[RESOLVED] Find line on multiline textbox
I have PokerLast.text 3 lines of text.
I need a function that will find the line with "Your cards" and return that full line. not any other lines.
text1.text = GetLine(PokerLast.text, "Your cards")
and text1.text will now display that line. I asked somthing before to get the "lineafter" but now i need to return the current line?
Re: Find line on multiline textbox
Code:
Private Function GetLine(sTxt As String, sFind As String) As String
Dim s() As String
Dim i As Integer
s = Split(sTxt, vbCrLf)
For i = 0 To UBound(s)
If InStr(1, s(i), sFind) > 0 Then ' Case sensitive find
GetLine = s(i)
Exit For
End If
Next i
End Function
Re: Find line on multiline textbox
Thanks! that worked 1st time perfectly. I love this fourm!
Re: Find line on multiline textbox
Code:
Option Explicit
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 Function SendMessageStr Lib "user32" Alias "SendMessageA" _
(ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As String) As Long
Private Const EM_GETLINECOUNT = &HBA
Private Const EM_LINELENGTH = &HC1
Private Const EM_LINEINDEX = &HBB
Private Const EM_GETLINE = &HC4
Private Sub Command1_Click()
Dim lngCount As Long
Dim lngLineIndex As Long
Dim lngLength As Long
Dim strBuffer As String
Dim lngIndex As Long
'Get the line count
lngCount = SendMessage(Text1.hwnd, EM_GETLINECOUNT, 0, 0)
With Text1
For lngIndex = 0 To lngCount - 1
'Get line index of the chosen line
lngLineIndex = SendMessage(.hwnd, EM_LINEINDEX, lngIndex, 0)
'get line length
lngLength = SendMessage(.hwnd, EM_LINELENGTH, lngLineIndex, 0)
' 'resize buffer
strBuffer = Space(lngLength)
' 'get line text
Call SendMessageStr(.hwnd, EM_GETLINE, lngIndex, ByVal strBuffer)
If InStr(1, strBuffer, "your cards") Then
MsgBox strBuffer
Exit For
End If
Next
End With
End Sub
Re: Find line on multiline textbox
Now that we've helped you, you can help us by pulling down the Thread Tools menu and selecting the Mark Thread Resolved item which will let everyone know that you have your answer. Also if someone has been particularly helpful you have the ability to affect a their forum "reputation" by rating their post. Only those ratings that you give after you have 20 posts will actually count, but in all cases the person you rate will see it and know that you appreciate their help.
Re: Find line on multiline textbox
Nice one Martin. I have always preferred "SendMessage" over "split". SendMessage lets you interact like no other...
Re: Find line on multiline textbox
Quote:
Originally Posted by koolsid
Nice one Martin. I have always preferred "SendMessage" over "split". SendMessage lets you interact like no other...
That is actually a bad example for what I think the OP wants to do, For example if the text is wrapped in the text box you won't get the full text result back or it may not even find the text you are after if the search words just so happen to be split (wrapped) on separate lines.
Re: Find line on multiline textbox
No Edge, It is not a bad example :)
I have just posted to similar queries today... and sendmessage is a very "professional" way of handling textbox, RTB lines of text...
Re: Find line on multiline textbox
Quote:
Originally Posted by koolsid
No Edge, It is not a bad example :)
I have just posted to similar queries today... and sendmessage is a very "professional" way of handling textbox, RTB lines of text...
But if the words you are searching for are broken/wrapped on separate lines then the code above will never find them.
Re: Find line on multiline textbox
I am not sure if I understand... can you post an example?
Re: Find line on multiline textbox
Re: Find line on multiline textbox
Naturally "are wrapped" are on two different line.
you can use sendmessage to check if a string is a particular line like OP wanted
Quote:
I need a function that will find the line with "Your cards" and return that full line. not any other lines.
If it is not in one line then it will not be found.
If OP's query is to check if "Your cards" is in Textbox's text then I understand that you shouldn't be using sendmessage but that is not the case...