Announcement

Collapse
No announcement yet.

send text when enter key

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

  • send text when enter key

    Hi,
    I try to send massage in the textbox by pressing enter key
    but i have two text box, I want to send only the text in
    the text box where the cursor in, but I can't understand
    the function of getcaretpos. anyone can help with some code?
    Thank.

    #COMPILE EXE
    #INCLUDE "win32api.inc"

    %ID_txt1 = 101
    %ID_txt2 = 102

    CALLBACK FUNCTION TrickBut
    'getcaretpos() 'how to?

    MSGBOX "" 'show the the text in either ID_txt1 or ID_txt2
    ' depend on the caret(cursor) at which box
    END FUNCTION

    FUNCTION PBMAIN () AS LONG

    LOCAL hdlg AS LONG

    DIALOG NEW 0, " The Enter key ", 100, 100, 100, 100, _
    %WS_SYSMENU,TO hdlg

    CONTROL ADD TEXTBOX, hdlg, %ID_txt1, "", 25, 20, 50, 14
    CONTROL ADD TEXTBOX, hdlg, %ID_txt2, "", 25, 50, 50, 14
    'put the button outside the window area to trap the enter key.
    CONTROL ADD BUTTON, hdlg, 100, "", 150, 80, 0, 0, %BS_DEFAULT _ CALL TrickBut
    DIALOG SHOW MODAL hdlg

    END FUNCTION



  • #2
    Aisin --
    I'm very doubt that you need GetCaretPos.
    Because I don't exactly understand, what you want, I can show simple technology, how to retrieve a window, where do you type, and how to insert a text to this field in current (caret) position (exactly, at the beginning of selection)

    Code:
       #Compile Exe
       #Register None
       #Dim All
       #Include "win32api.inc"
    
       Global hDlg As Long, hHook As Long
       Function KbdProc(ByVal nCode As Integer, ByVal wParam As Long, ByVal lParam As Long) As Long
          Function = CallNextHookEx(ByVal hHook, ByVal nCode, ByVal wParam, ByVal lParam)
          If (ncode >= 0) And (wParam = 13) And IsFalse(lParam And &H80000000) Then ' Key Down
             Local i As Long, j As Long, k As Long, Txt As String
             ' Find window with focus
             k = GetFocus
             For i = 1 To 3
                If k = GetDlgItem(hDlg, 100 + i) Then j = i: Exit For
             Next
             If j > 0 Then
                ' Insert <>
                SendMessage GetDlgItem(hDlg, 100 + j), %EM_GETSEL, VarPtr(i), 0
                Control Get Text hDlg, 100 + j To Txt
                Control Set Text hDlg, 100 + j, Left$(Txt, i) + "<>" + Mid$(Txt, i + 1)
                ' Goto next line
                If j = 3 Then j = 1 Else Incr j
                Control Set Focus hDlg, 100 + j: Function = 1
             End If
          End If
       End Function
       
       CallBack Function DlgProc
          Select Case CbMsg
             Case %WM_INITDIALOG
                hHook = SetWindowsHookEx (%WH_KEYBOARD, CodePtr(KbdProc), _
                   0, GetWindowThreadProcessId(CbHndl, 0&))
             Case %WM_DESTROY
                UnhookWindowsHookEx hHook
          End Select
       End Function
    
       Function PbMain () As Long
          Dialog New 0, " The Enter key ", , , 100, 65, %WS_CAPTION Or %WS_SYSMENU,To hdlg
          Control Add TextBox, hdlg, 101, "", 5,  5, 90, 12
          Control Add TextBox, hdlg, 102, "", 5, 25, 90, 12
          Control Add TextBox, hdlg, 103, "", 5, 45, 90, 12
          Dialog Show Modal hdlg Call DlgProc
       End Function
    ------------------

    Comment


    • #3
      If I understand your question correctly you want to know which textbox the caret is in when the user presses ENTER, then send the text from that textbox? There are several ways to do this, but the way you describe would be, in my opinion, the long way around since your sample code is using DDT.

      Since you are using DDT, here is one solution. It has the down-side of using a Global variable to hold the Id of the last textbox though:

      Code:
      #COMPILE EXE 
      #INCLUDE "win32api.inc" 
       
      %ID_txt1 = 101 
      %ID_txt2 = 102 
       
      GLOBAL lastFocus AS LONG 
       
      CALLBACK FUNCTION TrickBut() AS LONG 
          DIM MyString AS STRING 
          CONTROL GET TEXT CBHNDL, lastFocus TO MyString 
           
          MSGBOX  Mystring 
      END FUNCTION 
       
      CALLBACK FUNCTION EditBoxesCallback() AS LONG 
          lastFocus = CBCTL 
      END FUNCTION 
       
      FUNCTION PBMAIN () AS LONG 
          Local hDlg as LONG 
          DIALOG NEW 0, " The Enter key ", 100, 100, 100, 100, %WS_SYSMENU,TO hdlg 
       
          CONTROL ADD TEXTBOX, hdlg, %ID_txt1, "", 25, 20, 50, 14 CALL EditBoxesCallback() 
          CONTROL ADD TEXTBOX, hdlg, %ID_txt2, "", 25, 50, 50, 14 CALL EditBoxesCallback() 
       
          'put the button outside the window area to trap the enter key. 
          CONTROL ADD BUTTON, hdlg, 100, "", 150, 80, 0, 0, %BS_DEFAULT CALL TrickBut 
          DIALOG SHOW MODAL hdlg 
      END FUNCTION
      There are other more elegant ways, but after watching all three Back to the Future, and both (of the good) Addam's Family movies tonight (five of my top 15 favorite movies), they're not coming to me <grin>. Good luck, though...

      ------------------
      Troy King
      [email protected]

      [This message has been edited by Troy King (edited May 28, 2000).]
      Troy King
      katravax at yahoo dot com

      Comment


      • #4
        semens example uses a hook function which should work, but adds a large layer of complexity to the application, and can make debugging more difficult.

        my suggestion is to use subclassing. take a look at:



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

        Comment


        • #5
          Thank Semen, Troy,Lance.

          The Troy example is what I need.
          but I will study on Semen technic too(the Hook??)

          I am too new to windows programming,
          I came from a big jump! The last time
          I do programming was with Turbo Basic and C.
          (that time C don't have Class yet.)
          so subclass for me is a big Alien!

          Troy, enjoy your Back to the Future.
          but I am talking about D0S 2.1!!

          Have a nice day, Cheer everybody.


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

          Comment


          • #6
            Hi Pal,

            Now I got another question, if the txt1 going to send to Massagebox 1 and txt2 send to messagebox2, but using just
            a Enter key, how to do that? can highlight what should I
            do. Thank.

            B.rds


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

            Comment


            • #7
              If you are using Troy's "hidden" button trick to capture the enter key, you'll already have the control ID of the last text box (that received a message), so you should be able to choose your course of action depending on the ID of that control.

              If this does not help, post the code you are *now* using, and describe whay you mean by "MessageBox1" and "MessageBox2".

              Also, when you post source code, be sure to use the UUB code tokens [ CODE ] and [ /CODE ] (without the spaces - if I put them in this note then the BBS would not display them as literal text!)
              See http://www.powerbasic.com/support/forums/ubbcode.html



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

              Comment


              • #8
                Hi Lance,
                Thank
                I got my problem solved.
                What I mean is that.......

                Code:
                #COMPILE EXE
                #INCLUDE "win32api.inc"
                
                %ID_txt1 = 101
                %ID_txt2 = 102
                
                GLOBAL lastFocus AS LONG
                
                CALLBACK FUNCTION TrickBut() AS LONG
                DIM MyString AS STRING
                
                IF lastfocus=101 THEN
                
                    CONTROL GET TEXT CBHNDL, lastfocus TO MyString
                
                        MSGBOX "Send "+Mystring
                
                 ELSEIF lastfocus=102 THEN
                    CONTROL GET TEXT CBHNDL, lastfocus TO MyString
                        MSGBOX "Read "+Mystring
                
                 ELSE
                        MSGBOX "Not at all"
                
                    END IF
                
                END FUNCTION
                
                CALLBACK FUNCTION EditBoxesCallback() AS LONG
                lastFocus = CBCTL
                END FUNCTION
                
                FUNCTION PBMAIN () AS LONG
                LOCAL hDlg AS LONG
                DIALOG NEW 0, " The Enter key ", 100, 100, 100, 100, %WS_SYSMENU,TO hdlg
                
                CONTROL ADD TEXTBOX, hdlg, %ID_txt1, "", 25, 20, 50, 14 CALL EditBoxesCallback()
                CONTROL ADD TEXTBOX, hdlg, %ID_txt2, "", 25, 50, 50, 14 CALL EditBoxesCallback()
                
                'put the button outside the window area to trap the enter key.
                CONTROL ADD BUTTON, hdlg, 100, "", 150, 80, 0, 0, %BS_DEFAULT CALL TrickBut
                DIALOG SHOW MODAL hdlg
                END FUNCTION

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

                Comment


                • #9
                  May I suggest some minor modifications...?

                  Since you are using equates already, it makes sense to use them everywhere in your code. Also, there is a duplicated CONTROL GET TEXT line. Finally, you should always be sure that your callback is actually receiving a "click" message - there are other messages that can execute your callback and these can cause subtle bugs in your applications.

                  Take at look at my slight changes to your callback:
                  Code:
                  CALLBACK FUNCTION TrickBut
                    DIM MyString AS STRING
                    IF CBMSG <> %WM_COMMAND AND CBCTLMSG <> %BN_CLICKED THEN EXIT FUNCTION
                    IF ISFALSE lastfocus THEN EXIT FUNCTION
                    CONTROL GET TEXT CBHNDL, lastfocus TO MyString        
                    IF lastfocus=%ID_txt1 THEN    
                      MSGBOX "Send "+Mystring 
                    ELSEIF lastfocus=%ID_txt2 THEN    
                      MSGBOX "Read "+Mystring 
                    ELSE        
                      MSGBOX "Not at all"    
                    END IF
                  END FUNCTION
                  ------------------
                  Lance
                  PowerBASIC Support
                  mailto:[email protected][email protected]</A>
                  Lance
                  mailto:[email protected]

                  Comment


                  • #10

                    Thank Lance,

                    I'm very new to Windows programming,In fact, after reading the
                    help file, I still not quite sure about what is CBMSG,CBHNDL,
                    CBCTL,CBLPARAM etc....
                    I try to make a little program that when I click some button,
                    all this value will display in the respective Text Box,so I
                    can find up what is this return value mean, but I crash the
                    system, and untill now my system don't seen like as healthy
                    as before.so I stop playing with it.
                    I study some Windows book, but usually the explaination is, when
                    ......return lparam.......... I think I really need a clear example of this kind of callback value.
                    like this example, at least I know CBCTL is equare to the %IDC...
                    value(I assign in the declear area, 101 or 102 )that in focus.
                    I know this may be too simple for a lot of old hand, but don't
                    give me up!
                    Best regards




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

                    Comment


                    • #11
                      The various CBxxxx system variables are equivalent to the conventional SDK-style callback function parameters.

                      For example, if we consider a conventional "discrete" callback function:

                      FUNCTION MyDlgCallback(BYVAL hDlg AS LONG, BYVAL wMsg AS LONG, BYVAL wParam AS LONG, BYVAL lParam AS LONG) AS LONG

                      DDT simplifies this type of function declaration to a simple declaration:

                      CALLBACK FUNCTION MyDlgCallback

                      Because the DDT callback function does not have parameters specified, the code within the callback still needs to be able to use the parameter values passed to the callback. DDT provides the various CBxxx system variables to allow these parameter values to be read.

                      The following table shows the equivalent DDT system variable against each of the conventional parameters:

                      CBHNDL ~ hDlg
                      CBMSG ~ wMsg
                      CBWPARAM ~ wParam
                      CBLPARAM ~ lParam

                      The other two DDT system variables (CBCTL anmd CBCTLMSG) are simply the high and low order 16-bit values of wParam.

                      CBCTL ~ LOWRD(wParam)
                      CBCTLMSG ~ HIWRD(wParam)

                      I highly recommend that you (and everyone else!) get yourselves a good Windows programming book, such as the ones in the "recommended titles" list in the FAQ forum. With an understanding of conventional SDK-style GUI programming, you'll get a much better insight into how DDT works, and you'll be able to write better applications too!




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

                      Comment

                      Working...
                      X