VB.net form focus

WONG Tony 161 Reputation points
2021-09-09T03:31:05.36+00:00

i am designing a VB quiz game, it loops 20 questions

i tried to show the Form by
Form1.show
Form1.focus

it shows but sometimes it seems loss focus. i've to use the mouse to click the form, so to let the form get my input

in my testing platform, there may be unnecessary programs running. in production platform, it should be more clean. But the interrupt make me loss confidence. How can i make sure the form get focus? the program has few forms. Thanks a lot.

VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,568 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Jack J Jun 24,286 Reputation points Microsoft Vendor
    2021-09-10T03:16:04.137+00:00

    @WONG Tony , you could try to use the event LostFocus and method Control.Focus to keep the focus all the time.

    Here is a code example you could refer to.

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load  
            Me.Focus()  
            AddHandler Me.LostFocus, AddressOf Form1_LostFocus  
        End Sub  
      
        Public Sub Form1_LostFocus(sender As Object, e As EventArgs)  
            Me.Focus()  
      
        End Sub  
    

    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.