Basic 2: Replace Text: All, Selection, Current Line, LineM, LinesM-N, CharsM-N

Category: Controls - RichEdit

Date: 02-16-2022

Return to Index


 
'This code shows how to replace various combination of text - all text, the
'selection, the current line, the current line, line M, or lines M-N.
 
'Both DDT and direct API solutions are provided.
 
'Compiler Comments:
'This code is written to compile with PBWin10. To compile with PBWin9,
'add this line:
#Include "CommCtrl.inc"
 
'Primary Code:
'Because of size, only a single copy of the primary code is shown, as part
'of the compilable example below.  All code is shown as Functions.
 
'But, in summary, here are the basic statements used. These can be used
'individually to get information, whereas the Functions in the compilable example
'below return several pieces of information with each call.
 
'DDT
   Control Send hDlg, %ID_RichEdit, %EM_LineIndex, n&, 0 To P.cpmax               'position of 1st char in line#
   Control Send hDlg, %ID_RichEdit, %EM_LineLength, iCharPos&, 0 to iLineLength&  'length of line starting at that char position
   Control Send hDlg, %ID_RichEdit, %EM_EXLineFromChar, 0,-1 To iCurrentLine&     'current line# / 1st line of selection
   Control Send hDlg, %ID_RichEdit, %EM_EXSetSel, 0, VarPTR(P) To iResult&        'select specified text (P.cpmin-P.cpmax)
'API
   SendMessage(hRichEdit, %EM_LineIndex, iCurrentLine&, 0)  'position of 1st char in line#
   SendMessage(hRichEdit, %EM_LineLength, iCharPos&, 0)     'length of line starting at that char position
   SendMessage(hRichEdit, %EM_EXLineFromChar, 0,-1)         'current line# / 1st line of selection
   SendMessage(hRichEdit, %EM_EXSetSel, 0, VarPTR(P))       'select specified text (P.cpmin-P.cpmax)
 
'Compilable Example:  (Jose Includes)
'This example shows how to select various combination of text - all text, the
'current line, the current line, line M, or lines M-N.  Line numbers start at 0.
#Compiler PBWin 10
#Compile EXE
#Dim All
%Unicode=1
#Include "win32api.inc
#Include "RichEdit.inc"
Global hDlg as DWord, hRichEdit as DWord
%ID_RichEdit = 500
 
Function PBMain() As Long
   Local style&, buf$
   buf$ =  "This is sample" + $CrLf + "text for the" + $CrLf + "edit control."
   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, "Test Code",300,300,200,330, %WS_OverlappedWindow To hDlg
   LoadLibrary("riched32.dll") : InitCommonControls
   Control Add "RichEdit", hDlg, %ID_RichEdit, buf$,20,10,160,50, style&, %WS_EX_ClientEdge
   Control Handle hDlg, %ID_RichEdit To hRichEdit
   Control Add Button, hDlg, 100,"All DDT", 10,70,80,20 : Control Add Button, hDlg, 180,"Lines 0-1 DDT", 100,70,90,20
   Control Add Button, hDlg, 110,"All API", 10,100,80,20 : Control Add Button, hDlg, 190,"Lines 0-1 API", 100,100,90,20
   Control Add Button, hDlg, 120,"Sel DDT", 10,130,80,20 : Control Add Button, hDlg, 200,"Chars 11-11 DDT", 100,130,90,20
   Control Add Button, hDlg, 130,"Sel API", 10,160,80,20 : Control Add Button, hDlg, 210,"Chars 11-11 API", 100,160,90,20
   Control Add Button, hDlg, 140,"Current Ln DDT", 10,190,80,20 : Control Add Button, hDlg, 220,"Chars 10-20 DDT", 100,190,90,20
   Control Add Button, hDlg, 150,"Current Ln API", 10,220,80,20 : Control Add Button, hDlg, 230,"Chars 10-20 API", 100,220,90,20
   Control Add Button, hDlg, 160,"Line 1 DDT", 10,250,80,20
   Control Add Button, hDlg, 170,"Line 1 API", 10,280,80,20 :    Control Add Button, hDlg, 240,"Restore", 100,280,80,20
   Dialog Show Modal hDlg Call DlgProc
End Function
 
CallBack Function DlgProc() As Long
   If CB.Msg = %WM_Command AND CB.Ctl = 100 Then ReplaceText_AllText_DDT "all text replaced"
   If CB.Msg = %WM_Command AND CB.Ctl = 110 Then ReplaceText_AllText_API "all text replaced"
   If CB.Msg = %WM_Command AND CB.Ctl = 120 Then ReplaceText_Selection_DDT "selection replaced"
   If CB.Msg = %WM_Command AND CB.Ctl = 130 Then ReplaceText_Selection_API "selection replaced"
   If CB.Msg = %WM_Command AND CB.Ctl = 140 Then ReplaceText_CurrentLine_DDT "current line"
   If CB.Msg = %WM_Command AND CB.Ctl = 150 Then ReplaceText_CurrentLine_API "current line"
   If CB.Msg = %WM_Command AND CB.Ctl = 160 Then ReplaceText_LinesMN_DDT 1,1, "Line 1 replaced"
   If CB.Msg = %WM_Command AND CB.Ctl = 170 Then ReplaceText_LinesMN_API 1,1, "Line 1 replaced"
   If CB.Msg = %WM_Command AND CB.Ctl = 180 Then ReplaceText_LinesMN_DDT 0,1, "Lines 0,1 replaced"
   If CB.Msg = %WM_Command AND CB.Ctl = 190 Then ReplaceText_LinesMN_API 0,1, "Lines 0,1 replaced"
   If CB.Msg = %WM_Command AND CB.Ctl = 200 Then ReplaceText_CharsMN_DDT 11,11, "XX"
   If CB.Msg = %WM_Command AND CB.Ctl = 210 Then ReplaceText_CharsMN_API 11,11, "XX"
   If CB.Msg = %WM_Command AND CB.Ctl = 220 Then ReplaceText_CharsMN_DDT 10,20, "Chars 10,20 replaced"
   If CB.Msg = %WM_Command AND CB.Ctl = 230 Then ReplaceText_CharsMN_API 10,20, "Chars 10,20 replaced"
   If CB.Msg = %WM_Command AND CB.Ctl = 240 Then Control Set Text hDlg, %ID_RichEdit, "This is sample" + $CrLf + "text for the" + $CrLf + "edit control."
End Function
   'All
   'DDT
 
Function ReplaceText_AllText_DDT(ByVal buf$) as Long
   Control Set Text hDlg, %ID_RichEdit, buf$
End Function
 
   'API
 
Function ReplaceText_AllText_API(ByVal buf$) as Long
   Function = SendMessage (hRichEdit, %WM_SetText, 0, StrPTR(buf$))
End Function
 
   'Selection
   'DDT
 
Function ReplaceText_Selection_DDT(ByVal buf$) as Long
   Local iResult&, P as CharRange, temp$
   Control Send hDlg, %ID_RichEdit, %EM_EXGetSel, 0, VarPTR(P)    'start/stop of selection, caret position if no selection
   Control Get Text hDlg, %ID_RichEdit TO temp$
   Control Set Text hDlg, %ID_RichEdit, Left$(temp$,P.cpmin) + buf$ + Mid$(temp$, P.cpmax + 1)
End Function
 
   'API
 
Function ReplaceText_Selection_API(ByVal buf$) as Long
   Local iResult&, P as CharRange
   iResult& = SendMessage(hRichEdit, %EM_EXGetSel, 0, VarPTR(P))    'start/stop of selection, caret position if no selection
   If P.cpmin <> P.cpmax Then
      Function = SendMessage(hRichEdit, %EM_ReplaceSel, %True, StrPTR(buf$))   'only replace if selection exists.
   End If
End Function
 
   'Current Line
   'DDT
 
Function ReplaceText_CurrentLine_DDT(ByVal buf$) as Long
   Local iCurrentLine&, iCharPos&, iLineLength&, iResult&, P as CharRange, temp$
   Control Send hDlg, %ID_RichEdit, %EM_LineFromChar, -1,0 To iCurrentLine&             'current line / 1st line of selection
   Control Send hDlg, %ID_RichEdit, %EM_LineIndex, iCurrentLine&, 0 TO iCharPos&            'position of 1st char in line iResult&
   Control Send hDlg, %ID_RichEdit, %EM_LineLength, iCharPos&, 0 TO iLineLength&   'length of line starting at that char position
   Control Get Text hDlg, %ID_RichEdit TO temp$
   Control Set Text hDlg, %ID_RichEdit, Left$(temp$,iCharPos&) + buf$ + Mid$(temp$, iCharPos& + iLineLength& + 1)
End Function
   'API
 
Function ReplaceText_CurrentLine_API(ByVal buf$) as Long
   Local iCurrentLine&, iCharPos&, iLineLength&, iResult&, P as CharRange
   iCurrentLine& = SendMessage(hRichEdit, %EM_LineFromChar, -1,0)        'current line / 1st line of selection
   iCharPos& = SendMessage(hRichEdit, %EM_LineIndex, iCurrentLine&, 0)   'position of 1st char in line iResult&
   iLineLength& = SendMessage(hRichEdit, %EM_LineLength, iCharPos&, 0)   'length of line starting at that char position
   P.cpmin = iCharPos& : P.cpmax = iCharPos& + iLineLength&
   iResult& = SendMessage(hRichEdit, %EM_EXSetSel, 0, VarPTR(P))
   Function = SendMessage(hRichEdit, %EM_ReplaceSel, %True,StrPTR(buf$))   'only replace if selection exists.
End Function
 
   'Lines M, M-N
   'DDT
 
Function ReplaceText_LinesMN_DDT(ByVal m&, ByVal n&, ByVal buf$) as Long
   Local iStartPos&, iStopPos&, iLineLength&, iResult&, temp$, P as CharRange
   Control Send hDlg, %ID_RichEdit, %EM_LineIndex, m&, 0 TO iStartPos&   'position of 1st char in start line
   Control Send hDlg, %ID_RichEdit, %EM_LineIndex, n&, 0 TO iStopPos&      'position of 1st char in ending line
   Control Send hDlg, %ID_RichEdit, %EM_LineLength, iStopPos&, 0 TO iLineLength&   'length of last line
   Control Get Text hDlg, %ID_RichEdit TO temp$
   Control Set Text hDlg, %ID_RichEdit, Left$(temp$,iStartPos&) + buf$ + Mid$(temp$, iStopPos + iLineLength& + 1)
End Function
 
   'API
 
Function ReplaceText_LinesMN_API(ByVal m&, ByVal n&, ByVal buf$) as Long
   Local iStartPos&, iStopPos&, iLineLength&, iResult&, P as CharRange
   iStartPos& = SendMessage(hRichEdit, %EM_LineIndex, m&, 0)   'position of 1st char in start line
   iStopPos& = SendMessage(hRichEdit, %EM_LineIndex, n&, 0)      'position of 1st char in ending line
   iLineLength& = SendMessage(hRichEdit, %EM_LineLength, iStopPos&, 0)   'length of last line
   iStopPos& = iStopPos& + iLineLength&
   P.cpmin = iStartPos& : P.cpmax = iStopPos&
   iResult& = SendMessage(hRichEdit, %EM_EXSetSel, 0, VarPTR(P))     'select the text you want to replace
   Function = SendMessage(hRichEdit, %EM_ReplaceSel, %True,StrPTR(buf$))   'only replace if selection exists.
End Function
 
   'Chars M, M-N
   'DDT
 
Function ReplaceText_CharsMN_DDT(ByVal m&, ByVal n&, ByVal buf$) as Long
   Local temp$
   Control Get Text hDlg, %ID_RichEdit TO temp$
   Control Set Text hDlg, %ID_RichEdit, Left$(temp$,m&) + buf$ + Mid$(temp$, n&+2)
End Function
 
   'API
 
Function ReplaceText_CharsMN_API(ByVal m&, ByVal n&, ByVal buf$) as Long
   Local iResult&, P as CharRange
   P.cpmin = m& : P.cpmax = n& + 1
   iResult& = SendMessage(hRichEdit, %EM_EXSetSel, 0, VarPTR(P))
   Function = SendMessage(hRichEdit, %EM_ReplaceSel, %True,StrPTR(buf$))
End Function
 
'gbs_00222
'Date: 03-10-2012


created by gbSnippets
http://www.garybeene.com/sw/gbsnippets.htm