views:

539

answers:

2

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.

+1  A: 

{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)

James Campbell
Try to do a new projet with 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 ' SendKeys ne peut pas s'exécuter dans cette application, car l'application ne gère pas les messages Windows. End Try End Subthen give the focus to another application and try to close your application
Jonathan
So your code will only fie if the textbox has focus. Not sure I 100% get what you are trying to do here. Let me try: You want your application to exit when your textbox has focus, then shift focus to another application?
James Campbell
No what happens is : When my maskedtextbox has the focus i want the cursor to be at the first position. My problem is : I open another application that application has the focus and then i try to close my first application. There is a deadlock and the computer freeze until u press control alt delete
Jonathan
If you want to try it. Make a new application put down a masked text box with a gotfocus event then type SendKeys.Send("{HOME}") open calc and try to close your application with the focus on calc. It will freeze.
Jonathan
try this and get rid of that sendkeys command on focus.http://msdn.microsoft.com/en-us/library/xc4fh98s.aspx
James Campbell
I've tried it doesnt work when u have a mask like : "CCCCC" and then the person click on the middle of it, it doesnt bring the cursor at the beginning
Jonathan
let's see if it does the job
Jonathan
Well thank you, it works.
Jonathan
NP, glad I could help.
James Campbell
A: 

Give the textbox a focus before the SendKeys

MaskedTextBox1.Focus()
Adir
It still freezes the screen
Jonathan