Search code examples
vb.netfocus

defocus button after clicking it


In a VB project I have 3 buttons (checkbox with buttons appearance). If I click 1 of them it remains selected (focused).

I will control that program with a small RF keyboard, I suffer of pressing the space key very easy!

How can I deselect (loose the focus) of that control to avoid the accidental activation-deactivation by space pressing? I've tried adding a hidden button, and focusing it on the click event of the check-buttons"... like:

newbutton.focus()

but it doesn't work! Later I've tried to focus the mainform but also it doesn't work!

form1.focus()

also I've tried:

Form1.select()

but still no working after clicking(checking) the button it can be easily unchecked clicking the space key again! Which is the correct way to avoid the accidental unchecking?

thanks.


Solution

  • It appears that you working with a WinForm. The Form as a property named ActiveControl that can be set to Nothing (null). Doing so in the CheckBox.CheckedChanged handler, will unfocus the CheckBox and prevent the issue you are experiencing.

    Private Sub CheckBox1_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBox1.CheckedChanged
        Me.ActiveControl = Nothing
        ' any other necessary code
    End Sub