Date: 02-16-2022
Return to Index
created by gbSnippets
'Primary Code:
'DDT method for textbox control
Control Send hDlg, %IDC_TextBox, %EM_SetSel, 0, -1
'API method for any edit control
SendMessage hEdit, %EM_SetSel, 0, -1
'When using API with Edit Controls, remember that line numbers and char positions
'are zero-based. Also, character positions include $crlf at end of previous lines.
'Compilable Example: (Jose Includes)
#Compiler PBWin 9, PBWin 10
#Compile Exe
#Dim All
%Unicode=1
#Include "Win32API.inc"
Global hDlg, hEdit As Dword
%IDC_TextBox = 500
%IDC_ButtonDDT = 501
%IDC_ButtonAPI = 502
Function PBMain() As Long
Local style&
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 %ES_NoHideSel Or %WS_TabStop
Dialog New Pixels, 0, "Select Line M",300,300,200,150, %WS_OverlappedWindow To hDlg
Control Add Button, hDlg, %IDC_ButtonDDT, "Select Line 2 (DDT)",20,10,160,20
Control Add Button, hDlg, %IDC_ButtonAPI, "Select Line 2 (API)",20,40,160,20
Control Add TextBox, hDlg, %IDC_TextBox, "Hello World"+$crlf+"Waiting ..."+$CrLf+"Goodbye World",20,70,160,80, style&, %WS_Ex_ClientEdge
Control Handle hDlg, %IDC_TextBox To hEdit
Dialog Show Modal hDlg Call DlgProc
End Function
CallBack Function DlgProc() As Long
Local i,StartPos, EndPos, LineNumber, LineLength As Long, temp$, tmp$
Select Case Cb.Msg
Case %WM_Command
Select Case Cb.Ctl
Case %IDC_ButtonDDT
'DDT method
LineNumber = 2 'Line M
Control Get Text hDlg, %IDC_TextBox to temp$
For i = 1 to LineNumber
tmp$ = Parse$(temp$, $crlf, i)
EndPos = EndPos + Len(tmp$)
Next i
EndPos = EndPos + (LineNumber-1)*2 '$crlf
StartPos = EndPos - Len(tmp$)
Control Send hDlg, %IDC_TextBox, %EM_SetSel, StartPos, EndPos 'select character range
Case %IDC_ButtonAPI
'API method
LineNumber = 1
StartPos = SendMessage(hEdit, %EM_LineIndex, LineNumber, 0) 'pos of 1st char in lineM
LineLength = SendMessage(hEdit, %EM_LineLength, StartPos, 0) 'length of line containing 1st char
SendMessage hEdit, %EM_SetSel, StartPos, StartPos + LineLength 'select character range
End Select
End Select
End Function
'gbs_01093
'Date: 03-10-2012
http://www.garybeene.com/sw/gbsnippets.htm