我正在尝试将键发送到窗体上的控件。但是我得到了一个NullReferenceException,我不知道为什么。代码就像它得到的一样基本:
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报告的错误是object reference not set to an instance of an object,但Send是一个共享方法,因此不需要实例。
奇怪的是,如果我忽略了这个错误,它就会正常工作,并且F4会被传递给控件。我知道sendkey和UAC有问题,但我认为这个问题已经解决了(我使用的是4.0框架)
发布于 2012-05-09 21:50:08
该调用不会抛出异常,该异常是在SendKeys.LoadSendMethodFromConfig()中抛出的,并在内部处理(因此,如果您在该调用周围放置一个try/catch,您将看到在用户代码中实际上没有捕获任何异常)。
您可以在调试器中看到它,因为您将异常设置为在抛出的任何异常时中断,而不管它在何处发生或是否已被处理。
我建议进入Tools > Options > going并选中"Enable Tools Code“旁边的复选框。
下面是抛出异常的方法。请注意,它是故意接受所有异常的:
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
}
}https://stackoverflow.com/questions/10516613
复制相似问题