Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to hide button control in VBA

Is there anybody out here still programming VBA?

I'm trying to get this code to work

Private Sub button3_click()

    'hide main buttons
    button1.Visible = False
    button2.Visible = False
    button3.Visible = False

    'show submenu buttons
    button4.Visible = True;
    button5.Visible = True;

End Sub

What I'm trying to do basically is that I have a main form that has 5 main button controls. 2 of them are hidden on startup. So when I click button 3, I want to hide the first 3 main buttons, and "unhide" the other two. When trying to execute this event, I got an error

"Runtime Error 2165 - You can't hide a control that has the focus".

Has anybody come across this aspect of programming before? I'm sure it's doable. I just don't understand what went wrong here...

like image 373
awongCM Avatar asked Jan 28 '12 21:01

awongCM


1 Answers

Change the focus to one of the visible control, before hiding the current one

Private Sub button3_click()

    'show submenu buttons
    button4.Visible = True
    button5.Visible = True

    DoEvents          'execute any pending events, to make sure the button 4 and 5 are really visible
    button4.SetFocus  'change the focus to a now visible control
    DoEvents          'execute any pending events, to make sure that button4 really has the focus

    'now you can hide the other buttons

    'hide main buttons
    button1.Visible = False
    button2.Visible = False
    button3.Visible = False

End Sub

Maybe you can skip the DoEvents command, you should try

like image 66
Max Avatar answered Oct 19 '22 15:10

Max