Announcement

Collapse
No announcement yet.

How to prevent type of certain character

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

  • How to prevent type of certain character

    hi,

    i would like to know how i can prevent my user from typing in a
    certain character (like '=' or '&') in a text box.

    if read the discussion http://www.powerbasic.com/support/pb...ad.php?t=13978
    but somehow i feel like this is overkill for this wish.

    tia

    ------------------

  • #2
    with DDT you will need to subclass the edit control.

    Look at Winapi SetWindowLong

    ------------------
    -Greg
    -Greg
    [email protected]
    MCP,MCSA,MCSE,MCSD

    Comment


    • #3
      Classic superclass
      Code:
         #Compile Exe
         #Register None
         #Dim All
         #Include "WIN32API.INC"
      
         CallBack Function SuperEditProc
            Static OldProc As Long, OffsetWndExtra As Long
            If CbHndl = 0 Then OldProc = CbWparam: OffsetWndExtra = CbLparam: Exit Function
            Select Case CbMsg
               Case %WM_CHAR
                  Select Case CbWparam
                     Case Asc("="), Asc("&"):
                        MessageBox GetParent(CbHndl), "Guy, you need to visit a hospital", "Very importantant notice", _
                           %MB_TASKMODAL Or %MB_ICONEXCLAMATION: Exit Function
                  End Select
            End Select
            Function = CallWindowProc(OldProc, CbHndl, CbMsg, CbWparam, CbLparam)
         End Function
      
         Function CreateSuperClass(OldClassName As String, NewClassName As String, lpfnNewWndProc As Long, cbWndExtra As Long) As Long
            Local wc As WNDCLASSEX
            wc.cbSize = SizeOf(wc)
            If GetClassInfoEx(ByVal 0&, ByVal StrPtr(OldClassName), wc) Then
               CallWindowProc lpfnNewWndProc, 0, 0, wc.lpfnWndProc, wc.cbWndExtra
               wc.hInstance = GetModuleHandle(ByVal 0&)
               wc.lpszClassName = StrPtr(NewClassName)
               wc.lpfnWndProc = lpfnNewWndProc
               wc.cbWndExtra = wc.cbWndExtra + cbWndExtra
               Function = RegisterClassEx(wc)
            End If
         End Function
      
         Function PbMain
            If IsFalse(CreateSuperClass("EDIT", "SuperEdit", CodePtr(SuperEditProc), 4)) Then Exit Function
      
            Local hDlg As Long
            Dialog New 0, "Test", , , 120, 80, %WS_CAPTION Or %WS_SYSMENU Or %WS_MAXIMIZEBOX To hDlg
            Control Add "SuperEdit", hDlg, 101, "", 10, 10, 100, 60, %WS_CHILD Or %WS_VISIBLE Or %WS_TABSTOP _
               Or %ES_MULTILINE Or %ES_WANTRETURN, %WS_EX_CLIENTEDGE
            Dialog Show Modal hDlg
      
         End Function
      ------------------
      E-MAIL: [email protected]

      Comment


      • #4
        for a compact ddt instance-subclassing example, see

        it should be pretty easy to eliminate unwanted characters by swallowing the appropriate %wm_char message(s). basically just do not pass the %wm_char message on to callwindowproc().

        ------------------
        lance
        powerbasic support
        mailto:[email protected][email protected]</a>
        Lance
        mailto:[email protected]

        Comment


        • #5
          Here's another more direct way.

          Code:
          #COMPILE EXE
          #INCLUDE "win32api.inc"
          
          GLOBAL DDTProc&
          %TEXT1 = 100
          CALLBACK FUNCTION DlgProc()
          
          END FUNCTION
          
          FUNCTION SubClassTextBox(BYVAL hWnd&, BYVAL wMsg&, BYVAL wParam&, BYVAL lParam&) AS LONG
                SELECT CASE wMsg&
                  CASE %WM_CHAR
                       SELECT CASE wParam&
                          CASE ASC("&"), ASC("%"), 32 '*Space
                               BEEP: EXIT FUNCTION
                       END SELECT
                END SELECT
                 FUNCTION = CallWindowProc(DDTProc&, hWnd&, wMsg&, wParam&, lParam&)
          END FUNCTION
          
          
          FUNCTION PBMAIN() AS LONG
              DIALOG NEW 0, "Text Box Subclass Example", -1, -1, 150, 150, %WS_SYSMENU TO hDlg&
              CONTROL ADD TEXTBOX, hDlg&, %TEXT1, "", 0, 0, 150, 150, %ES_MULTILINE OR %ES_WANTRETURN
              DIALOG SHOW MODELESS hDlg& CALL dlgProc
              DDTProc& = SetWindowLong(GetDlgItem(hDlg&, %TEXT1), %GWL_WNDPROC, CODEPTR(SubClassTextBox))
              DO
                  DIALOG DOEVENTS TO FormsCount&
              LOOP UNTIL FormsCount& = 0
          END FUNCTION
          ------------------
          -Greg
          -Greg
          [email protected]
          MCP,MCSA,MCSE,MCSD

          Comment


          • #6
            Greg, that is instance-subclassing too.

            ------------------
            Lance
            PowerBASIC Support
            mailto:[email protected][email protected]</A>
            Lance
            mailto:[email protected]

            Comment


            • #7
              Lance,

              Sorry about that, I wrote the code right after my first post and
              I saw Semen's Superclass example, and I justed posted mine, didn't even
              see your's yet.

              ------------------
              -Greg

              [This message has been edited by Gregery D Engle (edited August 17, 2001).]
              -Greg
              [email protected]
              MCP,MCSA,MCSE,MCSD

              Comment


              • #8
                Of course, subclassing is a little shorter.
                But "superclass" is more understable (at least, for me).
                Typically, there is a wish to overwrite SETFOCUS, sometimes "Tab" and so on. And in real code difference between sub/super is not serious.



                ------------------
                E-MAIL: [email protected]

                Comment

                Working...
                X