Hide/show command button - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!


Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Hide/show command button

I was wandering how to make a comand button hidden unless two buttons such as ctrl + L is pressed and how to make the form auto-resize to fit the new command button?


  • To do this, you'd set the Visible property of the Command button to False

    at design time. Next, put the following procedure in the Form_KeyDown event

    of the Form containing the Command Button:

    'Check for Ctrl+L

    If KeyCode = vbKeyL And (Shift And vbCtrlMask > 0) Then

    'Move the top of the button to the bottom of the Form

    Command1.Top = Height

    'Add the height of the button to the height of the Form - plus some

    'extra to make it look OK

    Height = Height + Command1.Height + 800

    'Show the button

    Command1.Visible = True

    'Make sure the Form repaints


    End If

    Depending on what other controls are on your Form, you may have to set the

    KeyPreview Property of the Form to True to make sure the Form knows that

    you've pressed Ctrl+L. You can use the Top and Left Propeties - or the

    Move Method - to adjust the position of the Command button before you make

    it visible.

    For more information about the VB Constants (vbKeyL and vbCtrlMask) see the

    KeyDown Event topic in VB Help. If you have a version of VB that doesn't

    support those specific Constants:

    vbKeyL= 76

    vbCtrlMask = 2


Sign In or Register to comment.