question

WONGTony-4228 avatar image
0 Votes"
WONGTony-4228 asked JackJJun-MSFT edited

VB.net form focus

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.

dotnet-visual-basic
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

JackJJun-MSFT avatar image
0 Votes"
JackJJun-MSFT answered JackJJun-MSFT edited

@WONGTony-4228 , 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.





5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

WONGTony-4228 avatar image
0 Votes"
WONGTony-4228 answered JackJJun-MSFT commented

Thanks

but the main form has some sub-forms to show and back to Main form

how can i make it only to Main form without affecting subforms?

· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

@WONGTony-4228, Could you provide some code or some pictures to show the Main Form and the sub-forms? It will be better for us to analyze your problem.

0 Votes 0 ·
WONGTony-4228 avatar image
0 Votes"
WONGTony-4228 answered JackJJun-MSFT edited

Thanks a lot for your help

' here to capture input in Main Form
Private Sub Form1_KeyDown(sender As Object, e As KeyEventArgs) Handles Me.KeyDown
If keyStatus = True Then
If e.KeyCode = 49 Or e.KeyCode = 50 Or e.KeyCode = 51 Or e.KeyCode = 97 Or e.KeyCode = 98 Or e.KeyCode = 99 Or e.KeyCode = 40 Or e.KeyCode = 34 Or e.KeyCode = 35 Then
do something here
End If
End If
End Sub

another subforms are simple form to display a label message. on screen i can see them switch back to main form. but sometimes in my testing environment, i do not touch anything but it look like out of focus because i have to use mouse to touch the main form again

· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

@WONGTony-4228, maybe I didn't ask the question very clearly. I want to know how do you set the Main form and subforms in your app. Please provide the code about it so that I can reproduce your problem. If you feel it troublesome, you can also upload your project to Github or Onedrive, and then share the link here.

0 Votes 0 ·
WONGTony-4228 avatar image
0 Votes"
WONGTony-4228 answered JackJJun-MSFT edited

Thanks a lot.

the out-of-focus just happens a few times, not sure the reason, maybe accidentally by user touch on mouse/keyboard or some processs?

i think there is a way to avoid it

is form.show equals to form.focus? is form.hide equals form.visible to false?

i am not sure how is the good practice to design the form and subform?

does it look like layers in photoshop? grateful for hints.

· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

@WONGTony-4228, Sorry for the late response, If you indeed want to solve your problem, I recommend that you could make a smaple project and upload your project to Github or Onedrive, and then share the link here. Because I don't know when your focus is lost.


,

0 Votes 0 ·