1

I'm working on a project where I have an input with validation. Next to it there are a set of buttons which you can press. When you focus the input the buttons will be shown. But when you click on 1 of the buttons the focus of the input is gone and the validation will start and the buttons will be hidden.

What I want is that the input focus is not lost, at any time.

Is it possible to not activate/focus the buttons when you click on them or is it possible to force focus on the input without losing it?

I have tried this on the Control, but seems to be only working on a form:

Const WS_EX_NOACTIVATE As Integer = &H8000000
Const WS_EX_TOOLWINDOW As Integer = &H80

Protected Overrides ReadOnly Property CreateParams As CreateParams
    Get
        Dim ret As CreateParams = MyBase.CreateParams
        ret.ExStyle = ret.ExStyle Or WS_EX_NOACTIVATE Or WS_EX_TOOLWINDOW
        Return ret
    End Get
End Property
Niels
  • 48,601
  • 4
  • 62
  • 81
  • 1
    [How to stop pressing button using keyboard keys like “Spacebar or Enter”. C#](http://stackoverflow.com/questions/32823525/how-to-stop-pressing-button-using-keyboard-keys-like-spacebar-or-enter-c-shar/32825190#32825190), [How to create a winform with buttons that will never attract keyboard focus](http://stackoverflow.com/questions/10077945/how-to-create-a-winform-with-buttons-that-will-never-attract-keyboard-focus). Setting `ControlStyles.Selectable` to `false` also prevents getting focus when clicked with a mouse. – Ivan Stoev Jan 11 '16 at 11:40
  • Set the buttons' CausesValidation property to False. – Hans Passant Jan 11 '16 at 12:36

2 Answers2

3

You can create your custom button and make it non-selectable:

Public Class MyButton
    Inherits Button

    Public Sub New()
        SetStyle(ControlStyles.Selectable, False)
    End Sub

End Class

If you don't want to create custom control, set CausesValidation = False for your normal button and it will get focus, but doesn't cause validation.

Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398
0

Set the TabStop property of the buttons to false.

By setting this to false you'll stop them receiving focus when the user tabs, but they'll still be clickable.

ChrisF
  • 134,786
  • 31
  • 255
  • 325