Results 1 to 12 of 12

Thread: KeyAscii

  1. #1
    dana340
    Guest

    KeyAscii

    hi there,
    I use this code on each text_KeyPress
    i think it could be use only at once in the form
    instead of rewrite this code every time ,



    Private Sub Text1_KeyPress(KeyAscii As Integer)
    If KeyAscii = 13 Then
    text2.SetFocus
    End If
    End Sub


    could someone help pls.
    thanks in advance

  2. #2
    PowerPoster rjlohan's Avatar
    Join Date
    Sep 2001
    Location
    Sydney, Australia
    Posts
    3,205
    Put your text boxes into a control array. So the code would be:

    Private Sub Text_KeyPress(Index As Integer, KeyAscii As Integer)
    If KeyAscii = 13 Then
    Text(Index + 1).SetFocus
    End If
    End Sub
    -----------------------------------------
    -RJ
    [email protected]
    -----------------------------------------

  3. #3
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333
    Try setting the Form's KeyPreview to True, and put your code in the Forms KeyPress event.

  4. #4
    dana340
    Guest
    thanks for the reply

    I have try both
    1:-
    I have create 3 textbox

    properties

    text1.index=1
    text2.index=2
    text3.index=3

    setting the Form's KeyPreview to True
    and then I have copied

    Private Sub Form_KeyPress(KeyAscii As Integer)
    If KeyAscii = 13 Then
    Text(Index + 1).SetFocus
    End If

    End Sub

    but it's still not working

    2:-
    you'v said Put your text boxes into a control array
    actually I don't know what is a control array
    pls. I need more explanation.

    thanks again for you help.

  5. #5
    should be

    text1 = index 0
    text1 = index 1
    text1 = index 2
    -Sensimillia

  6. #6
    you could try....

    Code:
    Private Sub Text1_KeyPress(KeyAscii As Integer) 
    
    Call IfKeyIsPressed
    End Sub 
    
    Public Sub IfKeyIsPressed(KeyAscii As Integer) 
         If KeyAscii = 13 Then 
              text2.SetFocus 
         End If 
    End Sub
    -Sensimillia

  7. #7
    PowerPoster beachbum's Avatar
    Join Date
    Jul 2001
    Location
    Wollongong, NSW, Australia
    Posts
    2,274
    hi
    if u dont want to worry about control arrays and keypress events etc u can use this 'trick'. This works as long as u dont already have a Default command button on ur form.

    Add a command button. Set properties as follows
    Default - True
    TabStop - False
    Name - cb_tab
    Then make it very small and move it behind a textbox or something. Use send to back from the right click menu.

    Put this code in the button event and that is all u need. NB. u can also check for different types of controls see eg 2
    VB Code:
    1. 'EXAMPLE 1. SIMPLE KEYPRESS
    2. Private Sub cb_tab_Click()
    3.     On Error Resume Next
    4.     SendKeys "{tab}"
    5. End Sub
    VB Code:
    1. 'EXAMPLE 2. CHECK FOR TYPE OF CONTROL
    2. Private Sub cb_beepstop_Click()
    3.     On Error Resume Next
    4.     If TypeOf fctlFocusControl Is ComboBox Then
    5.         'Yadda yadda yadda
    6.     Else
    7.         SendKeys "{tab}"
    8.     End If
    9. End Sub
    10.  
    11. 'Then in every controls got focus event set the fctlfocuscontrol variable to the current control
    12. ie Set FctlFocusControl = Text1 ' etc etc
    Regards
    Stuart
    Stuart Laidlaw
    Brightspark Financial Software
    http://www.gstsmartbook.com

  8. #8
    PowerPoster beachbum's Avatar
    Join Date
    Jul 2001
    Location
    Wollongong, NSW, Australia
    Posts
    2,274
    PS
    NB The text boxes should have sequential tab order numbers
    eg Text1 : TabIndex =0
    Text2 : TabIndex =1
    etc
    Regards
    Stuart
    Stuart Laidlaw
    Brightspark Financial Software
    http://www.gstsmartbook.com

  9. #9
    dana340
    Guest
    thank you Stuart & Sensimillia for the reply

    i have tried your example & code
    works perfects

    but i still want to ask Sensimillia about the code sent , i realy want to learn something becuase some time i use 256 tool as in my product , want to save cmd button so,i tried this

    properties:-
    text1 = index 0
    text1 = index 1
    text1 = index 2


    and i have copied your code as:-

    Private Sub Text1_KeyPress(KeyAscii As Integer)

    Call IfKeyIsPressed
    End Sub

    Public Sub IfKeyIsPressed(KeyAscii As Integer)
    If KeyAscii = 13 Then
    'text2.SetFocus ' i tried it but not working properly

    text(index+1).setfocus ' i tried it also but not working
    either

    End If
    End Sub

    sill not got it ,
    so , is there any thing i have missed pls. advice & sorry for the disturb

    thank you again for the help


  10. #10
    PowerPoster beachbum's Avatar
    Join Date
    Jul 2001
    Location
    Wollongong, NSW, Australia
    Posts
    2,274
    Hi again
    I can help u with Sensimillia's code also
    There are a number of ways using S's code also

    On your form create a textbox called Text1. Copy it using Ctrl-C and paste it Ctrl-V... when it asks u if u want to create a control array press Yes. U now have Text1(Index = 0) and Text1(Index = 1). Paste a few more times to get more boxes.

    You can now tab between textboxes using a number of methods... here are 2

    VB Code:
    1. '1) If textboxes are in tabindex order
    2. Private Sub Text1_KeyPress(Index As Integer, KeyAscii As Integer)
    3. 'Note that the sub now has an Index As integer item. This
    4. 'is the index number of each box u created so u can know
    5. 'which one the user is in.
    6.     If KeyAscii = 13 Then 'Check for the enter key (13)
    7.         KeyAscii = 0 'Reset it to stop beeping
    8.         SendKeys "{tab}" 'Tab to next control
    9.     End If
    10. End Sub
    VB Code:
    1. '2) Tabbing between controls based on index numbers
    2. Private Sub Text1_KeyPress(Index As Integer, KeyAscii As Integer)
    3.     If KeyAscii = 13 Then'Check for enter key
    4.         KeyAscii = 0 'Reset for beeping
    5.         If Index < Text1.UBound Then 'If not last textbox
    6.             Text1(Index + 1).SetFocus 'send focus to next item
    7.         Else 'if last control send focus back to first item
    8.             Text1(Text1.LBound).SetFocus
    9.         End If
    10.     End If
    11. End Sub
    Stuart Laidlaw
    Brightspark Financial Software
    http://www.gstsmartbook.com

  11. #11
    dana340
    Guest

    Talking

    thank you for all the reply,
    I really appreciated
    it work fantastic + I have know what is the control array also
    I LOVE VB Q&A because of you
    thank you again

  12. #12
    Hyperactive Member FUBAR's Avatar
    Join Date
    Jan 2000
    Posts
    307
    im glad you got it workign

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width