Date: 02-16-2022
Return to Index
created by gbSnippets
'The edit control, unlike a Rich Edit control, does not provide a FindText API. However, it
'can be implemented easily with the PowerBASIC INSTR command.
'Primary Code:
'Credit: Nathan Maddox (for suggested improvements)
Local temp$, iStartPos&, iResult&, search$
Control Get Text hDlg, %ID_Control to temp$
iStartPos& = Instr(temp$, search$)
If iStartPos&
iResult& = SendMessage(hEdit, %EM_SetSel, iStartPos&, iStartPos& + Len(Search$))
End If
'Compilable Example: (Jose Includes)
'This example finds, and highlights, occurrence of a text string within
'an edit control. It starts at the top and goes downward. When it finds
'no more matches, it sounds a Beep. Then the next search starts at the
'top again.
#Compiler PBWin 9, PBWin 10
#Compile EXE
#Dim All
%Unicode=1
#Include "Win32API.inc"
Global hDlg As DWord, hEdit As DWord
Function PBMain() As Long
Local style&, buf$
style& = %WS_Child Or %WS_Visible Or %ES_MultiLine Or %WS_VScroll Or %ES_AutoHScroll _
Or %WS_HScroll Or %ES_AutoVScroll Or %ES_WantReturn Or %WS_TabStop Or %ES_NoHideSel
buf$ = "This is sample text for the " + $Crlf + _
"edit control. " + $Crlf + "Any Text Could " + $Crlf + _
"be present in it " + $Crlf + "so this text " + $Crlf + _
"could be changed " + $Crlf + "to what you want. " + $Crlf + _
"I just happened to want this!"
Dialog New Pixels, 0, "TextBox Test",300,300,200,200, %WS_SysMenu, 0 To hDlg
Control Add Button, hDlg, 100,"Find/Find Again 't' ", 30,20,140,20
Control Add TextBox, hDlg, 200,buf$, 50,50,100,100, style&
Control Handle hDlg, 200 To hEdit
Dialog Show Modal hDlg Call DlgProc
End Function
CallBack Function DlgProc() As Long
Local temp$, iResult&, search$
Static iStartPos&
If iStartPos& < 1 Then iStartPos& = 1
If CB.Msg = %WM_Command AND CB.Ctl = 100 AND CB.Ctlmsg = %BN_Clicked Then
search$ = "t"
Control Get Text hDlg, 200 To temp$
iStartPos& = Instr(iStartPos&, temp$, search$)
If iStartPos& Then
iResult& = SendMessage(hEdit, %EM_SetSel, iStartPos&-1, iStartPos& + Len(Search$)-1) '+/-1 to convert to zero-based
iResult& = SendMessage(hEdit, %EM_SCROLLCARET, 0, 0)
iStartPos& = iStartPos& + Len(search$)
Else
Beep
End If
End If
End Function
'gbs_00130
'Date: 03-10-2012
http://www.garybeene.com/sw/gbsnippets.htm