Announcement

Collapse

Forum Guidelines

This forum is for finished source code that is working properly. If you have questions about this or any other source code, please post it in one of the Discussion Forums, not here.
See more
See less

Synchronized Textboxes

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Synchronized Textboxes

    Code:
    '¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤
    ' Example of synchronising textboxes via parent's WM_CTLCOLOREDIT
    ' Second textbox has got ES_READONLY style, just to show how that works
    '¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤
    #COMPILE EXE
    #INCLUDE "WIN32API.INC"
    %ID_TEXT     = 21
    %ID_TEXT2    = 22
    DECLARE CALLBACK FUNCTION DlgProc() AS LONG
     
    '¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤
    ' Create dialog and controls, etc
    '¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤
    FUNCTION PBMAIN () AS LONG
      LOCAL hDlg AS LONG, I AS LONG, txt AS STRING
     
      FOR I = 1 TO 50
         txt = txt & "Line" & STR$(I) & $CRLF
      NEXT
     
      DIALOG NEW 0, "Synchronized textboxes",,, 188, 155, %WS_CAPTION OR %WS_SYSMENU, 0 TO hDlg
     
      CONTROL ADD TEXTBOX, hDlg, %ID_TEXT, txt, 4, 4, 180, 60, _
                           %WS_CHILD OR %ES_MULTILINE OR %WS_VSCROLL OR %ES_NOHIDESEL OR %ES_WANTRETURN, _
                           %WS_EX_CLIENTEDGE CALL DlgProc
     
      CONTROL ADD TEXTBOX, hDlg, %ID_TEXT2, txt, 4, 74, 180, 60, _
                           %WS_CHILD OR %ES_MULTILINE OR %WS_VSCROLL OR %ES_NOHIDESEL OR %ES_READONLY, _
                           %WS_EX_CLIENTEDGE CALL DlgProc
     
      CONTROL ADD BUTTON, hDlg, 10, "E&xit", 135, 138, 50, 14
      CONTROL SET FOCUS hDlg, %ID_TEXT
     
      DIALOG SHOW MODAL hDlg CALL DlgProc
    END FUNCTION
     
    '¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤
    ' Main callback
    '¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤
    CALLBACK FUNCTION DlgProc() AS LONG
      STATIC NoUpdate AS LONG 'to avoid un-neccessay updating
     
      SELECT CASE CBMSG
         CASE %WM_CTLCOLOREDIT, %WM_CTLCOLORSTATIC
            IF CBMSG = %WM_CTLCOLORSTATIC THEN            '<- to give readonly textbox proper window color
               SetBkColor CBWPARAM, GetSysColor(%COLOR_WINDOW)
               FUNCTION = GetSysColorBrush(%COLOR_WINDOW)  '<- instead of dull gray..
            END IF
     
            IF NoUpdate THEN EXIT FUNCTION
            NoUpdate = %TRUE
     
            LOCAL hEdit AS LONG, hEdit2 AS LONG, fLine AS LONG, fLine2 AS LONG, _
                  x AS LONG, x2 AS LONG, txt AS STRING, txt2 AS STRING
     
            hEdit = GetDlgItem(CBHNDL, %ID_TEXT)
            hEdit2 = GetDlgItem(CBHNDL, %ID_TEXT2)
            IF CBLPARAM = hEdit2 THEN SWAP hEdit, hEdit2      'hEdit is the one with focus..
     
            'SYNCRONIZE TEXT (if you need, otherwise rem out)
            'Note: %ES_READONLY style in textbox 2 only prevents direct editing.
            '%WM_SETTEXT still works for changing readonly contents.
            CONTROL GET TEXT CBHNDL, %ID_TEXT TO txt
            CONTROL GET TEXT CBHNDL, %ID_TEXT2 TO txt2
            IF txt <> txt2 THEN SendMessage hEdit2, %WM_SETTEXT, 0, BYVAL STRPTR(txt)
     
            'SYNCRONIZE SELECTION (if you need, otherwise rem out)
            'NOTE: Windows only has one caret, so only control with focus gets to show it.
            fLine  = SendMessage(hEdit, %EM_GETSEL, VARPTR(x), VARPTR(x2))   '<- get selection
            fLine  = SendMessage(hEdit2, %EM_SETSEL, x, x2)   '<- set selection
     
            'SYNCRONIZE FIRST VISIBLE LINE (if you need, otherwise rem out)
            fLine  = SendMessage(hEdit, %EM_GETFIRSTVISIBLELINE, 0, 0)   '<- get first visible line in 1
            fLine2 = SendMessage(hEdit2, %EM_GETFIRSTVISIBLELINE, 0, 0)  '<- get first visible line in 2
            IF fLine <> fLine2 THEN
               SendMessage hEdit2, %EM_LINESCROLL, 0, fLine - fLine2     '<- amount to scroll textbox 2
            END IF
     
            NoUpdate = 0
     
         CASE %WM_COMMAND
            IF CBCTL = 10 THEN DIALOG END CBHNDL
     
      END SELECT
    END FUNCTION

    ------------------
Working...
X