Events on invisible buttons

InertiaM

Well-known member
Joined
Nov 3, 2007
Messages
663
Location
Kent, UK
Programming Experience
10+
Is it normal for events to fire on invisible controls? I've always been lazy and just made certain buttons invisible rather than set their Enabled property to false.

Create a new project, and only put Button1 on it.

VB.NET:
Public Class Form1

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Me.KeyPreview = True
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        MessageBox.Show("OK")
        Me.Button1.Visible = False
    End Sub
End Class

When I run the code, and press Space, I get the OK messagebox. Press Space to accept the OK - and Button1 disappears. Now, if I press Space again, the Click event fires (and shows the messagebox) - even though the button is hidden :confused: After this, when you press Space again, it doesnt fire.

As far as I can ascertain, when I accept the 1st messagebox, it puts the focus back onto Button1 before it is made invisible, but doesnt blur the focus when it is hidden. Pressing Space then fires the event on the invisible button, but once it IS invisible, it obviously cannot accept the focus.

I know I can avoid this, but are there any other quirks like this that I need to try and avoid :)
 
KeyPreview doesn't have anything to do with this.
 
First of all, hiding a button that, intuitively, should be disabled is not lazy; it's just silly. Setting the Enabled property is no more work than setting the Visible property, so you're not saving yourself any work. Also, controls appearing and disappearing can be appropriate under some circumstances but an OK button that cannot be clicked because some field hasn't been filled or whatever should certainly be disabled, not hidden.

I think you'll probably find that disabling the button instead of hiding it would prevent this issue too. That said, what you describe is a bit of a quirk. Note that it will only occur if there are no other controls that can receive focus on the form though. If there are other controls focus will pass to the next control in the tab order when the Button is hidden.

I can't see a situation where you would want to hide a Button like that when there are no other controls on the form that can receive focus. That means that, in the real world, this bug will never be seen. In your specific case you can still work around it by setting the ActiveControl property of the form to Nothing, thus removing focus from the Button. You'll also find that hiding the Button before showing the message will avoid the issue too.
 
First of all, hiding a button that, intuitively, should be disabled is not lazy; it's just silly.

I agree - I promise not to do it again ;)

I can't see a situation where you would want to hide a Button like that when there are no other controls on the form that can receive focus. That means that, in the real world, this bug will never be seen.

On a production line, I have five PCs which all have PLCs running on them. The button was used to connect to the PLC on the individual PC, as the operator needs to start the connection manually. The form is then a simple GDI application displaying the production line program - hence no other controls on the form.

Anyway, it all works now - and I've learnt my lesson :eek:
 
The code that's in the one button on the form could be put into a sub and you call that sub in the form's Shown event, and of course display in messages and whatnot in any labels and whatever else you have on that form.

If the single button is indeed the only control on the entire form, why even have a form at all? Use Sub Main() to do that code and simply keep the program in a loop until it's time to exit in which you let the code exit the loop and the whole program ends when it reaches the 'End Sub' line of Sub Main()
 
Back
Top