SendKeys.Send NullReferenceException

I am trying to send keys to a control in my form. But I get a NullReferenceException, and I don't know why. The code is about the same basic as it is:

Private Sub Button19_Click(sender As System.Object, e As System.EventArgs) Handles Button19.Click
    DateTimePicker2.Focus() 'commenting out this line has no effect
    SendKeys.Send("{F4}") 'error thrown on this line
End Sub

An error is reported object reference not set to an instance of an object, but Sendis a general method, so it does not need an instance.

It's strange if I ignore the error, it works fine and F4 is passed to the control. I know that there was a problem with sendkeys and UAC, but I thought it was resolved (I am using 4.0 framework)

+3
source share
1 answer

, SendKeys.LoadSendMethodFromConfig() (, try/catch , , ).

, , , , .

"" > "" > "" " ".

throwing exception. , :

    private static void LoadSendMethodFromConfig()
    { 
        if (!sendMethod.HasValue) 
        {
            sendMethod = SendMethodTypes.Default; 

            try
            {
                // read SendKeys value from config file, not case sensitive 
                string value = System.Configuration.ConfigurationManager.AppSettings.Get("SendKeys");

                if (value.Equals("JournalHook", StringComparison.OrdinalIgnoreCase)) 
                    sendMethod = SendMethodTypes.JournalHook;
                else if (value.Equals("SendInput", StringComparison.OrdinalIgnoreCase)) 
                    sendMethod = SendMethodTypes.SendInput;
            }
            catch {} // ignore any exceptions to keep existing SendKeys behavior
        } 
    }
+6

All Articles