Results 1 to 6 of 6

Thread: [Resolved] Adding item to listbox with Enter Keypress

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2001
    Location
    Maumelle, AR
    Posts
    624

    [Resolved] Adding item to listbox with Enter Keypress

    I have been out of VB since VB 6, but am back into it to write a basic app. I have created a listbox and am able to add data to it from a textbox via a command button. However, I want to give the user the option to simply press the enter key after they have entered the info the textbox, instead of clicking on the command button. I tried calling the function the command button is using from the "ENTER" event on the textbox, but all that does is execute the function when I click on the textbox? I'm sure this is very simple. What am I missing? Thanks for any help.
    Last edited by hipopony66; Feb 20th, 2009 at 10:09 AM.

  2. #2
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: Adding item to listbox with Enter Keypress

    Hey,

    On the KeyPress Event, when you find the user has pressed the enter button put the following code:

    Code:
    Button1.PerformClick()
    (where Button1 is the name of your command button).

    This will have the same effect as if the user clicked the button.

    Gary

  3. #3
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    Re: Adding item to listbox with Enter Keypress

    When you say "am back into it" do that mean you are still using VB6.0? If yes then you have posted in the wrong section.

    If it would have been VB6.0 then what you want can be done like this.

    Code:
    Option Explicit
    
    Private Sub Command1_Click()
        MsgBox "Called!"
    End Sub
    
    Private Sub Text1_KeyPress(KeyAscii As Integer)
        If KeyPress = 13 Then ' enter key was pressed
            KeyPress = 0 'suppress sound
            Command1_Click 'call the Command1 click procedure
        End If
    End Sub
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  4. #4

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2001
    Location
    Maumelle, AR
    Posts
    624

    Re: Adding item to listbox with Enter Keypress

    Nope, I have previous experience with VB6, but just started programming again and am now using VB.NET 2008 Express.

  5. #5
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    Re: Adding item to listbox with Enter Keypress

    Another method is you can specify in your form's property AcceptButton the button which you like to be triggered when you press Enter key.

    Its like setting the Default property to True of a command button in VB6.0.
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  6. #6
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: Adding item to listbox with Enter Keypress

    Quote Originally Posted by dee-u
    Another method is you can specify in your form's property AcceptButton the button which you like to be triggered when you press Enter key.

    Its like setting the Default property to True of a command button in VB6.0.
    Good thinking!!

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