Question

I am using SendKeys.Send("{HOME}") in a MaskedTextBox to bring the cursor to the beginning of the textbox when the text is empty.

When I try to close the project the application freezes if it doesn't have focus.

How do I put this application in focus before I call SendKeys?

Public Class Form1

Private Sub MaskedTextBox1_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles MaskedTextBox1.GotFocus
    Try
        SendKeys.Send("{HOME}")
    Catch ex As InvalidOperationException
        ' Do nothing
    End Try
End Sub
End Class

After that, it should give focus to the previous application and close.

Was it helpful?

Solution

{HOME} is like hitting the home key, this will not bring focus.

best thing to do to bring focus is to call the focus method on the text control. like textbox.focus();

Please include your page code, or an example of what is on the page.

MaskedTextBox1.Select(0,0)

OTHER TIPS

Give the textbox a focus before the SendKeys

MaskedTextBox1.Focus()

I am using visual basic login and it it is not running at run-time. Highlights SENDKEYS as the offender. Here is my code

Private Sub cmdOK_Click() 'check for correct password If txtPassword = "password" Then 'place code to here to pass the 'success to the calling sub 'setting a global var is the easiest LoginSucceeded = True Me.Hide Else MsgBox "Invalid Password, try again!", , "Login" txtPassword.SetFocus SendKeys "{HOME}+{END}" End If End Sub

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top