Results 1 to 11 of 11

Thread: [RESOLVED] vbKey help

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Feb 2010
    Location
    Damascus - Syria
    Posts
    145

    Resolved [RESOLVED] vbKey help

    hello all

    first of all thanks for this amazing website.

    I am a starter in VB, I need your help in a code that end it self when the user type exit.

    somebody told me to use vbKey, but it is ok for one key only (as I know)

    thank you

    regards.

  2. #2
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: vbKey help

    I need your help in a code that end it self when the user type exit.
    type where?
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  3. #3
    Fanatic Member louvelle's Avatar
    Join Date
    Jun 2008
    Posts
    513

    Re: vbKey help

    @fjober

    use this in the extbox

    Code:
    Private Sub Text1_KeyPress(KeyAscii as Integer)
    
    If Keyascii = 13 Then
       If Text1.Text = "Exit" Then
           End
       End if
    End if
    
    Exit Sub
    Search for the keyascii table for different kinds of keys...

    Manny Pacquiao once posted in his twitter:
    It doesn't matter if the grammar is wrong, what matter is that you get the message

  4. #4
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: vbKey help

    If Text1.Text = "Exit" Then
    End
    End if
    uggh never use end
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Feb 2010
    Location
    Damascus - Syria
    Posts
    145

    Re: vbKey help

    good morning

    thanks for replaying, but I may didn't clear what I want.

    I don't want the user to type in a text box or any other field

    look at this code:

    Code:
    Private Sub Form_KeyDown(KeyCode As Integer , Shift As Integer)
    if KeyCode = vbKeyF Then End
    End Sub
    as you know, the last program will end it self on pressing F and there are no fields.

    I just want it to end on typing exit instead of F

  6. #6
    Freelancer akhileshbc's Avatar
    Join Date
    Jun 2008
    Location
    Trivandrum, Kerala, India
    Posts
    7,652

    Re: vbKey help

    I think this will give you an idea.... You have to play with it to get the perfect one that really suits you...
    Code:
    Option Explicit
    
    Dim strTemp As String
    
    Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
    Select Case (KeyCode)
      Case vbKeyE: strTemp = strTemp & "E"
      Case vbKeyX: strTemp = strTemp & "X"
      Case vbKeyI: strTemp = strTemp & "I"
      Case vbKeyT: strTemp = strTemp & "T"
    End Select
    If strTemp = "EXIT" Then Unload Me
    End Sub
    
    Private Sub Form_Load()
    strTemp = ""
    Me.KeyPreview = True
    End Sub

    If my post was helpful to you, then express your gratitude using Rate this Post.
    And if your problem is SOLVED, then please Mark the Thread as RESOLVED (see it in action - video)
    My system: AMD FX 6100, Gigabyte Motherboard, 8 GB Crossair Vengance, Cooler Master 450W Thunder PSU, 1.4 TB HDD, 18.5" TFT(Wide), Antec V1 Cabinet

    Social Group: VBForums - Developers from India


    Skills: PHP, MySQL, jQuery, VB.Net, Photoshop, CodeIgniter, Bootstrap,...

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Feb 2010
    Location
    Damascus - Syria
    Posts
    145

    Re: vbKey help

    oh my god, this code is really good but....

    it would work only if user typed EXIT

    what if an error happened while typing? "ECIT" instead of "EXIT" (for example), this means that the program will not continue as I want it because the user cannot undo or delete what he typed.

    thanks

  8. #8
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: vbKey help

    Code:
    Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
    Select Case (KeyCode)
      Case vbKeyE: strTemp = strTemp & "E"
      Case vbKeyX: strTemp = strTemp & "X"
      Case vbKeyI: strTemp = strTemp & "I"
      Case vbKeyT: strTemp = strTemp & "T"
      case else: strtemp = ""
    End Select
    
    If strTemp = "EXIT" Then Unload Me
       select case strtemp   ' clear any invalid strings
           case "E", "EX", "EXI", EXIT"
           case else : strtemp = ""
       end select
    End Sub
    or

    Code:
    Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
    
      if keycode = vbKeyE and strTemp = "" then strTemp = "E"
      if keycade = vbKeyX and strtemp = "E" then strTemp = strTemp & "X"
      if keycode = vbKeyI and strtemp = "EX" then strTemp = strTemp & "I"
      if keycode = vbKeyT and strTemp = "EXI" then strtemp = strTemp & "T"
      
    
    If strTemp = "EXIT" Then Unload Me
    End Sub
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  9. #9
    Head Hunted anhn's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    3,669

    Re: vbKey help

    Or:
    Code:
    Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
        Static n As Long
        Select Case KeyCode
            Case vbKeyE: n = -(n = 0) * 1
            Case vbKeyX: n = -(n = 1) * 2
            Case vbKeyI: n = -(n = 2) * 3
            Case vbKeyT: If n = 3 Then Unload Me else n = 0
        End Select
    End Sub
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • If your question was answered please use Thread Tools to mark your thread [RESOLVED]
    • Don't forget to RATE helpful posts

    • Baby Steps a guided tour
    • IsDigits() and IsNumber() functions • Wichmann-Hill Random() function • >> and << functions for VB • CopyFileByChunk

  10. #10

    Thread Starter
    Addicted Member
    Join Date
    Feb 2010
    Location
    Damascus - Syria
    Posts
    145

    Resolved Re: vbKey help

    hello everybody

    thanks for your help and your ideas

    I have wrote a code that do what I want, the most important thing in this code that you can undo what you have typed, here it is:

    Code:
    Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
    str = str & Chr(KeyCode)
    If LCase(str) = "exit" Then End
    If KeyCode = vbKeyZ Then str = ""
    End Sub

  11. #11
    Freelancer akhileshbc's Avatar
    Join Date
    Jun 2008
    Location
    Trivandrum, Kerala, India
    Posts
    7,652

    Re: vbKey help

    If LCase(str) = "exit" Then End
    A suggestion: Rather than using End, you can use Unload Me ...
    Check this link: http://www.vbforums.com/showthread.php?t=511766

    If my post was helpful to you, then express your gratitude using Rate this Post.
    And if your problem is SOLVED, then please Mark the Thread as RESOLVED (see it in action - video)
    My system: AMD FX 6100, Gigabyte Motherboard, 8 GB Crossair Vengance, Cooler Master 450W Thunder PSU, 1.4 TB HDD, 18.5" TFT(Wide), Antec V1 Cabinet

    Social Group: VBForums - Developers from India


    Skills: PHP, MySQL, jQuery, VB.Net, Photoshop, CodeIgniter, Bootstrap,...

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