Results 1 to 15 of 15

Thread: [RESOLVED] Visual Basic Command Button Problem

  1. #1

    Thread Starter
    Member
    Join Date
    Dec 2007
    Location
    Brunei Darussalam
    Posts
    50

    Resolved [RESOLVED] Visual Basic Command Button Problem

    hi,
    i just want to ask what is the right code for this,
    for example i have a command button and instead of clicking it by the mouse the keyboard enter key will do the job,
    i mean you can click the command button by mouse and also you can use enter to execute the command?

    sorry im really a noob about this,
    i know this isnt my line, its just my hobby


    thanks

  2. #2
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: Visual Basic Command Button Problem

    Welcome to the forums. Yes, ensure button has the focus first.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  3. #3

  4. #4

    Thread Starter
    Member
    Join Date
    Dec 2007
    Location
    Brunei Darussalam
    Posts
    50

    Re: Visual Basic Command Button Problem

    sir did you i need to add this

    example.setfocus

    End Sub

    am i correct?
    sorry

    and thanks sir for welcoming me

  5. #5
    Next Of Kin baja_yu's Avatar
    Join Date
    Aug 2002
    Location
    /dev/root
    Posts
    5,989

    Re: Visual Basic Command Button Problem

    You need to Tab through the controls until the rectangular focus marker gets on the button you want to 'click'.

    If you have an input form for example, a few text boxes and an OK/Cancel buttons, you can use the Keypress event of the textboxes to catch the enter and execute the OK if the user presses Enter in one of the textboxes.

  6. #6

  7. #7
    Next Of Kin baja_yu's Avatar
    Join Date
    Aug 2002
    Location
    /dev/root
    Posts
    5,989

    Re: Visual Basic Command Button Problem

    Yeah, but we're ignoring you. J/k His solution will probably depend on what the form does and looks like. If it is simple with only a few buttons, like an input form or something withat a few command buttons (Yes/No, Yes/No/Cancel etc) the Default property should more than ok.

  8. #8

  9. #9

    Thread Starter
    Member
    Join Date
    Dec 2007
    Location
    Brunei Darussalam
    Posts
    50

    Re: Visual Basic Command Button Problem

    hehehe
    sorry guys for being such a noob,
    im really a newbie
    @Baja_yu

    yes i understand now sir,
    yes i have command button and a textbox,
    if i put something on text box i have to press the command button to execute the command,
    i try the keypress event it doesnt work :-?
    or maybe i have a mistake on it?

    @sir Rhino
    You may set button's Default property to True however be aware that it may create some side effects.

    Sir i will try but what side effects may occur sir?
    well i will just try and find out

    thank you all for the response
    i really apreciate you answer a question from a noob like me

  10. #10

  11. #11
    Next Of Kin baja_yu's Avatar
    Join Date
    Aug 2002
    Location
    /dev/root
    Posts
    5,989

    Re: Visual Basic Command Button Problem

    This is how you would do it with the KeyPress method

    Code:
    Option Explicit
    
    Private Sub Command1_Click()
       MsgBox "button was clicked"
    End Sub
    
    Private Sub Text1_KeyPress(KeyAscii As Integer)
       If KeyAscii = 13 Then 'Enter
          Call Command1_Click
       End If
    End Sub
    Now, when you press Enter while typing in the text box, the click event of the command box will fire and the message box will be shown. Just an example, you can modify it to suite your needs.

  12. #12

    Thread Starter
    Member
    Join Date
    Dec 2007
    Location
    Brunei Darussalam
    Posts
    50

    Re: Visual Basic Command Button Problem

    @baja_yu

    thanks it is now working,


    thanks sir LaVolpe, sir Rhino and sir Baja_yu for the great help
    please expect me to ask more question in the future, i really appreciate everything

    your unwavering assistance is a big help to this community

    regards

  13. #13
    Next Of Kin baja_yu's Avatar
    Join Date
    Aug 2002
    Location
    /dev/root
    Posts
    5,989

    Re: Visual Basic Command Button Problem

    No problem!

    Like Rhino said before, you don't have to call us sir. Also, if your question is answered, please take time to mark the thread as 'Resolved'. You can do so by going to the Thread Tools menu which is on the right side, above your original post, and choosing Mark Thread Resolved.

  14. #14
    Frenzied Member
    Join Date
    Dec 2007
    Posts
    1,072

    Re: [RESOLVED] Visual Basic Command Button Problem

    Since "Call" is optional, forget you ever saw it

    I've heard that using Call is bad coding habits / excess memory. Looking for the source now,

  15. #15
    Next Of Kin baja_yu's Avatar
    Join Date
    Aug 2002
    Location
    /dev/root
    Posts
    5,989

    Re: [RESOLVED] Visual Basic Command Button Problem

    I'd love to see the info on that.

    There's a very, very limited scenario under which a memory leak can occur, but is limited to functions that pass Arrays as return values. It's described here http://support.microsoft.com/kb/q197190/
    There are a lot of other good uses for Call, for example, when you don't need the return value from the function etc.

    Other than that, since it's optional, I guess it comes down to personal preference/stlyle.

    EDIT: In fact, the memory leak only happens if the call is implicit to UBound or LBound like described in the test. If you explicitly Call the ArryFunc in the example, there is no leak.
    Last edited by baja_yu; May 17th, 2010 at 03:02 AM.

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